module MetalpriceAPI::Request

Public Instance Methods

delete(path, options = {}) click to toggle source
# File lib/metalpriceapi/request.rb, line 17
def delete(path, options = {})
  request(:delete, path, options)
end
get(path, options = {}) click to toggle source
# File lib/metalpriceapi/request.rb, line 5
def get(path, options = {})
  request(:get, path, options)
end
post(path, options = {}) click to toggle source
# File lib/metalpriceapi/request.rb, line 9
def post(path, options = {})
  request(:post, path, options)
end
put(path, options = {}) click to toggle source
# File lib/metalpriceapi/request.rb, line 13
def put(path, options = {})
  request(:put, path, options)
end

Private Instance Methods

request(method, path, options) click to toggle source

@param [Symbol] method - Faraday HTTP method. @param [String] path - URL to send. @param [Hash] options - :appid, :lang, :units, :endpoint, :body keys will configure the request.

The rest will be converted to query params for GET/DELETE, or jsonified for POST/PUT.

@return [Object] - the Faraday::Response#body.

# File lib/metalpriceapi/request.rb, line 31
def request(method, path, options)
  options = options.dup
  options[:api_key] ||= api_key if !api_key.nil?
  root = options.delete(:endpoint) || endpoint
  path = [root, path].join('/')
  response = connection.send(method) do |request|
    case method
    when :get, :delete
      request.url(path, options)
    when :post, :put
      request.path = path
      request.params = { appid: options.delete(:appid) }
      if options.key?(:body)
        request.body = options.delete(:body).to_json
      elsif !options.empty?
        request.body = options.to_json
      end
    end
    request.options.merge!(options.delete(:request)) if options.key?(:request)
  end
  response.body
end