Class: Ohanakapa::Client
- Inherits:
-
Object
- Object
- Ohanakapa::Client
- 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 =
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
-
#agent ⇒ Sawyer::Agent
Hypermedia agent for the Ohana API.
-
#delete(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP DELETE request.
-
#get(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP GET request.
-
#head(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP HEAD request.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#inspect ⇒ String
Text representation of the client, masking tokens and passwords.
-
#last_response ⇒ Sawyer::Response
Response for last HTTP request.
-
#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.
- #patch(url, options = {}) ⇒ Sawyer::Resource
-
#post(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP POST request.
-
#put(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PUT request.
-
#root ⇒ Sawyer::Resource
Fetch the root resource for the API.
-
#same_options?(opts) ⇒ Boolean
Compares client options to a Hash of requested options.
Methods included from RateLimit
Methods included from Search
Methods included from Categories
#categories, #replace_all_categories
Methods included from Locations
#location, #locations, #nearby, #update_location
Methods included from Organizations
Methods included from Configurable
Methods included from Authentication
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( = {}) # Use options passed in, but fall back to module defaults Ohanakapa::Configurable.keys.each do |key| instance_variable_set(:"@#{key}", [key] || Ohanakapa.instance_variable_get(:"@#{key}")) end end |
Instance Method Details
#agent ⇒ Sawyer::Agent
Hypermedia agent for the Ohana API
150 151 152 153 154 155 156 157 158 |
# File 'lib/ohanakapa/client.rb', line 150 def agent @agent ||= Sawyer::Agent.new(api_endpoint, ) 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
101 102 103 |
# File 'lib/ohanakapa/client.rb', line 101 def delete(url, = {}) request :delete, url, end |
#get(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP GET request
64 65 66 |
# File 'lib/ohanakapa/client.rb', line 64 def get(url, = {}) request :get, url, parse_query_and_convenience_headers() end |
#head(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP HEAD request
110 111 112 |
# File 'lib/ohanakapa/client.rb', line 110 def head(url, = {}) request :head, url, parse_query_and_convenience_headers() end |
#inspect ⇒ String
Text representation of the client, masking tokens and passwords
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_response ⇒ Sawyer::Response
Response for last HTTP request
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.
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, = {}, &block) opts = parse_query_and_convenience_headers(.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
92 93 94 |
# File 'lib/ohanakapa/client.rb', line 92 def patch(url, = {}) request :patch, url, end |
#post(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP POST request
73 74 75 |
# File 'lib/ohanakapa/client.rb', line 73 def post(url, = {}) request :post, url, end |
#put(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PUT request
83 84 85 |
# File 'lib/ohanakapa/client.rb', line 83 def put(url, = {}) request :put, url, end |
#root ⇒ Sawyer::Resource
Fetch the root resource for the API
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
41 42 43 |
# File 'lib/ohanakapa/client.rb', line 41 def (opts) opts.hash == .hash end |