reamaze_api 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95917a571145836ed5a44ce215ef1c1e5ba0e2ae
4
- data.tar.gz: 6cf9b15764ba698f2c94294c62ac5e22b8e03a26
3
+ metadata.gz: fad68d5cd62a6332ad24030733db0643e2307ceb
4
+ data.tar.gz: eb3694f56e5124e5d364fc9e9b36ef08b4b1a3e2
5
5
  SHA512:
6
- metadata.gz: e308a9c6d214a96bbb21a3c03384da0e87c595c4e259873f61dba9c1fcc22223af91b4f31caddfad8bd1705a685ca460e4915f84fc983a2ad5c8dff6a3d049d3
7
- data.tar.gz: 21db9b12a5a93fc75e7697f125a9773ae8137da624e783a1a21f269e70e772f726bd751cae840457a3043d81014980fc8034d5e5f5c5e189b931bcfc5f37cc2f
6
+ metadata.gz: 71a5d2f92737fcfbcfb9ebb6921f2ced6952c033527c0316420e377e2effab29a71ab7154d0bc86a15fe3c80e61807aab7573c7127e71d11b5ec29f4c53526f2
7
+ data.tar.gz: 617e7775821955deb3cc6b87c6fa722820032912ec296d3feb3b89e1974b922911824a61c84bd9197bfb176d4d4b1a11aec2e553f88ec7b243784b89663c42cd
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2016 Joshua Priddle <jpriddle@me.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person ob-
4
+ taining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without restric-
6
+ tion, including without limitation the rights to use, copy, modi-
7
+ fy, merge, publish, distribute, sublicense, and/or sell copies of
8
+ the Software, and to permit persons to whom the Software is fur-
9
+ nished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONIN-
17
+ FRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -66,6 +66,37 @@ client.messages.all
66
66
  client.messages.create
67
67
  ```
68
68
 
69
+ ## Pagination
70
+
71
+ Reamaze paginates responses that return multiple resources (ie: this library's
72
+ `#all` methods), and by default you receive page 1. You can control which page
73
+ to fetch by passing the `:page` parameter:
74
+
75
+ ```ruby
76
+ page1 = client.messages.all
77
+ page2 = client.messages.all(page: 2)
78
+ page3 = client.messages.all(page: 3)
79
+ ```
80
+
81
+ ## Auto-Pagination
82
+
83
+ Auto-pagination allows you to fetch _all_ results without having to manually
84
+ fetch each page. For example, with 3 pages of 30 conversations the following
85
+ would fetch all 90:
86
+
87
+ ```ruby
88
+ conversations = client.conversations.all(auto_paginate: true)
89
+ ```
90
+
91
+ **Beware of API rate limiting!** If you attempt to auto-paginate with a large
92
+ number of pages you may be rate limited by Reamaze. Make sure to apply filters
93
+ where necessary (eg: `all(auto_paginate: true, for: "me@example.com")`).
94
+
95
+ **Errors** If fetching any page is not successful the error will be returned
96
+ and no further pages will be fetched.
97
+
98
+ ## Customization and Middleware
99
+
69
100
  ReamazeAPI uses the [Faraday][] library for HTTP interactions, and by default,
70
101
  Net::HTTP. To configure a different HTTP adapter you can set
71
102
  `Faraday.default_adapter`:
@@ -141,3 +172,26 @@ push git commits and tags, and push the `.gem` file to
141
172
  Bug reports and pull requests are welcome on GitHub at
142
173
  https://github.com/itspriddle/reamaze_api.
143
174
 
175
+ ## License
176
+
177
+ Released under the MIT license:
178
+
179
+ Copyright (c) 2016 Joshua Priddle <jpriddle@me.com>
180
+
181
+ Permission is hereby granted, free of charge, to any person obtaining a copy
182
+ of this software and associated documentation files (the "Software"), to deal
183
+ in the Software without restriction, including without limitation the rights
184
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
185
+ copies of the Software, and to permit persons to whom the Software is
186
+ furnished to do so, subject to the following conditions:
187
+
188
+ The above copyright notice and this permission notice shall be included in all
189
+ copies or substantial portions of the Software.
190
+
191
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
194
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
195
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
196
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
197
+ SOFTWARE.
@@ -14,8 +14,9 @@ module ReamazeAPI
14
14
  # Returns a Hash.
15
15
  def all(params = {})
16
16
  params = Utils.symbolize_hash(params)
17
+ url = articles_path(params.delete(:topic))
17
18
 
18
- get articles_path(params.delete(:topic)), params
19
+ paginate url, :articles, params
19
20
  end
20
21
 
21
22
  # Public: Retrieves a specific KB article.
@@ -12,7 +12,7 @@ module ReamazeAPI
12
12
  #
13
13
  # Returns a Hash.
14
14
  def all(params = {})
15
- get "/channels", params
15
+ paginate "/channels", :channels, params
16
16
  end
17
17
 
18
18
  # Public: Retrieve a specific channel.
@@ -37,9 +37,9 @@ module ReamazeAPI
37
37
 
38
38
  # Public: Initialize a new Client instance.
39
39
  #
40
- # brand: Brand name (subdomain from your Reamaze URL)
41
- # login: Reamaze login
42
- # token: Reamaze API token
40
+ # brand - Brand name (subdomain from your Reamaze URL)
41
+ # login - Reamaze login
42
+ # token - Reamaze API token
43
43
  #
44
44
  # Yields a Faraday::Connection if a block is given.
45
45
  #
@@ -98,9 +98,9 @@ module ReamazeAPI
98
98
 
99
99
  # Private: Submits an HTTP request to the upstream API.
