Class: Ohanakapa::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Categories, Locations, Organizations, RateLimit, Search, Configurable
Defined in:
lib/ohanakapa/client.rb,
lib/ohanakapa/client/search.rb,
lib/ohanakapa/client/locations.rb,
lib/ohanakapa/client/categories.rb,
lib/ohanakapa/client/rate_limit.rb,
lib/ohanakapa/client/organizations.rb

Overview

Client for the Ohana API

Defined Under Namespace

Modules: Categories, Locations, Organizations, RateLimit, Search

Constant Summary collapse

CONVENIENCE_HEADERS =

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

Set.new [:accept]

Instance Attribute Summary

Attributes included from Configurable

#api_endpoint, #api_token, #auto_paginate, #connection_options, #default_media_type, #middleware, #per_page, #proxy, #user_agent

Instance Method Summary collapse

Methods included from RateLimit

#rate_limit, #rate_limit!

Methods included from Search

#search

Methods included from Categories

#categories, #replace_all_categories

Methods included from Locations

#location, #locations, #nearby, #update_location

Methods included from Organizations

#organization, #organizations

Methods included from Configurable

#configure, keys, #reset!

Methods included from Authentication

#application_authenticated?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
# 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

Instance Method Details

#agentSawyer::Agent

Hypermedia agent for the Ohana API

Returns:

  • (Sawyer::Agent)


150
151
152
153
154
155
156
157
158
# 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 = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

Returns:

  • (Sawyer::Resource)


101
102
103
# File 'lib/ohanakapa/client.rb', line 101

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

Returns:

  • (Sawyer::Resource)


64
65
66
# File 'lib/ohanakapa/client.rb', line 64

def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

Returns:

  • (Sawyer::Resource)


110
111
112
# File 'lib/ohanakapa/client.rb', line 110

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


48
49
50
51
52
53
54
55
56
57
# 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_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


170
171
172
# File 'lib/ohanakapa/client.rb', line 170

def last_response
  @last_response
end

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

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

Parameters:

  • url (String)

    The path, relative to Ohanakapa::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • 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.

Returns:

  • (Sawyer::Resource)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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 = {}) ⇒ Sawyer::Resource

Parameters:

Returns:

  • (Sawyer::Resource)


92
93
94
# File 'lib/ohanakapa/client.rb', line 92

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

Returns:

  • (Sawyer::Resource)


73
74
75
# File 'lib/ohanakapa/client.rb', line 73

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

Returns:

  • (Sawyer::Resource)


83
84
85
# File 'lib/ohanakapa/client.rb', line 83

def put(url, options = {})
  request :put, url, options
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


163
164
165
# File 'lib/ohanakapa/client.rb', line 163

def root
  agent.start.data
end

#same_options?(opts) ⇒ Boolean

Compares client options to a Hash of requested options

Parameters:

  • opts (Hash)

    Options to compare with current client options

Returns:

  • (Boolean)


41
42
43
# File 'lib/ohanakapa/client.rb', line 41

def same_options?(opts)
  opts.hash == options.hash
end