class Voog::Client
Constants
- MAX_PER_PAGE
Attributes
api_token[R]
auto_paginate[R]
host[R]
per_page[R]
protocol[R]
Public Class Methods
new(host = Voog.host, api_token = Voog.api_token, options = {})
click to toggle source
@param host [String] a Voog
site host. @param api_token
[String] a Voog
site API
token. @option options [String] :protocol endpoint protocol (“http” or “https”). Defaults to “http”. @option options [String] :auto_paginate enable auto pagination for list requests. Defaults to “false”. @option options [String] :per_page set default “per_page” value for list requests. Defaults to “nil”. @option options [Boolean] :raise_on_error interrupts program with error (“Faraday::Error”) when request response code is between “400” and “600” (default is “false”). @example Initialize client
client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, raise_on_error: false)
# File lib/voog_api/client.rb, line 71 def initialize(host = Voog.host, api_token = Voog.api_token, options = {}) @host = host @api_token = api_token @options = options @protocol = options[:protocol].to_s.downcase == 'https' ? 'https' : 'http' @auto_paginate = options.fetch(:auto_paginate, Voog.auto_paginate) @per_page = options.fetch(:per_page, Voog.per_page) @raise_on_error = options.fetch(:raise_on_error, true) end
Public Instance Methods
agent()
click to toggle source
# File lib/voog_api/client.rb, line 113 def agent @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| http.headers[:content_type] = 'application/json' http.headers[:accept] = 'application/json' http.headers[:user_agent] = 'Voog.rb Ruby wrapper' http.headers[:x_api_token] = @api_token end end
api_endpoint()
click to toggle source
# File lib/voog_api/client.rb, line 105 def api_endpoint "#{host_with_protocol}/admin/api".freeze end
delete(url, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 97 def delete(url, options = {}) request :delete, url, nil, options end
get(url, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 81 def get(url, options = {}) request :get, url, nil, options end
head(url, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 101 def head(url, options = {}) request :head, url, nil, options end
host_with_protocol()
click to toggle source
# File lib/voog_api/client.rb, line 109 def host_with_protocol "#{protocol}://#{host}".freeze end
last_response()
click to toggle source
# File lib/voog_api/client.rb, line 133 def last_response @last_response end
multipart_agent()
click to toggle source
# File lib/voog_api/client.rb, line 122 def multipart_agent @multipart_agent ||= Faraday.new do |faraday| faraday.request :multipart faraday.response :raise_error if @raise_on_error faraday.adapter :net_http faraday.headers[:x_api_token] = @api_token faraday.headers[:user_agent] = 'Voog.rb Ruby wrapper' end end
paginate(url, options = {}) { |data, last_response| ... }
click to toggle source
Fetch all elements for requested API
resource when {#auto_paginate} is turned on.
@see www.voog.com/developers/api/basics/pagination
# File lib/voog_api/client.rb, line 144 def paginate(url, options = {}, &block) opts = options.dup if @auto_paginate || @per_page opts[:query] ||= {} opts[:query][:per_page] ||= @per_page || (@auto_paginate ? MAX_PER_PAGE : nil) end data = request(:get, url, nil, opts) if @auto_paginate while @last_response.rels[:next] @last_response = @last_response.rels[:next].get(headers: opts[:headers]) 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
parse_response(response)
click to toggle source
# File lib/voog_api/client.rb, line 137 def parse_response(response) JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} end
patch(url, data, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 93 def patch(url, data, options = {}) request :patch, url, data, options end
post(url, data, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 85 def post(url, data, options = {}) request :post, url, data, options end
put(url, data, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 89 def put(url, data, options = {}) request :put, url, data, options end
Private Instance Methods
request(method, path, data, options = {})
click to toggle source
# File lib/voog_api/client.rb, line 169 def request(method, path, data, options = {}) multipart = options.fetch(:multipart, false) && (method == :post) @last_response = response = multipart ? \ multipart_agent.post("#{api_endpoint}/#{path}", data) : \ agent.call(method, URI.encode(path.to_s), data, options.dup) raise Voog::MovedPermanently.new(response, host_with_protocol) if response.status == 301 if multipart parse_response(response.body) else response.data end end
sawyer_options(multipart = false)
click to toggle source
# File lib/voog_api/client.rb, line 185 def sawyer_options(multipart = false) faraday = Faraday.new do |faraday| faraday.response :raise_error if @raise_on_error faraday.adapter :net_http end opts = { links_parser: Sawyer::LinkParsers::Simple.new, faraday: faraday } opts end