Skip to content

Commit

Permalink
Added auto pagination option for listing endpoints
Browse files Browse the repository at this point in the history
Allow allow to set default "per_page" value

Example usage:

  client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', auto_paginate: true, per_page: 250)

Closes #3
  • Loading branch information
tanelj committed Feb 8, 2016
1 parent 8cbfe3b commit cdc418b
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To generate your personal **API token** for your site, go to `Account` -> `My pr
All requests to the Voog API are done through the `Voog::Client` class, which takes two parameters: site host and API token.

```ruby
client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, raise_on_error: false)
client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, auto_paginate: true, raise_on_error: false)
```

Making an API request is as simple as calling a single method on the client:
Expand All @@ -59,4 +59,6 @@ puts layout[:title]
### Optional parameters for client

* `protocol` - endpoint protocol (`http` or `https` default is `http`).
* `auto_paginate` - enable auto pagination for list requests. Defaults to `false`.
* `per_page` - set default `per_page` value for list requests. Defaults to `nil` (API default is `50`).
* `raise_on_error` - interrupts program with error (`Faraday::Error`) when request response code is between `400` and `600` (default is `false`).
2 changes: 1 addition & 1 deletion lib/voog_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Voog

class << self

attr_accessor :host, :api_token
attr_accessor :host, :api_token, :auto_paginate, :per_page

def client(options = {})
unless host.nil? && api_token.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Articles

# List articles
def articles(params = {})
get 'articles', {query: params}
paginate 'articles', {query: params}
end

# Get a single article
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Assets

# List assets
def assets(params = {})
get 'assets', {query: params}
paginate 'assets', {query: params}
end

# Get a single asset
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Comments

# List article comments
def comments(article_id, params = {})
get "articles/#{article_id}/comments", {query: params}
paginate "articles/#{article_id}/comments", {query: params}
end

# Get a single comment for article
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/content_partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ContentPartials

# List content partials contents
def content_partials(params = {})
get 'content_partials', {query: params}
paginate 'content_partials', {query: params}
end

# Get a single content partial content
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ParentKind

# List contents
def contents(parent_name, parent_id, params = {})
get "#{parent_name}/#{parent_id}/contents", {query: params}
paginate "#{parent_name}/#{parent_id}/contents", {query: params}
end

# Get a single content
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/element_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ElementDefinitions

# List element_definitions
def element_definitions(params = {})
get 'element_definitions', {query: params}
paginate 'element_definitions', {query: params}
end

# Get a single element_definition
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Elements

# List elements
def elements(params = {})
get 'elements', {query: params}
paginate 'elements', {query: params}
end

# Get a single element
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Forms

# List forms
def forms(params = {})
get 'forms', {query: params}
paginate 'forms', {query: params}
end

# Get a single form
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/languages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Languages

# List languages
def languages(params = {})
get 'languages', {query: params}
paginate 'languages', {query: params}
end

# Get a single language
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/layout_assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module LayoutAssets

# List layouts assets
def layout_assets(params = {})
get 'layout_assets', {query: params}
paginate 'layout_assets', {query: params}
end

# Get a single layout asset
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Layouts

# List layouts
def layouts(params = {})
get 'layouts', {query: params}
paginate 'layouts', {query: params}
end

# Get a single layout
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/media_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module MediaSets

# List media_sets
def media_sets(params = {})
get 'media_sets', {query: params}
paginate 'media_sets', {query: params}
end

# Get a single media_set
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Nodes

# List nodes
def nodes(params = {})
get 'nodes', {query: params}
paginate 'nodes', {query: params}
end

# Get a single node
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Pages

# List pages
def pages(params = {})
get 'pages', {query: params}
paginate 'pages', {query: params}
end

# Get a single page
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/people.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module People

# List people
def people(params = {})
get 'people', {query: params}
paginate 'people', {query: params}
end

# Get a single person
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Tags

# List tags
def tags(params = {})
get 'tags', {query: params}
paginate 'tags', {query: params}
end

# Get a single tag
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/texts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Texts

# List text contents
def texts(params = {})
get 'texts', {query: params}
paginate 'texts', {query: params}
end

# Get a single text content
Expand Down
2 changes: 1 addition & 1 deletion lib/voog_api/api/tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Tickets

# List form tickets
def tickets(form_id, params = {})
get "forms/#{form_id}/tickets", {query: params}
paginate "forms/#{form_id}/tickets", {query: params}
end

# Get a single ticket for form
Expand Down
37 changes: 34 additions & 3 deletions lib/voog_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
module Voog

class Client


MAX_PER_PAGE = 250

include Voog::API::Articles
include Voog::API::Assets
include Voog::API::Comments
Expand All @@ -46,13 +48,15 @@ class Client
include Voog::API::Texts
include Voog::API::Tickets

attr_reader :api_token, :host
attr_reader :api_token, :host, :auto_paginate, :per_page

# Initialize Voog API client.
#
# @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)
Expand All @@ -61,6 +65,8 @@ def initialize(host = Voog.host, api_token = Voog.api_token, options = {})
@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

Expand Down Expand Up @@ -120,7 +126,32 @@ def last_response
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.
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)
end
end
end

data
end

private

def request(method, path, data, options = {})
Expand Down

0 comments on commit cdc418b

Please sign in to comment.