class Breacan::Client
Constants
- CONVENIENCE_HEADERS
Public Class Methods
new(options = {})
click to toggle source
# File lib/breacan/client.rb, line 42 def initialize(options = {}) Breacan::Configurable.keys.each do |key| instance_variable_set(:"@#{key}", options[key] || Breacan.instance_variable_get(:"@#{key}")) end end
Public Instance Methods
agent()
click to toggle source
# File lib/breacan/client.rb, line 115 def agent @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| http.headers[:accept] = media_type http.headers[:user_agent] = user_agent end end
delete(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 107 def delete(url, options = {}) request :delete, url, options end
get(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 52 def get(url, options = {}) return get_with_auto_paginate(url, options) if auto_paginate request :get, url, parse_query_and_convenience_headers(options) end
get_with_auto_paginate(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 57 def get_with_auto_paginate(url, options = {}) res = nil if url.end_with?('.list') nextc = nil loop do r = request :get, url, parse_query_and_convenience_headers(options.merge({cursor: nextc})) if r.is_a?(Sawyer::Resource) res ||= {} res = res.deep_merge(r) do |key, this_val, other_val| if this_val.is_a?(Array) && other_val.is_a?(Array) this_val + other_val else other_val end end elsif r.is_a?(Array) res ||= [] res.concat(r) else raise "unknown response type" end if r.respond_to?(:response_metadata) if r.response_metadata.next_cursor == "" res = Sawyer::Resource.new(Sawyer::Agent.new(api_endpoint), res) break end nextc = r.response_metadata.next_cursor else break end end else res = request :get, url, parse_query_and_convenience_headers(options) end res end
head(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 111 def head(url, options = {}) request :head, url, parse_query_and_convenience_headers(options) end
patch(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 103 def patch(url, options = {}) request :patch, url, options end
post(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 95 def post(url, options = {}) request :post, url, options end
put(url, options = {})
click to toggle source
# File lib/breacan/client.rb, line 99 def put(url, options = {}) request :put, url, options end
same_options?(opts)
click to toggle source
# File lib/breacan/client.rb, line 48 def same_options?(opts) opts.hash == options.hash end
Private Instance Methods
boolean_from_response(method, path, options = {})
click to toggle source
# File lib/breacan/client.rb, line 165 def boolean_from_response(method, path, options = {}) request(method, path, options) @last_response.status == 204 rescue Breacan::NotFound false end
cache_ts()
click to toggle source
# File lib/breacan/client.rb, line 157 def cache_ts @cache_ts if defined? @cache_ts end
last_response()
click to toggle source
# File lib/breacan/client.rb, line 161 def last_response @last_response if defined? @last_response end
parse_query_and_convenience_headers(options)
click to toggle source
# File lib/breacan/client.rb, line 185 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, options = {})
click to toggle source
# File lib/breacan/client.rb, line 124 def request(method, path, data, options = {}) if data.is_a?(Hash) options[:query] = data.delete(:query) || {} options[:headers] = data.delete(:headers) || {} if accept = data.delete(:accept) options[:headers][:accept] = accept end options[:query][:token] = access_token end @last_response = response = agent.call( method, URI::Parser.new.escape(path.to_s), data, options ) simplize_body response.data end
sawyer_options()
click to toggle source
# File lib/breacan/client.rb, line 172 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[:serializer] = @serializer opts end
simplize_body(body)
click to toggle source
# File lib/breacan/client.rb, line 144 def simplize_body(body) keys = body.to_hash.keys.dup @cache_ts = if keys.include?(:cache_ts) keys.delete(:cache_ts) body.cache_ts else nil end keys.count == 1 ? body[keys.last] : body end