Class: Voog::Client

Overview

Voog API client.

Constant Summary collapse

MAX_PER_PAGE =
250

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Tickets

#delete_spam_tickets, #delete_ticket, #delete_tickets, #ticket, #tickets

Methods included from API::Texts

#text, #texts, #update_text

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

#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

#people, #person

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.

Examples:

Initialize client

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

Parameters:

  • host (String) (defaults to: Voog.host)

    a Voog site host.

  • api_token (String) (defaults to: Voog.api_token)

    a Voog site API token.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :protocol (String)

    endpoint protocol (“http” or “https”). Defaults to “http”.

  • :auto_paginate (String)

    enable auto pagination for list requests. Defaults to “false”.

  • :per_page (String)

    set default “per_page” value for list requests. Defaults to “nil”.

  • :raise_on_error (Boolean)

    interrupts program with error (“Faraday::Error”) when request response code is between “400” and “600” (default is “false”).



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, 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

Instance Attribute Details

#api_tokenObject (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_paginateObject (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

#hostObject (readonly)

Returns the value of attribute host.



70
71
72
# File 'lib/voog_api/client.rb', line 70

def host
  @host
end

#per_pageObject (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

#protocolObject (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

#agentObject



124
125
126
127
128
129
130
131
# File 'lib/voog_api/client.rb', line 124

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_endpointObject



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, options = {})
  request :delete, url, nil, options
end

#get(url, options = {}) ⇒ Object



92
93
94
# File 'lib/voog_api/client.rb', line 92

def get(url, options = {})
  request :get, url, nil, options
end

#head(url, options = {}) ⇒ Object



112
113
114
# File 'lib/voog_api/client.rb', line 112

def head(url, options = {})
  request :head, url, nil, options
end

#host_with_protocolObject



120
121
122
# File 'lib/voog_api/client.rb', line 120

def host_with_protocol
  "#{protocol}://#{host}".freeze
end

#last_responseObject



144
145
146
# File 'lib/voog_api/client.rb', line 144

def last_response
  @last_response
end

#multipart_agentObject



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, 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) ⇒ 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, options = {})
  request :patch, url, data, options
end

#post(url, data, options = {}) ⇒ Object



96
97
98
# File 'lib/voog_api/client.rb', line 96

def post(url, data, options = {})
  request :post, url, data, options
end

#put(url, data, options = {}) ⇒ Object



100
101
102
# File 'lib/voog_api/client.rb', line 100

def put(url, data, options = {})
  request :put, url, data, options
end