lib/voog_api/client.rb in voog_api-0.0.10 vs lib/voog_api/client.rb in voog_api-0.0.11
- old
+ new
@@ -17,16 +17,21 @@
require 'voog_api/api/media_sets'
require 'voog_api/api/nodes'
require 'voog_api/api/site'
require 'voog_api/api/pages'
require 'voog_api/api/people'
+require 'voog_api/api/search'
+require 'voog_api/api/site_users'
require 'voog_api/api/tags'
require 'voog_api/api/texts'
require 'voog_api/api/tickets'
module Voog
-
+
+ # Voog API client.
+ #
+ # @see http://www.voog.com/developers/api
class Client
MAX_PER_PAGE = 250
include Voog::API::Articles
@@ -42,16 +47,18 @@
include Voog::API::LayoutAssets
include Voog::API::MediaSets
include Voog::API::Nodes
include Voog::API::Pages
include Voog::API::People
+ include Voog::API::Search
include Voog::API::Site
+ include Voog::API::SiteUsers
include Voog::API::Tags
include Voog::API::Texts
include Voog::API::Tickets
- attr_reader :api_token, :host, :auto_paginate, :per_page
+ attr_reader :api_token, :host, :protocol, :auto_paginate, :per_page
# Initialize Voog API client.
#
# @param host [String] a Voog site host.
# @param api_token [String] a Voog site API token.
@@ -94,12 +101,16 @@
def head(url, options = {})
request :head, url, nil, options
end
def api_endpoint
- "#{@protocol}://#{host}/admin/api"
+ "#{host_with_protocol}/admin/api".freeze
end
+
+ def host_with_protocol
+ "#{protocol}://#{host}".freeze
+ end
def agent
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
http.headers[:content_type] = 'application/json'
http.headers[:accept] = 'application/json'
@@ -126,22 +137,22 @@
def parse_response(response)
JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end
# Fetch all elements for requested API resource when {#auto_paginate} is turned on.
+ #
+ # @see http://www.voog.com/developers/api/basics/pagination
def paginate(url, options = {}, &block)
opts = options.dup
if @auto_paginate || @per_page
opts[:query][:per_page] ||= @per_page || (@auto_paginate ? MAX_PER_PAGE : nil)
end
data = request(:get, url, nil, opts)
if @auto_paginate
- i = 0
while @last_response.rels[:next]
- puts "Request: #{i += 1}"
@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)
@@ -159,10 +170,10 @@
@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.headers['location']) if response.status == 301
+ raise Voog::MovedPermanently.new(response, host_with_protocol) if response.status == 301
if multipart
parse_response(response.body)
else
response.data