amara 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +2 -2
- data/README.md +8 -0
- data/lib/amara.rb +2 -1
- data/lib/amara/api.rb +56 -6
- data/lib/amara/client.rb +5 -0
- data/lib/amara/configuration.rb +7 -3
- data/lib/amara/errors.rb +16 -0
- data/lib/amara/path.rb +14 -0
- data/lib/amara/response.rb +4 -15
- data/lib/amara/version.rb +1 -1
- data/test/api_test.rb +46 -1
- data/test/client_test.rb +54 -0
- data/test/path_test.rb +24 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 144c53baf060dded67c646c85f51a52dac9582f4
|
4
|
+
data.tar.gz: 727735380c694fea5311ee26a584b0356d9bc1a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b5f3a15c7489353a09d7d7858e4d420ef54ed24a7375b5b4267b8613772f03fee6bd8ca8c6747f5478b0911a2ff1b1173f4f81701bda9ab3171eb15dc83a2bc
|
7
|
+
data.tar.gz: 7e9432906468f09cd4d4a2e15699b8b0a716cc3f26621081b4b09c00f01866a78989645176c6bba04c4af28bd4c3f232e3543d5206f1b14508cdeedf7eb35b50
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -104,6 +104,14 @@ new_team = amara.teams.create(
|
|
104
104
|
|
105
105
|
```
|
106
106
|
|
107
|
+
In the event of an error from the Amara service, the error will be wrapped in one of the following exception clases.
|
108
|
+
```ruby
|
109
|
+
raise Amara::NotFoundError # 404 error
|
110
|
+
raise Amara::ClientError # All other 400 errors
|
111
|
+
raise Amara::ServerError # 500 errors
|
112
|
+
raise Amara::UnknownError # All other errors
|
113
|
+
```
|
114
|
+
|
107
115
|
## Contributing
|
108
116
|
|
109
117
|
1. Fork it
|
data/lib/amara.rb
CHANGED
@@ -4,6 +4,7 @@ require 'rubygems'
|
|
4
4
|
require 'active_support/all'
|
5
5
|
|
6
6
|
require 'amara/version'
|
7
|
+
require 'amara/errors'
|
7
8
|
require 'amara/configuration'
|
8
9
|
require 'amara/connection'
|
9
10
|
require 'amara/response'
|
@@ -18,6 +19,7 @@ require 'amara/videos/languages'
|
|
18
19
|
require 'amara/videos/languages/subtitles'
|
19
20
|
require 'amara/videos/urls'
|
20
21
|
require 'amara/activity'
|
22
|
+
require 'amara/path'
|
21
23
|
require 'amara/client'
|
22
24
|
|
23
25
|
module Amara
|
@@ -42,5 +44,4 @@ module Amara
|
|
42
44
|
:managers => 'Managers and admins',
|
43
45
|
:admins => 'Admins only'
|
44
46
|
}
|
45
|
-
|
46
47
|
end
|
data/lib/amara/api.rb
CHANGED
@@ -36,19 +36,42 @@ module Amara
|
|
36
36
|
raise ArgumentError, "whoops, that isn't a valid http method: #{method}"
|
37
37
|
end
|
38
38
|
|
39
|
-
|
40
|
-
|
39
|
+
options = current_options.merge(params[:options] || {})
|
40
|
+
request_params = params.except(:options)
|
41
|
+
conn = connection(options)
|
42
|
+
request_path = (conn.path_prefix + '/' + path + '/').gsub(/\/+/, '/')
|
41
43
|
|
42
44
|
response = conn.send(method) do |request|
|
43
45
|
case method.to_sym
|
44
46
|
when :get, :delete
|
45
|
-
request.url(request_path,
|
47
|
+
request.url(request_path, request_params)
|
46
48
|
when :post, :put
|
47
49
|
request.path = request_path
|
48
|
-
request.body =
|
50
|
+
request.body = request_params[:data] ? request_params[:data].to_json : nil
|
49
51
|
end
|
50
52
|
end
|
51
|
-
|
53
|
+
|
54
|
+
amara_response = Amara::Response.new(response, { api: self, method: method, path: path, params: params } )
|
55
|
+
check_for_error(response) if options[:raise_errors]
|
56
|
+
amara_response
|
57
|
+
end
|
58
|
+
|
59
|
+
def check_for_error(response)
|
60
|
+
status_code_type = response.status.to_s[0]
|
61
|
+
case status_code_type
|
62
|
+
when "2"
|
63
|
+
# puts "all is well, status: #{response.status}"
|
64
|
+
when "4"
|
65
|
+
if response.status == 404
|
66
|
+
raise NotFoundError.new("Resource not found", response)
|
67
|
+
else
|
68
|
+
raise ClientError.new("Whoops, error back from Amara: #{response.status}", response)
|
69
|
+
end
|
70
|
+
when "5"
|
71
|
+
raise ServerError.new("Whoops, error back from Amara: #{response.status}", response)
|
72
|
+
else
|
73
|
+
raise UnknownError.new("Unrecongized status code: #{response.status}", response)
|
74
|
+
end
|
52
75
|
end
|
53
76
|
|
54
77
|
def base_path
|
@@ -64,7 +87,7 @@ module Amara
|
|
64
87
|
end
|
65
88
|
|
66
89
|
def paginate(params={})
|
67
|
-
|
90
|
+
params.reverse_merge(limit: 20, offset: 0)
|
68
91
|
end
|
69
92
|
|
70
93
|
def list(params={})
|
@@ -72,26 +95,46 @@ module Amara
|
|
72
95
|
request(:get, base_path, paginate(params))
|
73
96
|
end
|
74
97
|
|
98
|
+
def list!(params={})
|
99
|
+
list(force_raise_errors(params))
|
100
|
+
end
|
101
|
+
|
75
102
|
def get(params={})
|
76
103
|
self.current_options = current_options.merge(args_to_options(params))
|
77
104
|
request(:get, base_path)
|
78
105
|
end
|
79
106
|
|
107
|
+
def get!(params={})
|
108
|
+
get(force_raise_errors(params))
|
109
|
+
end
|
110
|
+
|
80
111
|
def create(params={})
|
81
112
|
self.current_options = current_options.merge(args_to_options(params))
|
82
113
|
request(:post, base_path, {data: params})
|
83
114
|
end
|
84
115
|
|
116
|
+
def create!(params={})
|
117
|
+
create(force_raise_errors(params))
|
118
|
+
end
|
119
|
+
|
85
120
|
def update(params={})
|
86
121
|
self.current_options = current_options.merge(args_to_options(params))
|
87
122
|
request(:put, base_path, {data: params})
|
88
123
|
end
|
89
124
|
|
125
|
+
def update!(params={})
|
126
|
+
update(force_raise_errors(params))
|
127
|
+
end
|
128
|
+
|
90
129
|
def delete(params={})
|
91
130
|
self.current_options = current_options.merge(args_to_options(params))
|
92
131
|
request(:delete, base_path)
|
93
132
|
end
|
94
133
|
|
134
|
+
def delete!(params={})
|
135
|
+
delete(force_raise_errors(params))
|
136
|
+
end
|
137
|
+
|
95
138
|
def args_to_options(args)
|
96
139
|
params = if args.is_a?(String) || args.is_a?(Symbol)
|
97
140
|
{"#{self.class.name.demodulize.downcase.singularize}_id" => args}
|
@@ -99,5 +142,12 @@ module Amara
|
|
99
142
|
args
|
100
143
|
end
|
101
144
|
end
|
145
|
+
|
146
|
+
def force_raise_errors(params)
|
147
|
+
(params || {}).with_indifferent_access.tap do |p|
|
148
|
+
p[:options] = ActiveSupport::HashWithIndifferentAccess.new(p[:options])
|
149
|
+
p[:options][:raise_errors] = true
|
150
|
+
end
|
151
|
+
end
|
102
152
|
end
|
103
153
|
end
|
data/lib/amara/client.rb
CHANGED
@@ -27,5 +27,10 @@ module Amara
|
|
27
27
|
@users ||= ApiFactory.api('Amara::Users', self, params, &block)
|
28
28
|
end
|
29
29
|
|
30
|
+
def path(path, params={}, &block)
|
31
|
+
@path ||= ApiFactory.api('Amara::Path', self, params, &block)
|
32
|
+
@path.current_options['path'] = path
|
33
|
+
@path
|
34
|
+
end
|
30
35
|
end
|
31
36
|
end
|
data/lib/amara/configuration.rb
CHANGED
@@ -8,7 +8,8 @@ module Amara
|
|
8
8
|
:api_key,
|
9
9
|
:adapter,
|
10
10
|
:endpoint,
|
11
|
-
:user_agent
|
11
|
+
:user_agent,
|
12
|
+
:raise_errors
|
12
13
|
].freeze
|
13
14
|
|
14
15
|
# this you need to get from amara - go register!
|
@@ -24,6 +25,9 @@ module Amara
|
|
24
25
|
# The value sent in the http header for 'User-Agent' if none is set
|
25
26
|
DEFAULT_USER_AGENT = "Amara Ruby Gem #{Amara::VERSION}".freeze
|
26
27
|
|
28
|
+
# by default, raise errors instead of returning them
|
29
|
+
DEFAULT_RAISE_ERRORS = true
|
30
|
+
|
27
31
|
attr_accessor *VALID_OPTIONS_KEYS
|
28
32
|
|
29
33
|
# Convenience method to allow for global setting of configuration options
|
@@ -54,8 +58,8 @@ module Amara
|
|
54
58
|
self.adapter = DEFAULT_ADAPTER
|
55
59
|
self.endpoint = DEFAULT_ENDPOINT
|
56
60
|
self.user_agent = DEFAULT_USER_AGENT
|
61
|
+
self.raise_errors = DEFAULT_RAISE_ERRORS
|
57
62
|
self
|
58
63
|
end
|
59
|
-
|
60
64
|
end
|
61
|
-
end
|
65
|
+
end
|
data/lib/amara/errors.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Amara
|
2
|
+
|
3
|
+
class Error < ::StandardError
|
4
|
+
attr_accessor :response
|
5
|
+
|
6
|
+
def initialize(message = nil, response = nil)
|
7
|
+
super(message || "Amara Error")
|
8
|
+
self.response = response
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ClientError < Error; end
|
13
|
+
class NotFoundError < Error; end
|
14
|
+
class ServerError < Error; end
|
15
|
+
class UnknownError < Error; end
|
16
|
+
end
|
data/lib/amara/path.rb
ADDED
data/lib/amara/response.rb
CHANGED
@@ -17,26 +17,16 @@ module Amara
|
|
17
17
|
def initialize(response, request={})
|
18
18
|
@raw = response
|
19
19
|
@request = request
|
20
|
-
|
21
|
-
check_for_error(response)
|
22
|
-
end
|
23
|
-
|
24
|
-
def check_for_error(response)
|
25
|
-
status_code_type = response.status.to_s[0]
|
26
|
-
case status_code_type
|
27
|
-
when "2"
|
28
|
-
# puts "all is well, status: #{response.status}"
|
29
|
-
when "4", "5"
|
30
|
-
raise "Whoops, error back from Amara: #{response.status}"
|
31
|
-
else
|
32
|
-
raise "Unrecongized status code: #{response.status}"
|
33
|
-
end
|
34
20
|
end
|
35
21
|
|
36
22
|
def body
|
37
23
|
self.raw.body
|
38
24
|
end
|
39
25
|
|
26
|
+
def status
|
27
|
+
self.raw.status
|
28
|
+
end
|
29
|
+
|
40
30
|
def object
|
41
31
|
body.objects.nil? ? body : body.objects
|
42
32
|
end
|
@@ -109,6 +99,5 @@ module Amara
|
|
109
99
|
self.raw = new_response.raw
|
110
100
|
self
|
111
101
|
end
|
112
|
-
|
113
102
|
end
|
114
103
|
end
|
data/lib/amara/version.rb
CHANGED
data/test/api_test.rb
CHANGED
@@ -46,7 +46,7 @@ describe Amara::API do
|
|
46
46
|
response.limit.must_equal 2
|
47
47
|
response.size.must_equal 2
|
48
48
|
response.first.id.must_equal 1
|
49
|
-
|
49
|
+
|
50
50
|
second_response = '{"meta": {"limit": 2, "next": "/api2/partners/api/?limit=2&offset=4", "offset": 2, "previous":"/api2/partners/api/?limit=2&offset=0", "total_count": 5}, "objects": [{"id": 3}, {"id": 4}]}'
|
51
51
|
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=2').
|
52
52
|
to_return(body: second_response)
|
@@ -70,4 +70,49 @@ describe Amara::API do
|
|
70
70
|
response.wont_be :has_next_page?
|
71
71
|
end
|
72
72
|
|
73
|
+
it "will raise errors" do
|
74
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
75
|
+
to_return(status: 500, body: '{}')
|
76
|
+
|
77
|
+
api = Amara::API.new(raise_errors: true)
|
78
|
+
|
79
|
+
error = proc {
|
80
|
+
response = api.list(limit: 2)
|
81
|
+
}.must_raise Amara::ServerError
|
82
|
+
|
83
|
+
error.response.wont_be_nil
|
84
|
+
error.response.status.must_equal 500
|
85
|
+
error.message.must_equal 'Whoops, error back from Amara: 500'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "can return errors instead of raise them" do
|
89
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
90
|
+
to_return(status: 500, body: '{}')
|
91
|
+
|
92
|
+
api = Amara::API.new(raise_errors: false)
|
93
|
+
response = api.list(limit: 2)
|
94
|
+
response.status.must_equal 500
|
95
|
+
|
96
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
97
|
+
to_return(status: 500, body: '{}')
|
98
|
+
|
99
|
+
api = Amara::API.new(raise_errors: true)
|
100
|
+
response = api.list(limit: 2, options: { raise_errors: false } )
|
101
|
+
response.status.must_equal 500
|
102
|
+
end
|
103
|
+
|
104
|
+
it "can use bang methods to raise errors" do
|
105
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
106
|
+
to_return(status: 500, body: '{}')
|
107
|
+
|
108
|
+
api = Amara::API.new(raise_errors: false)
|
109
|
+
|
110
|
+
error = proc {
|
111
|
+
response = api.list!(limit: 2)
|
112
|
+
}.must_raise Amara::ServerError
|
113
|
+
|
114
|
+
error.response.wont_be_nil
|
115
|
+
error.response.status.must_equal 500
|
116
|
+
error.message.must_equal 'Whoops, error back from Amara: 500'
|
117
|
+
end
|
73
118
|
end
|
data/test/client_test.rb
CHANGED
@@ -19,4 +19,58 @@ describe Amara::Client do
|
|
19
19
|
amara.languages.wont_be_nil
|
20
20
|
end
|
21
21
|
|
22
|
+
it "returns a path api object" do
|
23
|
+
amara = Amara::Client.new
|
24
|
+
amara.path('some/path').wont_be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "throws no error on 200" do
|
28
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
29
|
+
to_return(body: nil, status: 200)
|
30
|
+
begin
|
31
|
+
Amara::API.new.list(limit: 2)
|
32
|
+
rescue
|
33
|
+
flunk "Client error should not be thrown!"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "throws a client error on 400s (except 404)" do
|
38
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
39
|
+
to_return(body: nil, status: 400)
|
40
|
+
error = proc {
|
41
|
+
Amara::API.new.list(limit: 2)
|
42
|
+
}.must_raise Amara::ClientError
|
43
|
+
|
44
|
+
error.response.status.must_equal 400
|
45
|
+
end
|
46
|
+
|
47
|
+
it "throws a not found error on 404" do
|
48
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
49
|
+
to_return(body: nil, status: 404)
|
50
|
+
error = proc {
|
51
|
+
Amara::API.new.list(limit: 2)
|
52
|
+
}.must_raise Amara::NotFoundError
|
53
|
+
|
54
|
+
error.response.status.must_equal 404
|
55
|
+
end
|
56
|
+
|
57
|
+
it "throws a server error on 500s" do
|
58
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
59
|
+
to_return(body: nil, status: 500)
|
60
|
+
error = proc {
|
61
|
+
Amara::API.new.list(limit: 2)
|
62
|
+
}.must_raise Amara::ServerError
|
63
|
+
|
64
|
+
error.response.status.must_equal 500
|
65
|
+
end
|
66
|
+
|
67
|
+
it "throws an unknown error on all other errors" do
|
68
|
+
stub_request(:get, 'https://www.amara.org/api2/partners/api/?limit=2&offset=0').
|
69
|
+
to_return(body: nil, status: 601)
|
70
|
+
error = proc {
|
71
|
+
Amara::API.new.list(limit: 2)
|
72
|
+
}.must_raise Amara::UnknownError
|
73
|
+
|
74
|
+
error.response.status.must_equal 601
|
75
|
+
end
|
22
76
|
end
|
data/test/path_test.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
4
|
+
|
5
|
+
describe Amara::Path do
|
6
|
+
|
7
|
+
it "returns a path api object" do
|
8
|
+
amara = Amara::Client.new
|
9
|
+
assert_equal amara.path('some/path').class, Amara::Path
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises an exception if no path is passed" do
|
13
|
+
amara = Amara::Client.new
|
14
|
+
assert_raises ArgumentError do
|
15
|
+
amara.path.wont_be_nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "sets the custom path" do
|
20
|
+
amara = Amara::Client.new
|
21
|
+
path = amara.path('some/path')
|
22
|
+
assert_equal 'some/path', path.current_options['path']
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amara
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kuklewicz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -172,8 +172,10 @@ files:
|
|
172
172
|
- lib/amara/client.rb
|
173
173
|
- lib/amara/configuration.rb
|
174
174
|
- lib/amara/connection.rb
|
175
|
+
- lib/amara/errors.rb
|
175
176
|
- lib/amara/languages.rb
|
176
177
|
- lib/amara/message.rb
|
178
|
+
- lib/amara/path.rb
|
177
179
|
- lib/amara/response.rb
|
178
180
|
- lib/amara/teams.rb
|
179
181
|
- lib/amara/teams/applications.rb
|
@@ -189,6 +191,7 @@ files:
|
|
189
191
|
- lib/amara/videos/urls.rb
|
190
192
|
- test/api_test.rb
|
191
193
|
- test/client_test.rb
|
194
|
+
- test/path_test.rb
|
192
195
|
- test/teams/projects_test.rb
|
193
196
|
- test/teams_test.rb
|
194
197
|
- test/test_helper.rb
|
@@ -219,6 +222,7 @@ summary: Works with API v1.2, http://amara.readthedocs.org/en/latest/api.html
|
|
219
222
|
test_files:
|
220
223
|
- test/api_test.rb
|
221
224
|
- test/client_test.rb
|
225
|
+
- test/path_test.rb
|
222
226
|
- test/teams/projects_test.rb
|
223
227
|
- test/teams_test.rb
|
224
228
|
- test/test_helper.rb
|