voog_api 0.0.7 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.markdown +10 -1
- data/Rakefile +7 -1
- data/lib/voog_api/api/articles.rb +3 -3
- data/lib/voog_api/api/assets.rb +1 -1
- data/lib/voog_api/api/comments.rb +1 -1
- data/lib/voog_api/api/content_partials.rb +1 -1
- data/lib/voog_api/api/contents.rb +1 -1
- data/lib/voog_api/api/element_definitions.rb +1 -1
- data/lib/voog_api/api/elements.rb +1 -1
- data/lib/voog_api/api/forms.rb +1 -1
- data/lib/voog_api/api/languages.rb +1 -1
- data/lib/voog_api/api/layout_assets.rb +2 -1
- data/lib/voog_api/api/layouts.rb +1 -1
- data/lib/voog_api/api/media_sets.rb +2 -2
- data/lib/voog_api/api/nodes.rb +1 -1
- data/lib/voog_api/api/pages.rb +3 -3
- data/lib/voog_api/api/people.rb +1 -1
- data/lib/voog_api/api/site.rb +2 -2
- data/lib/voog_api/api/tags.rb +1 -1
- data/lib/voog_api/api/texts.rb +1 -1
- data/lib/voog_api/api/tickets.rb +1 -1
- data/lib/voog_api/client.rb +53 -8
- data/lib/voog_api/error.rb +6 -0
- data/lib/voog_api/version.rb +1 -1
- data/lib/voog_api.rb +1 -1
- data/spec/voog_api/api/media_sets_spec.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7f44b299ce7547c3f02afb6ef7c359dc1bb227f
|
4
|
+
data.tar.gz: a11ac4014716a5123f0402e32dc7dbd43b468a47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2a7313731a067a53d33b31c39c5d0940dc53d6a150781562850ac46a2e08a937d2c2324e5fe048e46f51dfc1b4410753ecebc5167da3400463686b018d9f0fb
|
7
|
+
data.tar.gz: 31553dcd752fad8a075132e403a50e6df4552b4ca8f2ddc3547ce33688e569b81c67baaf4a9f9a6241507a8972ae00f55d8774e9c6b4b06bfca6fa205edc7624
|
data/README.markdown
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# voog.rb
|
2
2
|
|
3
|
+
[ ](https://www.codeship.io/projects/30460)
|
4
|
+
|
3
5
|
Ruby toolkit for the [Voog](http://www.voog.com) API.
|
4
6
|
|
5
7
|
Learn more at the [Voog developer central](http://www.voog.com/developers/api) and by reading the [Voog API documentation](http://www.voog.com/developers/api).
|
@@ -33,7 +35,7 @@ To generate your personal **API token** for your site, go to `Account` -> `My pr
|
|
33
35
|
All requests to the Voog API are done through the `Voog::Client` class, which takes two parameters: site host and API token.
|
34
36
|
|
35
37
|
```ruby
|
36
|
-
client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14')
|
38
|
+
client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, auto_paginate: true, raise_on_error: false)
|
37
39
|
```
|
38
40
|
|
39
41
|
Making an API request is as simple as calling a single method on the client:
|
@@ -53,3 +55,10 @@ puts layout.title
|
|
53
55
|
puts layout[:title]
|
54
56
|
# => "Front page"
|
55
57
|
```
|
58
|
+
|
59
|
+
### Optional parameters for client
|
60
|
+
|
61
|
+
* `protocol` - endpoint protocol (`http` or `https` default is `http`).
|
62
|
+
* `auto_paginate` - enable auto pagination for list requests. Defaults to `false`.
|
63
|
+
* `per_page` - set default `per_page` value for list requests. Defaults to `nil` (API default is `50`).
|
64
|
+
* `raise_on_error` - interrupts program with error (`Faraday::Error`) when request response code is between `400` and `600` (default is `false`).
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ module Voog
|
|
4
4
|
|
5
5
|
# List articles
|
6
6
|
def articles(params = {})
|
7
|
-
|
7
|
+
paginate 'articles', {query: params}
|
8
8
|
end
|
9
9
|
|
10
10
|
# Get a single article
|
@@ -34,14 +34,14 @@ module Voog
|
|
34
34
|
|
35
35
|
# Update/create a key in article data field
|
36
36
|
#
|
37
|
-
# @
|
37
|
+
# @param id [String] key in data field
|
38
38
|
def update_article_data(article_id, id, data)
|
39
39
|
put "articles/#{article_id}/data/#{id}", {value: data}
|
40
40
|
end
|
41
41
|
|
42
42
|
# Delete a key from article data field
|
43
43
|
#
|
44
|
-
# @
|
44
|
+
# @param id [String] key in data field
|
45
45
|
def delete_article_data(article_id, id)
|
46
46
|
delete "articles/#{article_id}/data/#{id}"
|
47
47
|
end
|
data/lib/voog_api/api/assets.rb
CHANGED
data/lib/voog_api/api/forms.rb
CHANGED
@@ -4,7 +4,7 @@ module Voog
|
|
4
4
|
|
5
5
|
# List layouts assets
|
6
6
|
def layout_assets(params = {})
|
7
|
-
|
7
|
+
paginate 'layout_assets', {query: params}
|
8
8
|
end
|
9
9
|
|
10
10
|
# Get a single layout asset
|
@@ -15,6 +15,7 @@ module Voog
|
|
15
15
|
# Create a layout asset
|
16
16
|
def create_layout_asset(data)
|
17
17
|
if data && data.key?(:file)
|
18
|
+
data = data.dup
|
18
19
|
data[:file] = Faraday::UploadIO.new(data[:file], data[:content_type])
|
19
20
|
options = {multipart: true}
|
20
21
|
end
|
data/lib/voog_api/api/layouts.rb
CHANGED
@@ -4,7 +4,7 @@ module Voog
|
|
4
4
|
|
5
5
|
# List media_sets
|
6
6
|
def media_sets(params = {})
|
7
|
-
|
7
|
+
paginate 'media_sets', {query: params}
|
8
8
|
end
|
9
9
|
|
10
10
|
# Get a single media_set
|
@@ -29,7 +29,7 @@ module Voog
|
|
29
29
|
|
30
30
|
# Add assets to media_set
|
31
31
|
def media_set_add_assets(media_set_id, data)
|
32
|
-
|
32
|
+
post "media_sets/#{media_set_id}/add_assets", data
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/lib/voog_api/api/nodes.rb
CHANGED
data/lib/voog_api/api/pages.rb
CHANGED
@@ -4,7 +4,7 @@ module Voog
|
|
4
4
|
|
5
5
|
# List pages
|
6
6
|
def pages(params = {})
|
7
|
-
|
7
|
+
paginate 'pages', {query: params}
|
8
8
|
end
|
9
9
|
|
10
10
|
# Get a single page
|
@@ -34,14 +34,14 @@ module Voog
|
|
34
34
|
|
35
35
|
# Update/create a key in page data field
|
36
36
|
#
|
37
|
-
# @
|
37
|
+
# @param id [String] key in data field
|
38
38
|
def update_page_data(page_id, id, data)
|
39
39
|
put "pages/#{page_id}/data/#{id}", {value: data}
|
40
40
|
end
|
41
41
|
|
42
42
|
# Delete a key from page data field
|
43
43
|
#
|
44
|
-
# @
|
44
|
+
# @param id [String] key in data field
|
45
45
|
def delete_page_data(page_id, id)
|
46
46
|
delete "pages/#{page_id}/data/#{id}"
|
47
47
|
end
|
data/lib/voog_api/api/people.rb
CHANGED
data/lib/voog_api/api/site.rb
CHANGED
@@ -19,14 +19,14 @@ module Voog
|
|
19
19
|
|
20
20
|
# Update/create a key in site data field
|
21
21
|
#
|
22
|
-
# @
|
22
|
+
# @param id [String] key in data field
|
23
23
|
def update_site_data(id, data)
|
24
24
|
put "site/data/#{id}", {value: data}
|
25
25
|
end
|
26
26
|
|
27
27
|
# Delete a key from site data field
|
28
28
|
#
|
29
|
-
# @
|
29
|
+
# @param id [String] key in data field
|
30
30
|
def delete_site_data(id)
|
31
31
|
delete "site/data/#{id}"
|
32
32
|
end
|
data/lib/voog_api/api/tags.rb
CHANGED
data/lib/voog_api/api/texts.rb
CHANGED
data/lib/voog_api/api/tickets.rb
CHANGED
data/lib/voog_api/client.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'sawyer'
|
3
3
|
|
4
|
+
require 'voog_api/error'
|
5
|
+
|
4
6
|
require 'voog_api/api/articles'
|
5
7
|
require 'voog_api/api/assets'
|
6
8
|
require 'voog_api/api/comments'
|
@@ -24,7 +26,9 @@ require 'voog_api/api/tickets'
|
|
24
26
|
module Voog
|
25
27
|
|
26
28
|
class Client
|
27
|
-
|
29
|
+
|
30
|
+
MAX_PER_PAGE = 250
|
31
|
+
|
28
32
|
include Voog::API::Articles
|
29
33
|
include Voog::API::Assets
|
30
34
|
include Voog::API::Comments
|
@@ -45,12 +49,25 @@ module Voog
|
|
45
49
|
include Voog::API::Texts
|
46
50
|
include Voog::API::Tickets
|
47
51
|
|
48
|
-
attr_reader :api_token, :host
|
52
|
+
attr_reader :api_token, :host, :auto_paginate, :per_page
|
49
53
|
|
54
|
+
# Initialize Voog API client.
|
55
|
+
#
|
56
|
+
# @param host [String] a Voog site host.
|
57
|
+
# @param api_token [String] a Voog site API token.
|
58
|
+
# @option options [String] :protocol endpoint protocol ("http" or "https"). Defaults to "http".
|
59
|
+
# @option options [String] :auto_paginate enable auto pagination for list requests. Defaults to "false".
|
60
|
+
# @option options [String] :per_page set default "per_page" value for list requests. Defaults to "nil".
|
61
|
+
# @option options [Boolean] :raise_on_error interrupts program with error ("Faraday::Error") when request response code is between "400" and "600" (default is "false").
|
62
|
+
# @example Initialize client
|
63
|
+
# client = Voog::Client.new('example.com', 'afcf30182aecfc8155d390d7d4552d14', protocol: :http, raise_on_error: false)
|
50
64
|
def initialize(host = Voog.host, api_token = Voog.api_token, options = {})
|
51
65
|
@host = host
|
52
66
|
@api_token = api_token
|
53
67
|
@options = options
|
68
|
+
@protocol = options[:protocol].to_s.downcase == 'https' ? 'https' : 'http'
|
69
|
+
@auto_paginate = options.fetch(:auto_paginate, Voog.auto_paginate)
|
70
|
+
@per_page = options.fetch(:per_page, Voog.per_page)
|
54
71
|
@raise_on_error = options.fetch(:raise_on_error, true)
|
55
72
|
end
|
56
73
|
|
@@ -79,7 +96,7 @@ module Voog
|
|
79
96
|
end
|
80
97
|
|
81
98
|
def api_endpoint
|
82
|
-
"
|
99
|
+
"#{@protocol}://#{host}/admin/api"
|
83
100
|
end
|
84
101
|
|
85
102
|
def agent
|
@@ -92,13 +109,13 @@ module Voog
|
|
92
109
|
end
|
93
110
|
|
94
111
|
def multipart_agent
|
95
|
-
Faraday.new do |faraday|
|
112
|
+
@multipart_agent ||= Faraday.new do |faraday|
|
96
113
|
faraday.request :multipart
|
97
114
|
faraday.response :raise_error if @raise_on_error
|
98
115
|
faraday.adapter :net_http
|
99
116
|
|
100
|
-
faraday.headers[
|
101
|
-
faraday.headers[
|
117
|
+
faraday.headers[:x_api_token] = @api_token
|
118
|
+
faraday.headers[:user_agent] = 'Voog.rb Ruby wrapper'
|
102
119
|
end
|
103
120
|
end
|
104
121
|
|
@@ -109,7 +126,32 @@ module Voog
|
|
109
126
|
def parse_response(response)
|
110
127
|
JSON.parse(response).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
111
128
|
end
|
112
|
-
|
129
|
+
|
130
|
+
# Fetch all elements for requested API resource when {#auto_paginate} is turned on.
|
131
|
+
def paginate(url, options = {}, &block)
|
132
|
+
opts = options.dup
|
133
|
+
if @auto_paginate || @per_page
|
134
|
+
opts[:query][:per_page] ||= @per_page || (@auto_paginate ? MAX_PER_PAGE : nil)
|
135
|
+
end
|
136
|
+
|
137
|
+
data = request(:get, url, nil, opts)
|
138
|
+
|
139
|
+
if @auto_paginate
|
140
|
+
i = 0
|
141
|
+
while @last_response.rels[:next]
|
142
|
+
puts "Request: #{i += 1}"
|
143
|
+
@last_response = @last_response.rels[:next].get(headers: opts[:headers])
|
144
|
+
if block_given?
|
145
|
+
yield(data, @last_response)
|
146
|
+
else
|
147
|
+
data.concat(@last_response.data) if @last_response.data.is_a?(Array)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
data
|
153
|
+
end
|
154
|
+
|
113
155
|
private
|
114
156
|
|
115
157
|
def request(method, path, data, options = {})
|
@@ -117,7 +159,10 @@ module Voog
|
|
117
159
|
|
118
160
|
@last_response = response = multipart ? \
|
119
161
|
multipart_agent.post("#{api_endpoint}/#{path}", data) : \
|
120
|
-
agent.call(method, URI.encode(path.to_s), data, options)
|
162
|
+
agent.call(method, URI.encode(path.to_s), data, options.dup)
|
163
|
+
|
164
|
+
raise Voog::MovedPermanently.new(response.headers['location']) if response.status == 301
|
165
|
+
|
121
166
|
if multipart
|
122
167
|
parse_response(response.body)
|
123
168
|
else
|
data/lib/voog_api/version.rb
CHANGED
data/lib/voog_api.rb
CHANGED
@@ -54,7 +54,7 @@ describe Voog::API::MediaSets do
|
|
54
54
|
describe '#media_set_add_assets' do
|
55
55
|
|
56
56
|
before do
|
57
|
-
request_fixture(:
|
57
|
+
request_fixture(:post, 'media_sets/2/add_assets', request: {body: {asset_ids: [5, 8]}}, response: {body: '{"id": 2, "assets": [{"id": 1}, {"id": 5}, {"id": 8}]}'})
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'responds with correct assets set' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voog_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Priit Haamer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- lib/voog_api/api/texts.rb
|
134
134
|
- lib/voog_api/api/tickets.rb
|
135
135
|
- lib/voog_api/client.rb
|
136
|
+
- lib/voog_api/error.rb
|
136
137
|
- lib/voog_api/version.rb
|
137
138
|
- spec/fixtures/articles/article.json
|
138
139
|
- spec/fixtures/articles/articles.json
|
@@ -276,3 +277,4 @@ test_files:
|
|
276
277
|
- spec/voog_api/api/texts_spec.rb
|
277
278
|
- spec/voog_api/api/tickets_spec.rb
|
278
279
|
- spec/voog_spec.rb
|
280
|
+
has_rdoc:
|