Class: Stretch::Connection

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

Constant Summary collapse

REQUEST_METHODS =
[ :get, :post, :put, :delete ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



10
11
12
# File 'lib/stretch/connection.rb', line 10

def initialize options = {}
  @connection = Faraday.new(options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/stretch/connection.rb', line 6

def connection
  @connection
end

Instance Method Details

#request(method, path, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stretch/connection.rb', line 14

def request method, path, options = {}
  validate_request_method method

  response = connection.send(method) do |request|
    request.headers["Accept"] = "application/json"
    request.path = path

    case method
    when :get
      options.each { |k,v| request.params[k] = v }
    when :post, :put
      request.body = MultiJson.dump(options)
    end
  end

  handle_response response
end