Module: EventedNet::HTTP::Get

Included in:
EventedNet::HTTP
Defined in:
lib/http/get.rb

Instance Method Summary collapse

Instance Method Details

#evented_get(uri, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/http/get.rb', line 16

def evented_get(uri, opts = {})
  http = EventedNet::HTTP::Connection.request(
    :host => uri.host, :port => uri.port,
    :request => uri.path, :query => uri.query
  )
  # Assign the user generated callback, as the callback for 
  # EM::Protocols::HttpClient
  http.callback { |r| opts[:callback].call(r[:status], r[:content]) }
end

#get(uri, opts = {}) ⇒ Object



4
5
6
7
8
9
# File 'lib/http/get.rb', line 4

def get(uri, opts = {})
  unless uri.is_a?(URI) && (opts[:callback].is_a?(Proc) || opts[:callback].is_a?(Method)) && opts[:callback].arity == 2
    raise ArgumentError, "uri must be a URI and opts[:callback] must be a Proc (or Method) which takes 2 args"
  end
  EM.reactor_running? ? evented_get(uri, opts) : synchronous_get(uri, opts)
end

#synchronous_get(uri, opts = {}) ⇒ Object



11
12
13
14
# File 'lib/http/get.rb', line 11

def synchronous_get(uri, opts = {})
  r = Net::HTTP.get_response(uri)
  opts[:callback].call(r.code, r.body)
end