Class: Voog::Client
- Inherits:
-
Object
- Object
- Voog::Client
- Includes:
- API::Articles, API::Assets, API::BuyButtons, API::Comments, API::ContentPartials, API::Contents, API::ElementDefinitions, API::Elements, API::Forms, API::Languages, API::LayoutAssets, API::Layouts, API::MediaSets, API::Nodes, API::Pages, API::People, API::ProductWidgets, API::RedirectRules, API::Search, API::Site, API::SiteUsers, API::Tags, API::Texts, API::Tickets
- Defined in:
- lib/voog_api/client.rb
Overview
Voog API client.
Constant Summary collapse
- MAX_PER_PAGE =
250
Instance Attribute Summary collapse
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
-
#auto_paginate ⇒ Object
readonly
Returns the value of attribute auto_paginate.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
- #agent ⇒ Object
- #api_endpoint ⇒ Object
- #delete(url, options = {}) ⇒ Object
- #get(url, options = {}) ⇒ Object
- #head(url, options = {}) ⇒ Object
- #host_with_protocol ⇒ Object
-
#initialize(host = Voog.host, api_token = Voog.api_token, options = {}) ⇒ Client
constructor
Initialize Voog API client.
- #last_response ⇒ Object
- #multipart_agent ⇒ Object
-
#paginate(url, options = {}, &block) ⇒ Object
Fetch all elements for requested API resource when #auto_paginate is turned on.
- #parse_response(response) ⇒ Object
- #patch(url, data, options = {}) ⇒ Object
- #post(url, data, options = {}) ⇒ Object
- #put(url, data, options = {}) ⇒ Object
Methods included from API::Tickets
#delete_spam_tickets, #delete_ticket, #delete_tickets, #ticket, #tickets
Methods included from API::Texts
Methods included from API::Tags
#delete_tag, #tag, #tags, #update_tag
Methods included from API::SiteUsers
#create_site_user, #delete_site_user, #site_user, #site_users
Methods included from API::Site
#delete_site_data, #patch_site, #site, #update_site, #update_site_data
Methods included from API::Search
Methods included from API::RedirectRules
#create_redirect_rule, #delete_redirect_rule, #redirect_rule, #redirect_rules, #update_redirect_rule
Methods included from API::ProductWidgets
#product_widget, #product_widgets, #update_product_widget
Methods included from API::People
Methods included from API::Pages
#create_page, #create_page_content, #delete_page, #delete_page_content, #delete_page_data, #move_page_content, #page, #page_content, #page_contents, #pages, #patch_page, #update_page, #update_page_data
Methods included from API::Nodes
#move_node, #node, #nodes, #update_node
Methods included from API::MediaSets
#create_media_set, #delete_media_set, #media_set, #media_set_add_assets, #media_sets, #update_media_set
Methods included from API::Layouts
#create_layout, #delete_layout, #layout, #layouts, #update_layout
Methods included from API::LayoutAssets
#create_layout_asset, #delete_layout_asset, #layout_asset, #layout_assets, #update_layout_asset
Methods included from API::Languages
#create_language, #create_language_content, #delete_language, #delete_language_content, #enable_language_autodetect, #language, #language_content, #language_contents, #languages, #move_language, #move_language_content, #update_language
Methods included from API::Forms
#delete_form, #form, #forms, #update_form
Methods included from API::Elements
#create_element, #create_element_content, #delete_element, #delete_element_content, #element, #element_content, #element_contents, #elements, #move_element, #move_element_content, #update_element
Methods included from API::ElementDefinitions
#create_element_definition, #delete_element_definition, #element_definition, #element_definitions, #update_element_definition
Methods included from API::Contents
#content, #contents, #create_content, #delete_content, #move_content
Methods included from API::ContentPartials
#content_partial, #content_partials, #update_content_partial
Methods included from API::Comments
#comment, #comments, #create_comment, #delete_comment, #delete_spam_comments, #toggle_spam_comment
Methods included from API::BuyButtons
#buy_button, #buy_buttons, #update_buy_button
Methods included from API::Assets
#asset, #assets, #confirm_asset, #create_asset, #delete_asset
Methods included from API::Articles
#article, #article_content, #article_contents, #articles, #create_article, #create_article_content, #delete_article, #delete_article_content, #delete_article_data, #move_article_content, #patch_article, #update_article, #update_article_data
Constructor Details
#initialize(host = Voog.host, api_token = Voog.api_token, options = {}) ⇒ Client
Initialize Voog API client.
82 83 84 85 86 87 88 89 90 |
# File 'lib/voog_api/client.rb', line 82 def initialize(host = Voog.host, api_token = Voog.api_token, = {}) @host = host @api_token = api_token = @protocol = [:protocol].to_s.downcase == 'https' ? 'https' : 'http' @auto_paginate = .fetch(:auto_paginate, Voog.auto_paginate) @per_page = .fetch(:per_page, Voog.per_page) @raise_on_error = .fetch(:raise_on_error, true) end |
Instance Attribute Details
#api_token ⇒ Object (readonly)
Returns the value of attribute api_token.
70 71 72 |
# File 'lib/voog_api/client.rb', line 70 def api_token @api_token end |
#auto_paginate ⇒ Object (readonly)
Returns the value of attribute auto_paginate.
70 71 72 |
# File 'lib/voog_api/client.rb', line 70 def auto_paginate @auto_paginate end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
70 71 72 |
# File 'lib/voog_api/client.rb', line 70 def host @host end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
70 71 72 |
# File 'lib/voog_api/client.rb', line 70 def per_page @per_page end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
70 71 72 |
# File 'lib/voog_api/client.rb', line 70 def protocol @protocol end |
Instance Method Details
#agent ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/voog_api/client.rb', line 124 def agent @agent ||= Sawyer::Agent.new(api_endpoint, ) 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 ⇒ Object
116 117 118 |
# File 'lib/voog_api/client.rb', line 116 def api_endpoint "#{host_with_protocol}/admin/api".freeze end |
#delete(url, options = {}) ⇒ Object
108 109 110 |
# File 'lib/voog_api/client.rb', line 108 def delete(url, = {}) request :delete, url, nil, end |
#get(url, options = {}) ⇒ Object
92 93 94 |
# File 'lib/voog_api/client.rb', line 92 def get(url, = {}) request :get, url, nil, end |
#head(url, options = {}) ⇒ Object
112 113 114 |
# File 'lib/voog_api/client.rb', line 112 def head(url, = {}) request :head, url, nil, end |
#host_with_protocol ⇒ Object
120 121 122 |
# File 'lib/voog_api/client.rb', line 120 def host_with_protocol "#{protocol}://#{host}".freeze end |
#last_response ⇒ Object
144 145 146 |
# File 'lib/voog_api/client.rb', line 144 def last_response @last_response end |
#multipart_agent ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/voog_api/client.rb', line 133 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 = {}, &block) ⇒ Object
Fetch all elements for requested API resource when #auto_paginate is turned on.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/voog_api/client.rb', line 155 def paginate(url, = {}, &block) opts = .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) ⇒ Object
148 149 150 |
# File 'lib/voog_api/client.rb', line 148 def parse_response(response) JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} end |
#patch(url, data, options = {}) ⇒ Object
104 105 106 |
# File 'lib/voog_api/client.rb', line 104 def patch(url, data, = {}) request :patch, url, data, end |
#post(url, data, options = {}) ⇒ Object
96 97 98 |
# File 'lib/voog_api/client.rb', line 96 def post(url, data, = {}) request :post, url, data, end |
#put(url, data, options = {}) ⇒ Object
100 101 102 |
# File 'lib/voog_api/client.rb', line 100 def put(url, data, = {}) request :put, url, data, end |