100
100
  #
101
- # method: HTTP method (eg: :get, :post)
102
- # path: API path (without `/api/v1` prefix, eg: "/messages")
103
- # params: Hash of parameters to send with the request (default: {})
101
+ # method - HTTP method (eg: :get, :post)
102
+ # path - API path (without `/api/v1` prefix, eg: "/messages")
103
+ # params - Hash of parameters to send with the request (default: {})
104
104
  #
105
105
  # Raises a ReamazeAPI::Error for any HTTP response code 400-599 unless
106
106
  # `ReamazeAPI.config.exceptions` is false.
@@ -118,11 +118,50 @@ module ReamazeAPI
118
118
  Utils.error_hash(e)
119
119
  end
120
120
 
121
+ # Private: Performs a GET request on the given path/resource. If results
122
+ # are more than one page, each additional page is fetched and added to the
123
+ # payload. If any page returns an error response, that response is
124
+ # immediately returned and no further requests are performed.
125
+ #
126
+ # NOTE: Beware of API rate limiting when using this method with large
127
+ # datasets.
128
+ #
129
+ # path - API path (without `/api/v1` prefix, eg: "/messages")
130
+ # resource - ReamazeAPI resource name (eg: `:messages`)
131
+ # params - Hash of parameters to send with the request (default: {})
132
+ #
133
+ # Returns a Hash.
134
+ def paginate(path, resource, params = {})
135
+ params = Utils.symbolize_hash(params)
136
+ auto_paginate = params.delete(:auto_paginate)
137
+ output = get(path, params)
138
+ page = params.fetch(:page, 1)
139
+ success = output[:success]
140
+ payload = output[:payload]
141
+ page_count = payload[:page_count]
142
+
143
+ if success && auto_paginate && page_count && page_count > page
144
+ more = paginate(path, resource, params.merge(
145
+ page: page.next,
146
+ auto_paginate: true
147
+ ))
148
+
149
+ if more[:success] && more[:payload]
150
+ payload[resource].concat more[:payload][resource]
151
+ else
152
+ output[:success] = false
153
+ output[:payload] = more[:payload]
154
+ end
155
+ end
156
+
157
+ output
158
+ end
159
+
121
160
  # Private: `get`, `put`, and `post` helper methods. These submit an HTTP
122
161
  # request to the upstream API.
123
162
  #
124
- # path: API path (without `/api/v1` prefix, eg: "/messages")
125
- # params: Hash of parameters to send with the request (default: {})
163
+ # path - API path (without `/api/v1` prefix, eg: "/messages")
164
+ # params - Hash of parameters to send with the request (default: {})
126
165
  #
127
166
  # Returns a Hash.
128
167
  HTTP_METHODS.each do |method|
@@ -12,7 +12,7 @@ module ReamazeAPI
12
12
  #
13
13
  # Returns a Hash.
14
14
  def all(params = {})
15
- get "/contacts", params
15
+ paginate "/contacts", :contacts, params
16
16
  end
17
17
 
18
18
  # Public: Create a new contact.
@@ -12,7 +12,7 @@ module ReamazeAPI
12
12
  #
13
13
  # Returns a Hash.
14
14
  def all(params = {})
15
- get "/conversations", params
15
+ paginate "/conversations", :conversations, params
16
16
  end
17
17
 
18
18
  # Public: Retrieve a specific conversation.
@@ -16,8 +16,9 @@ module ReamazeAPI
16
16
  # Returns a Hash.
17
17
  def all(params = {})
18
18
  params = Utils.symbolize_hash(params)
19
+ url = message_path(params.delete(:conversation_slug))
19
20
 
20
- get message_path(params.delete(:conversation_slug)), params
21
+ paginate url, :messages, params
21
22
  end
22
23
 
23
24
  # Public: Create a new message under the given conversation.
@@ -5,7 +5,7 @@ module ReamazeAPI
5
5
  extend Forwardable
6
6
 
7
7
  # Delegate HTTP actions back to the given Client.
8
- def_delegators :@client, *Client::HTTP_METHODS
8
+ def_delegators :@client, :paginate, *Client::HTTP_METHODS
9
9
 
10
10
  # Public: Initialize a new Resource instance. API resources should inherit
11
11
  # from this class.
@@ -1,3 +1,3 @@
1
1
  module ReamazeAPI
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
data/reamaze_api.gemspec CHANGED
@@ -5,6 +5,7 @@ Gem::Specification.new do |spec|
5
5
  spec.version = ReamazeAPI::VERSION
6
6
  spec.authors = ["Joshua Priddle"]
7
7
  spec.email = ["jpriddle@me.com"]
8
+ spec.license = "MIT"
8
9
 
9
10
  spec.summary = %q{Reamaze API client}
10
11
  spec.description = %q{Reamaze API client}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reamaze_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Priddle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -104,6 +104,7 @@ files:
104
104
  - ".gitignore"
105
105
  - ".travis.yml"
106
106
  - Gemfile
107
+ - LICENSE
107
108
  - README.md
108
109
  - Rakefile
109
110
  - bin/console
@@ -122,7 +123,8 @@ files:
122
123
  - lib/reamaze_api/version.rb
123
124
  - reamaze_api.gemspec
124
125
  homepage: https://github.com/itspriddle/reamaze_api
125
- licenses: []
126
+ licenses:
127
+ - MIT
126
128
  metadata: {}
127
129
  post_install_message:
128
130
  rdoc_options: []
@@ -145,4 +147,3 @@ signing_key:
145
147
  specification_version: 4
146
148
  summary: Reamaze API client
147
149
  test_files: []
148
- has_rdoc: