class Stretch::Connection

Constants

REQUEST_METHODS

Attributes

connection[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/stretch/connection.rb, line 10
def initialize options = {}
  @connection = Faraday.new(options)
end

Public Instance Methods

request(method, path, options = {}) click to toggle source
# 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

Private Instance Methods

handle_response(response) click to toggle source
# File lib/stretch/connection.rb, line 39
def handle_response response
  if response.success?
    if response.body.empty?
      { "ok" => true }
    else
      MultiJson.load response.body
    end
  else
    response.error!
  end
end
validate_request_method(method) click to toggle source
# File lib/stretch/connection.rb, line 33
def validate_request_method method
  unless REQUEST_METHODS.member? method
    raise Stretch::UnsupportedRequestMethod, "#{method} is not supported!"
  end
end