rtx-api 0.0.1
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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +8 -0
- data/Gemfile +25 -0
- data/LICENSE +21 -0
- data/README.md +226 -0
- data/Rakefile +20 -0
- data/bin/console +15 -0
- data/lib/rtx/api.rb +20 -0
- data/lib/rtx/api/client.rb +126 -0
- data/lib/rtx/api/collection.rb +154 -0
- data/lib/rtx/api/errors/authentication_error.rb +7 -0
- data/lib/rtx/api/errors/client_error.rb +7 -0
- data/lib/rtx/api/errors/invalid_resource_error.rb +7 -0
- data/lib/rtx/api/errors/request_error.rb +7 -0
- data/lib/rtx/api/resources.rb +9 -0
- data/lib/rtx/api/version.rb +5 -0
- data/rtx-api.gemspec +27 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6706402b07962d40d4a9ad82458179879a410bd4
|
4
|
+
data.tar.gz: 42ab40acc0a06dacb31ee419c8b662b47349b063
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1c682d7c181c8cee8df287b088c792d59390dd0128453ce912b040a961b9bc06a278c2291686d3f4f8d2cbdba63c7b0355290f498b4f7d988bd4ce4e8bf02dc
|
7
|
+
data.tar.gz: f525bf32165765445b2f0a6725eece66aca45cab92f0088c302836de4414631282dfdcd56b1f21a29f7d1ba15bc0f2469aee6ae2e5c7c384829bf155031ee5c9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
group :development do
|
5
|
+
gem 'pry'
|
6
|
+
gem 'httparty'
|
7
|
+
gem 'oj'
|
8
|
+
end
|
9
|
+
|
10
|
+
group :development, :test, :rake do
|
11
|
+
gem 'bundler'
|
12
|
+
end
|
13
|
+
|
14
|
+
group :rake do
|
15
|
+
gem 'rake'
|
16
|
+
end
|
17
|
+
|
18
|
+
group :test do
|
19
|
+
gem 'minitest'
|
20
|
+
gem 'minitest-spec-context'
|
21
|
+
gem 'minitest-reporters', require: 'minitest/reporters'
|
22
|
+
gem 'mocha', require: 'mocha/setup'
|
23
|
+
gem 'vcr', require: false
|
24
|
+
gem 'webmock', require: false
|
25
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ReviewTrackers
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
# rtx-api-client-ruby [](https://travis-ci.org/reviewtrackers/rtx-api-client-ruby)
|
2
|
+
|
3
|
+
Ruby Client for the RTX API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rtx-api'
|
10
|
+
|
11
|
+
Then bundle install to retrieve the latest gem:
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
## Environment Variables
|
16
|
+
|
17
|
+
By adding `RTX_USER_EMAIL` and `RTX_USER_PASSWORD` to your environment variables, the gem will be able to load them during initialization of the Ruby client.
|
18
|
+
|
19
|
+
If you using a different url for for the RTX API, you can set the `RTX_API_URL` environment variable to use the appropriate one.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The client is lazily loaded, which means it will not authenticate until you make your initial request. Once the request is performed, we'll store the information for future requests. You will want to use the same instance of the client to perform all of your requests.
|
24
|
+
|
25
|
+
### Initialization of the Client
|
26
|
+
|
27
|
+
Based on if you have your environment variables for RTX_API_EMAIL and RTX_API_PASSWORD configured
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
client = RTX::API::Client.new
|
31
|
+
```
|
32
|
+
|
33
|
+
If you do not have your environment variables setup
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
client = RTX::API::Client.new("demo_account@yourdomain.com", "your_password")
|
37
|
+
```
|
38
|
+
|
39
|
+
### Logging Out
|
40
|
+
|
41
|
+
The client is authenticated once requests are made. After you are done with all of your requests, you can log out of your session. You should do this at the end regardless if you know you have made a request. It will not logout unless a token has been received.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
client.logout
|
45
|
+
```
|
46
|
+
|
47
|
+
### Get Reviews
|
48
|
+
|
49
|
+
Using the initialized client, you can request reviews
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# Creates the query
|
53
|
+
reviews = client.reviews
|
54
|
+
|
55
|
+
# Performs the request
|
56
|
+
reviews.data
|
57
|
+
|
58
|
+
# Metadata for the response can be retrieved
|
59
|
+
reviews.meta
|
60
|
+
```
|
61
|
+
|
62
|
+
If you want to add additional parameters to the query
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# Creates the query
|
66
|
+
reviews = client.reviews(location_id: "56f2b386a97985e5a81e72f9", source_id: "56f2b386a97985e5a81e9td2")
|
67
|
+
|
68
|
+
# Performs the request
|
69
|
+
reviews.data
|
70
|
+
```
|
71
|
+
|
72
|
+
### Get Notes
|
73
|
+
|
74
|
+
Using the initialized client, you can request notes
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# Creates the query
|
78
|
+
notes = client.notes
|
79
|
+
|
80
|
+
# Performs the request
|
81
|
+
notes.data
|
82
|
+
|
83
|
+
# Metadata for the response can be retrieved
|
84
|
+
notes.meta
|
85
|
+
```
|
86
|
+
|
87
|
+
If you want to add additional parameters to the query
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Creates the query
|
91
|
+
notes = client.notes(review_id: "56f2b386a97985e5a81e72f9")
|
92
|
+
|
93
|
+
# Performs the request
|
94
|
+
notes.data
|
95
|
+
```
|
96
|
+
|
97
|
+
## Pagination
|
98
|
+
|
99
|
+
If you want to add custom paging on the collection. The `per_page` and `page` method can be chained on to the query.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# Creates the query
|
103
|
+
reviews = client.reviews.per_page(50)
|
104
|
+
|
105
|
+
# Performs the request
|
106
|
+
reviews.data
|
107
|
+
```
|
108
|
+
|
109
|
+
If you want to get the next set of the collection
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# Creates the query
|
113
|
+
reviews = client.reviews.per_page(50)
|
114
|
+
|
115
|
+
# Performs the request (For first 50)
|
116
|
+
reviews.data
|
117
|
+
|
118
|
+
# Next 50 of the collection (If available)
|
119
|
+
reviews.next.data
|
120
|
+
```
|
121
|
+
|
122
|
+
If you want to get a specific page from the collection
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# Creates the query
|
126
|
+
reviews = client.reviews.per_page(50).page(10)
|
127
|
+
|
128
|
+
# Performs the request for 50 reviews from page 10
|
129
|
+
reviews.data
|
130
|
+
```
|
131
|
+
|
132
|
+
If you want to get the previous set of the collection
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
# Creates the query
|
136
|
+
reviews = client.reviews.per_page(50).page(10)
|
137
|
+
|
138
|
+
# Performs the request for 50 reviews from page 10
|
139
|
+
reviews.data
|
140
|
+
|
141
|
+
# Previous 50 of the collection from page 9
|
142
|
+
reviews.prev.data
|
143
|
+
```
|
144
|
+
|
145
|
+
If you want to know if there is a next page in the collection
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
# Creates the query
|
149
|
+
reviews = client.reviews.per_page(50).page(10)
|
150
|
+
|
151
|
+
# Performs the request for 50 reviews from page 10
|
152
|
+
reviews.data
|
153
|
+
|
154
|
+
# Returns true if there is another page in the collection
|
155
|
+
reviews.has_next?
|
156
|
+
```
|
157
|
+
|
158
|
+
If you want to know if there is a previous page in the collection
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
# Creates the query
|
162
|
+
reviews = client.reviews.per_page(50).page(2)
|
163
|
+
|
164
|
+
# Performs the request for 50 reviews from page 10
|
165
|
+
reviews.data
|
166
|
+
|
167
|
+
# Returns true if there is another page before it in the collection
|
168
|
+
reviews.has_previous?
|
169
|
+
```
|
170
|
+
|
171
|
+
### Iterate all pages of a collection of resources
|
172
|
+
|
173
|
+
If you want to retrieve all pages of a collection starting at a specific page, you can pass a block into `all_pages`
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
# Creates the query
|
177
|
+
reviews = client.reviews.per_page(50)
|
178
|
+
|
179
|
+
# Performs the request for each page (at 50 per page) starting at page 2 and ending at the last page returning the page in the block
|
180
|
+
pages = []
|
181
|
+
reviews.all_pages(2) do |page|
|
182
|
+
pages << page
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
186
|
+
### Iterate all resources spanning all pages
|
187
|
+
|
188
|
+
If you want to retrieve all resources of a collection starting at a specific page, you can pass a block into `all_resources`
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
# Creates the query
|
192
|
+
reviews = client.reviews.per_page(50)
|
193
|
+
|
194
|
+
# Performs the request for each page starting at page 2 and ending at the last page returning the resource in the block
|
195
|
+
resources = []
|
196
|
+
reviews.all_resources(2) do |resource|
|
197
|
+
resources << resource
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
### Creating a Resource
|
202
|
+
|
203
|
+
The `create!` method will perform the create and return the new object
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
# Create a note
|
207
|
+
note = client.notes.create!(review_id: "56f2ad01565d61bb2a606329", location_id: "566c91337536e826ec8ebfa0", content: "This is my note!")
|
208
|
+
```
|
209
|
+
|
210
|
+
### Updating a Resource
|
211
|
+
|
212
|
+
The `update!` method will perform the update and return the new object
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# Update a review
|
216
|
+
review = client.reviews.update!(id: "56f2ad01565d61bb2a606329", summary: "Updated Summary Text")
|
217
|
+
```
|
218
|
+
|
219
|
+
### Deleting a Resource
|
220
|
+
|
221
|
+
The `delete!` method will perform the update and return true on success
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
# Delete a note
|
225
|
+
success = client.notes.delete!(id: "56f2bb805ee5d0bb2a9c6633")
|
226
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'bundler/gem_helper'
|
4
|
+
|
5
|
+
# environment
|
6
|
+
ENV['RACK_ENV'] ||= 'development'
|
7
|
+
ENV['RTX_API_URL'] ||= 'http://localhost:5000'
|
8
|
+
|
9
|
+
# load path
|
10
|
+
lib_path = File.expand_path('../lib', __FILE__)
|
11
|
+
($:.unshift lib_path) unless ($:.include? lib_path)
|
12
|
+
|
13
|
+
Rake::TestTask.new do |t|
|
14
|
+
t.libs << "spec"
|
15
|
+
t.pattern = "spec/**/*_spec.rb"
|
16
|
+
end
|
17
|
+
|
18
|
+
# tasks
|
19
|
+
task :default => [:test]
|
20
|
+
Bundler::GemHelper.install_tasks
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
ENV['RACK_ENV'] ||= 'development'
|
3
|
+
# load path
|
4
|
+
lib_path = File.expand_path('../../lib', __FILE__)
|
5
|
+
($:.unshift lib_path) unless ($:.include? lib_path)
|
6
|
+
|
7
|
+
# require farm
|
8
|
+
require 'bundler'
|
9
|
+
Bundler.require(:default, ENV['RACK_ENV'])
|
10
|
+
|
11
|
+
# fire up the ftl drive
|
12
|
+
Pry.config.prompt_name = "rtx-api-client-ruby@#{ENV['RACK_ENV']}:pry"
|
13
|
+
Pry.start
|
14
|
+
|
15
|
+
# vim: set syntax=ruby:
|
data/lib/rtx/api.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "oj"
|
2
|
+
require "httparty"
|
3
|
+
require "rtx/api/version"
|
4
|
+
|
5
|
+
# client
|
6
|
+
require "rtx/api/client"
|
7
|
+
|
8
|
+
# resources
|
9
|
+
require "rtx/api/resources"
|
10
|
+
require "rtx/api/collection"
|
11
|
+
|
12
|
+
# errors
|
13
|
+
require "rtx/api/errors/client_error"
|
14
|
+
require "rtx/api/errors/authentication_error"
|
15
|
+
require "rtx/api/errors/request_error"
|
16
|
+
require "rtx/api/errors/invalid_resource_error"
|
17
|
+
|
18
|
+
module RTX
|
19
|
+
module API; end
|
20
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module RTX
|
2
|
+
module API
|
3
|
+
class Client
|
4
|
+
include HTTParty
|
5
|
+
attr_accessor :email, :password, :token, :expires, :account_id, :profile_id
|
6
|
+
|
7
|
+
def initialize(email = ENV["RTX_USER_EMAIL"], password = ENV["RTX_USER_PASSWORD"])
|
8
|
+
@email = email
|
9
|
+
@password = password
|
10
|
+
@token = nil
|
11
|
+
@expires = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def authenticated?
|
15
|
+
!token.nil? && !expires.nil? && !account_id.nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
def authenticate
|
19
|
+
request = self.class.post("#{rtx_api_url}/auth", headers: default_headers, basic_auth: {username: email, password: password})
|
20
|
+
response = Oj.load(request.body)
|
21
|
+
if request.code != 201
|
22
|
+
raise API::Errors::AuthenticationError.new("Authentication Login Error: #{response}")
|
23
|
+
end
|
24
|
+
@token = response["token"]
|
25
|
+
@expires = response["expires_at"]
|
26
|
+
@account_id = response["account_id"]
|
27
|
+
@profile_id = response["profile_id"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def logout
|
31
|
+
if token
|
32
|
+
request = self.class.delete("#{rtx_api_url}/auth", options(:delete))
|
33
|
+
if request.code != 204
|
34
|
+
raise API::Errors::AuthenticationError.new("Authentication Logout Error: #{response}")
|
35
|
+
end
|
36
|
+
@token, @expires, @account_id, @profile_id = nil, nil, nil, nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(method, *args, &block)
|
41
|
+
allowed_resources = API::Resources.allowed_resources
|
42
|
+
method_name = method.to_s
|
43
|
+
|
44
|
+
if allowed_resources.include? method_name
|
45
|
+
attrs = {}
|
46
|
+
if args.size > 0
|
47
|
+
attrs = args.last.is_a?(Hash) ? args.pop : {}
|
48
|
+
end
|
49
|
+
RTX::API::Collection.new(self, method_name, attrs)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def get(resource_name, attrs = {})
|
54
|
+
resource_exists?(resource_name)
|
55
|
+
request = self.class.get("#{rtx_api_url}/#{resource_name}", options(:get, attrs))
|
56
|
+
handle_request(request)
|
57
|
+
end
|
58
|
+
|
59
|
+
def post(resource_name, attrs = {})
|
60
|
+
resource_exists?(resource_name)
|
61
|
+
request = self.class.post("#{rtx_api_url}/#{resource_name}", options(:post, attrs))
|
62
|
+
handle_request(request)
|
63
|
+
end
|
64
|
+
|
65
|
+
def put(resource_name, resource_id, attrs = {})
|
66
|
+
resource_exists?(resource_name)
|
67
|
+
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
|
68
|
+
request = self.class.put("#{rtx_api_url}/#{resource_name}/#{resource_id}", options(:put, attrs))
|
69
|
+
handle_request(request)
|
70
|
+
end
|
71
|
+
|
72
|
+
def delete(resource_name, resource_id)
|
73
|
+
resource_exists?(resource_name)
|
74
|
+
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
|
75
|
+
request = self.class.delete("#{rtx_api_url}/#{resource_name}/#{resource_id}", options(:delete))
|
76
|
+
handle_request(request)
|
77
|
+
end
|
78
|
+
|
79
|
+
def handle_request(request)
|
80
|
+
if !request.success?
|
81
|
+
raise API::Errors::RequestError.new("#{response}")
|
82
|
+
end
|
83
|
+
|
84
|
+
if request.parsed_response.nil?
|
85
|
+
return true
|
86
|
+
end
|
87
|
+
|
88
|
+
Oj.load(request.body)
|
89
|
+
end
|
90
|
+
|
91
|
+
def resource_exists?(resource_name)
|
92
|
+
allowed_resources = API::Resources.allowed_resources
|
93
|
+
if !allowed_resources.include? resource_name
|
94
|
+
raise API::Errors::InvalidResourceError.new("The resource provided (#{resource_name}) is not allowed")
|
95
|
+
end
|
96
|
+
|
97
|
+
true
|
98
|
+
end
|
99
|
+
|
100
|
+
protected
|
101
|
+
|
102
|
+
def default_headers
|
103
|
+
{
|
104
|
+
'Content-Type' => "application/vnd.rtx.v2.hal+json",
|
105
|
+
'Accept' => "application/vnd.rtx.v2.hal+json",
|
106
|
+
'User-Agent' => "rtx-api-client-ruby/#{API::VERSION}"
|
107
|
+
}.freeze
|
108
|
+
end
|
109
|
+
|
110
|
+
def options(action, attrs = {})
|
111
|
+
default_params = {account_id: account_id}
|
112
|
+
default_params[:profile_id] = profile_id if write_request?(action)
|
113
|
+
query_params = default_params.merge(attrs)
|
114
|
+
{query: query_params, headers: default_headers, basic_auth: {username: email, password: token}}
|
115
|
+
end
|
116
|
+
|
117
|
+
def rtx_api_url
|
118
|
+
ENV["RTX_API_URL"] || "https://api-gateway.reviewtrackers.com"
|
119
|
+
end
|
120
|
+
|
121
|
+
def write_request?(action)
|
122
|
+
(action == :post || action == :put)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module RTX
|
2
|
+
module API
|
3
|
+
class Collection
|
4
|
+
attr_accessor :client, :resource_name, :response, :options
|
5
|
+
|
6
|
+
def initialize(client, resource_name, attrs = {})
|
7
|
+
@client = client
|
8
|
+
@resource_name = resource_name
|
9
|
+
@options = symbolize_hash(attrs)
|
10
|
+
@response = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# Creates a new resource via POST and returns it on success
|
14
|
+
def create!(attrs = {})
|
15
|
+
client.authenticate if !client.authenticated?
|
16
|
+
post(symbolize_hash(attrs))
|
17
|
+
end
|
18
|
+
|
19
|
+
# Updates a resource via PUT and returns it on success
|
20
|
+
def update!(attrs = {})
|
21
|
+
client.authenticate if !client.authenticated?
|
22
|
+
put(symbolize_hash(attrs))
|
23
|
+
end
|
24
|
+
|
25
|
+
# Deletes a resource via DELETE and returns true on success
|
26
|
+
def delete!(attrs = {})
|
27
|
+
client.authenticate if !client.authenticated?
|
28
|
+
delete(symbolize_hash(attrs))
|
29
|
+
end
|
30
|
+
|
31
|
+
# Chainable method that allows you to set the per page number of the collection for your request
|
32
|
+
def per_page(num)
|
33
|
+
clear if !num.nil?
|
34
|
+
@options[:per_page] = num
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Chainable method that allows you to set the page number of the collection for your request
|
39
|
+
def page(num)
|
40
|
+
clear if !num.nil?
|
41
|
+
@options[:page] = num
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns all data associated with the existing response
|
46
|
+
def data
|
47
|
+
client.authenticate if !client.authenticated?
|
48
|
+
get if !has_response?
|
49
|
+
response["_embedded"][resource_name]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the metadata about the current response
|
53
|
+
def meta
|
54
|
+
client.authenticate if !client.authenticated?
|
55
|
+
get if !has_response?
|
56
|
+
response.reject {|key,_| key == "_embedded"}
|
57
|
+
end
|
58
|
+
|
59
|
+
# For moving forward one page with the collection
|
60
|
+
def next
|
61
|
+
if has_next?
|
62
|
+
page(options[:page] += 1)
|
63
|
+
end
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
# For moving backward one page with the collection
|
68
|
+
def prev
|
69
|
+
if has_previous?
|
70
|
+
page(options[:page] -= 1)
|
71
|
+
end
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
# For moving to the last page of the collection
|
76
|
+
def last
|
77
|
+
page(meta["_total_pages"])
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
# For moving to the first page of the collection
|
82
|
+
def first
|
83
|
+
page(1)
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
# Responds true if the collection has another page ahead of it
|
88
|
+
def has_next?
|
89
|
+
current_page < meta["_total_pages"]
|
90
|
+
end
|
91
|
+
|
92
|
+
# Responds true if the collection has a previous one
|
93
|
+
def has_previous?
|
94
|
+
current_page > 1
|
95
|
+
end
|
96
|
+
|
97
|
+
# Allows you to loop through all of the pages and retrieve the records
|
98
|
+
def all_pages(initial_page = 1, &block)
|
99
|
+
page(initial_page)
|
100
|
+
|
101
|
+
pages = (current_page..meta["_total_pages"]).to_a
|
102
|
+
pages.each do |page_num|
|
103
|
+
block.call(page(page_num).data)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Allows you to loop through all of the resources within the pages specified and retrieve the records
|
108
|
+
def all_resources(initial_page = 1, &block)
|
109
|
+
all_pages(initial_page) do |page|
|
110
|
+
page.each do |resource|
|
111
|
+
block.call(resource)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
protected
|
117
|
+
|
118
|
+
def clear
|
119
|
+
@response = {}
|
120
|
+
end
|
121
|
+
|
122
|
+
def current_page
|
123
|
+
@options[:page] = options[:page].nil? ? meta["_page"] : options[:page]
|
124
|
+
options[:page]
|
125
|
+
end
|
126
|
+
|
127
|
+
def get
|
128
|
+
@response = client.get(resource_name, options)
|
129
|
+
end
|
130
|
+
|
131
|
+
def post(attrs = {})
|
132
|
+
client.post(resource_name, attrs)
|
133
|
+
end
|
134
|
+
|
135
|
+
def put(attrs = {})
|
136
|
+
resource_id = attrs[:id]
|
137
|
+
client.put(resource_name, resource_id, attrs)
|
138
|
+
end
|
139
|
+
|
140
|
+
def delete(attrs = {})
|
141
|
+
resource_id = attrs[:id]
|
142
|
+
client.delete(resource_name, resource_id)
|
143
|
+
end
|
144
|
+
|
145
|
+
def has_response?
|
146
|
+
response != {}
|
147
|
+
end
|
148
|
+
|
149
|
+
def symbolize_hash(hash)
|
150
|
+
Hash[hash.map{|(key,value)| [key.to_sym,value]}]
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/rtx-api.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rtx/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rtx-api"
|
8
|
+
spec.version = RTX::API::VERSION
|
9
|
+
spec.authors = ["ReviewTrackers Engineering"]
|
10
|
+
spec.email = ["engineering@reviewtrackers.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby Client for the Review Trackers RTX API.}
|
13
|
+
spec.description = %q{A Ruby Client for the RTX API provided by Review Trackers.}
|
14
|
+
spec.homepage = "https://github.com/reviewtrackers/rtx-api-client-ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.2222222222222222222222'
|
23
|
+
|
24
|
+
# add dependencies here. development dependencies should go into the Gemfile
|
25
|
+
spec.add_dependency 'httparty'
|
26
|
+
spec.add_dependency 'oj'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rtx-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ReviewTrackers Engineering
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oj
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A Ruby Client for the RTX API provided by Review Trackers.
|
42
|
+
email:
|
43
|
+
- engineering@reviewtrackers.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- lib/rtx/api.rb
|
56
|
+
- lib/rtx/api/client.rb
|
57
|
+
- lib/rtx/api/collection.rb
|
58
|
+
- lib/rtx/api/errors/authentication_error.rb
|
59
|
+
- lib/rtx/api/errors/client_error.rb
|
60
|
+
- lib/rtx/api/errors/invalid_resource_error.rb
|
61
|
+
- lib/rtx/api/errors/request_error.rb
|
62
|
+
- lib/rtx/api/resources.rb
|
63
|
+
- lib/rtx/api/version.rb
|
64
|
+
- rtx-api.gemspec
|
65
|
+
homepage: https://github.com/reviewtrackers/rtx-api-client-ruby
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.2222222222222222222222'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.5.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Ruby Client for the Review Trackers RTX API.
|
89
|
+
test_files: []
|