class Juicer::Client

Constants

BASE_PATH

Attributes

api_key[R]

@return [String] API key

Public Class Methods

new(api_key) click to toggle source

Initialize HTTP client

@param api_key [String] the API key from Apigee. @return [Juicer::Client] HTTP client instance.

# File lib/juicer/client.rb, line 24
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

request(method, path, query_params = {}, body = nil) click to toggle source

Internal method for constructing API calls to the Juicer public facing API. Automatically handles the API key when provided.

@note Internal method, should not be used directly! See {Juicer} docs

instead.

@note The Juicer API currently only supports `:get` and `:post` values for

the `method` param.

@param method [Symbol] HTTP method. @param path [String] an endpoint path. @param query_params [Hash] a hash of query parameters. List values are

handled "correctly" in the sense that values of [Array] type produce
`key[]=foo&key[]=bar` style values, suitable for Rails.

@param body [String] body of the request. Makes sense for POST requests. @return [Hash, Array<Hash>] either a list of results or one result.

# File lib/juicer/client.rb, line 43
def request(method, path, query_params = {}, body = nil)
  req                         = HTTPI::Request.new
  req.url                     = api_url(path)
  req.query                   = { apikey: api_key }.merge(query_params)
  req.body                    = body if body
  req.headers["Accept"]       = "application/json"
  req.headers["Content-Type"] = "application/json"
  JSON.parse(HTTPI.request(method, req).body)
end

Private Instance Methods

api_url(path) click to toggle source

Small helper method for sanitizing the `path` for `request` method.

@param path [String] a path to an endpoint. @return [String] sanitized and fully constructed endpoint URL.

# File lib/juicer/client.rb, line 60
def api_url(path)
  path.sub!(/^\/+/, '')
  path.sub!(/\/+$/, '')
  "#{BASE_PATH}/#{path}.json"
end