Class: EaseHTTP::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ease_http/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
# File 'lib/ease_http/connection.rb', line 7

def initialize(endpoint)
  @endpoint = endpoint
  uri = URI(endpoint)

  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = uri.scheme == 'https'
end

Instance Method Details

#get(path, options = {}, &block) ⇒ Object

path - path to request options

:headers
:query


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ease_http/connection.rb', line 19

def get(path, options={}, &block)
  uri = URI.join(@endpoint, path)
  headers = options.delete(:headers)
  query = options.delete(:query)

  if query
    uri.query = URI.encode_www_form(query)
  end

  @http.request_get uri.request_uri, headers, &block
end

#post(path, options = {}, &block) ⇒ Object

path - path to request options

:headers
:data


35
36
37
38
39
40
# File 'lib/ease_http/connection.rb', line 35

def post(path, options={}, &block)
  headers = options.delete :headers
  data = options.delete :data

  @http.request_post path, data, headers, &block
end