class Ohanakapa::Client

Client for the Ohana API

@see ohanapi.herokuapp.com/api/docs

Constants

CONVENIENCE_HEADERS

Header keys that can be passed in options hash to {#get},{#head}

Public Class Methods

new(options = {}) click to toggle source
# File lib/ohanakapa/client.rb, line 30
def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  Ohanakapa::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Ohanakapa.instance_variable_get(:"@#{key}"))
  end
end

Public Instance Methods

agent() click to toggle source

Hypermedia agent for the Ohana API

@return [Sawyer::Agent]

# File lib/ohanakapa/client.rb, line 150
def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:user_agent] = user_agent
    if application_authenticated?
      http.headers["X-Api-Token"] = @api_token
    end
  end
end
delete(url, options = {}) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 101
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 64
def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end
head(url, options = {}) click to toggle source

Make a HTTP HEAD request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 110
def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end
inspect() click to toggle source

Text representation of the client, masking tokens and passwords

@return [String]

Calls superclass method
# File lib/ohanakapa/client.rb, line 48
def inspect
  inspected = super

  # Only show last 4 of api token
  if @api_token
    inspected = inspected.gsub! @api_token, "#{'*'*32}#{@api_token[32..-1]}"
  end

  inspected
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/ohanakapa/client.rb, line 170
def last_response
  @last_response
end
paginate(url, options = {}) { |data, last_response| ... } click to toggle source

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in {#auto_paginate}.

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param block [Block] Block to perform the data concatination of the

multiple requests. The block is called with two parameters, the first
contains the contents of the requests so far and the second parameter
contains the latest response.

@return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 125
def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 50 : nil)
  end

  data = request(:get, url, opts)

  if @auto_paginate
    while @last_response.rels[:next] && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end
  end

  data
end
patch(url, options = {}) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 92
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 73
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source

Make a HTTP PUT request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 83
def put(url, options = {})
  request :put, url, options
end
root() click to toggle source

Fetch the root resource for the API

@return [Sawyer::Resource]

# File lib/ohanakapa/client.rb, line 163
def root
  agent.start.data
end
same_options?(opts) click to toggle source

Compares client options to a Hash of requested options

@param opts [Hash] Options to compare with current client options @return [Boolean]

# File lib/ohanakapa/client.rb, line 41
def same_options?(opts)
  opts.hash == options.hash
end

Private Instance Methods

boolean_from_response(method, path, options = {}) click to toggle source

Executes the request, checking if it was successful

@return [Boolean] True on success, false otherwise

# File lib/ohanakapa/client.rb, line 195
def boolean_from_response(method, path, options = {})
  request(method, path, options)
  @last_response.status == 204
rescue Ohanakapa::NotFound
  false
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/ohanakapa/client.rb, line 215
def parse_query_and_convenience_headers(options)
  headers = options.fetch(:headers, {})
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {:query => options}
  opts[:query].merge!(query) if query && query.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data) click to toggle source
# File lib/ohanakapa/client.rb, line 176
def request(method, path, data)
  options = {}
  options[:query]   = data.delete(:query) || {}
  options[:headers] = data.delete(:headers) || {}

  if application_authenticated?
    options[:query].merge! application_authentication
  end
  if accept = data.delete(:accept)
    options[:headers][:accept] = accept
  end

  @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
  response.data
end
sawyer_options() click to toggle source
# File lib/ohanakapa/client.rb, line 203
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts = @connection_options
  conn_opts[:builder] = @middleware if @middleware
  conn_opts[:proxy] = @proxy if @proxy
  opts[:faraday] = Faraday.new(conn_opts)

  opts
end