Class: Voog::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, #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::Site

#delete_site_data, #patch_site, #site, #update_site, #update_site_data

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::LayoutAssets

#create_layout_asset, #delete_layout_asset, #layout_asset, #layout_assets, #update_layout_asset

Methods included from API::Layouts

#create_layout, #delete_layout, #layout, #layouts, #update_layout

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

#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::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”).



64
65
66
67
68
69
70
71
72
# File 'lib/voog_api/client.rb', line 64

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.



52
53
54
# File 'lib/voog_api/client.rb', line 52

def api_token
  @api_token
end

#auto_paginateObject (readonly)

Returns the value of attribute auto_paginate.



52
53
54
# File 'lib/voog_api/client.rb', line 52

def auto_paginate
  @auto_paginate
end

#hostObject (readonly)

Returns the value of attribute host.



52
53
54
# File 'lib/voog_api/client.rb', line 52

def host
  @host
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



52
53
54
# File 'lib/voog_api/client.rb', line 52

def per_page
  @per_page
end

Instance Method Details

#agentObject



102
103
104
105
106
107
108
109
# File 'lib/voog_api/client.rb', line 102

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



98
99
100
# File 'lib/voog_api/client.rb', line 98

def api_endpoint
  "#{@protocol}://#{host}/admin/api"
end

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



90
91
92
# File 'lib/voog_api/client.rb', line 90

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

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



74
75
76
# File 'lib/voog_api/client.rb', line 74

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

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



94
95
96
# File 'lib/voog_api/client.rb', line 94

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

#last_responseObject



122
123
124
# File 'lib/voog_api/client.rb', line 122

def last_response
  @last_response
end

#multipart_agentObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/voog_api/client.rb', line 111

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.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/voog_api/client.rb', line 131

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

#parse_response(response) ⇒ Object



126
127
128
# File 'lib/voog_api/client.rb', line 126

def parse_response(response)
  JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end

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



86
87
88
# File 'lib/voog_api/client.rb', line 86

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

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



78
79
80
# File 'lib/voog_api/client.rb', line 78

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

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



82
83
84
# File 'lib/voog_api/client.rb', line 82

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