easy-api 0.2.1 → 0.2.3
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/CHANGELOG.md +9 -1
- data/README.md +8 -1
- data/lib/easy/api/block_wrapper.rb +8 -2
- data/lib/easy/api/error.rb +2 -2
- data/lib/easy/api/result.rb +4 -0
- data/lib/easy/api/version.rb +1 -1
- data/spec/lib/easy/api/customers_controller_spec.rb +2 -2
- data/spec/lib/easy/api/error_spec.rb +14 -0
- data/spec/lib/easy/api/result_spec.rb +34 -0
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e7a7b501b68e2d27078a346db4d2e9ccd369c227
|
|
4
|
+
data.tar.gz: 4c9343aac20833e366a9e62d213457f9c1e76e47
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 24a3a204a78387f1571f66f832c4b343b5c1b5786b3fac965449b514da09bab49ae71a9bb0d35e1c7cf56e9ebb12d2663be60545501e34bcc0a95d983a7a2f85
|
|
7
|
+
data.tar.gz: e98d3bc861219153d22ef088100079c1346e75dd7273b14ba2a5a12a9abf6de6bdb5e6971d4f53df7a1a9673e8f3d10431ec3fbdb8891799ee72ecfdc7e85842
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# EasyAPI 0.2.3 (August 28, 2015) #
|
|
2
|
+
|
|
3
|
+
* Allow Easy::Api:Error.codes and Easy::Api:Error.messages to be added to
|
|
4
|
+
|
|
5
|
+
# EasyAPI 0.2.2 (August 15, 2014) #
|
|
6
|
+
|
|
7
|
+
* Reintroduce default formatting options when rendering xml
|
|
8
|
+
|
|
1
9
|
# EasyAPI 0.2.1 (July 30, 2014) #
|
|
2
10
|
|
|
3
11
|
* Auto-load without the need for a require statement
|
|
@@ -14,4 +22,4 @@
|
|
|
14
22
|
# EasyAPI 0.1.2 (March 11, 2013) #
|
|
15
23
|
|
|
16
24
|
* Genericize error messages
|
|
17
|
-
* Add support for Ruby 1.8.7
|
|
25
|
+
* Add support for Ruby 1.8.7
|
data/README.md
CHANGED
|
@@ -36,6 +36,11 @@ If you want to override the default error message, pass in a custom message, e.g
|
|
|
36
36
|
|
|
37
37
|
Easy::Api::Error objects have a code (e.g. 404) and a message (e.g. 'Resource not found')
|
|
38
38
|
|
|
39
|
+
You can also define you own error types. Add the following to `initializers/easy_api.rb`
|
|
40
|
+
|
|
41
|
+
Easy::Api::Error.codes[:rate_limited] = 429
|
|
42
|
+
Easy::Api::Error.messages[:rate_limited] = 'Rate limited'
|
|
43
|
+
|
|
39
44
|
### Using Easy::Api
|
|
40
45
|
|
|
41
46
|
Add the following line to all Api Controllers:
|
|
@@ -72,7 +77,9 @@ Then render the result
|
|
|
72
77
|
|
|
73
78
|
api.render_result(format: params[:format])
|
|
74
79
|
|
|
75
|
-
|
|
80
|
+
### Support for JSONP
|
|
81
|
+
|
|
82
|
+
If your API supports callbacks (JSONP) these can also be passed. The returned content_type header will change to `application/javascript` in this case.
|
|
76
83
|
|
|
77
84
|
api.render_result(format: params[:format], callback: params[:callback])
|
|
78
85
|
|
|
@@ -25,10 +25,16 @@ module Easy::Api::BlockWrapper
|
|
|
25
25
|
# use the controller to render the response
|
|
26
26
|
def render_result(render_params)
|
|
27
27
|
format = (render_params[:format] || 'json').try(:to_sym)
|
|
28
|
+
formatted_result = if format == :xml
|
|
29
|
+
@result.to_xml(render_params[:options] || {})
|
|
30
|
+
else
|
|
31
|
+
@result
|
|
32
|
+
end
|
|
33
|
+
|
|
28
34
|
if render_params[:callback].blank?
|
|
29
|
-
@controller.render(format =>
|
|
35
|
+
@controller.render(format => formatted_result, :status => @result.status_code)
|
|
30
36
|
else
|
|
31
|
-
@controller.render(format =>
|
|
37
|
+
@controller.render(format => formatted_result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
|
|
32
38
|
end
|
|
33
39
|
end
|
|
34
40
|
|
data/lib/easy/api/error.rb
CHANGED
|
@@ -4,7 +4,7 @@ module Easy::Api
|
|
|
4
4
|
attr_reader :code, :message
|
|
5
5
|
|
|
6
6
|
def self.codes
|
|
7
|
-
{
|
|
7
|
+
@_codes ||= {
|
|
8
8
|
:invalid => 400,
|
|
9
9
|
:unauthorized => 401,
|
|
10
10
|
:not_found => 404,
|
|
@@ -13,7 +13,7 @@ module Easy::Api
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def self.messages
|
|
16
|
-
{
|
|
16
|
+
@_messages ||= {
|
|
17
17
|
:invalid => "Invalid request",
|
|
18
18
|
:unauthorized => "Unauthorized request",
|
|
19
19
|
:not_found => "Resource not found",
|
data/lib/easy/api/result.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'ostruct'
|
|
2
|
+
require "active_support/core_ext/hash/conversions"
|
|
2
3
|
|
|
3
4
|
module Easy::Api
|
|
4
5
|
# Encapsulates the response data of an API call
|
|
@@ -42,6 +43,9 @@ module Easy::Api
|
|
|
42
43
|
# Will always contain 'success', the error if there is one, and any dynamic attributes.
|
|
43
44
|
# @return [Hash]
|
|
44
45
|
def to_xml(options={})
|
|
46
|
+
options = options.dup
|
|
47
|
+
options[:root] ||= 'response'
|
|
48
|
+
options[:skip_types] ||= true
|
|
45
49
|
convert_to_hash.to_xml(options)
|
|
46
50
|
end
|
|
47
51
|
|
data/lib/easy/api/version.rb
CHANGED
|
@@ -28,7 +28,7 @@ RSpec.describe CustomersController, :type => :controller do
|
|
|
28
28
|
|
|
29
29
|
it "gets the index in xml format" do
|
|
30
30
|
get :index, :format => 'xml'
|
|
31
|
-
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<
|
|
31
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customers>\n <customer>\n <name>fred</name>\n <age>19</age>\n </customer>\n <customer>\n <name>jackie</name>\n <age>21</age>\n </customer>\n </customers>\n <success>true</success>\n</response>\n")
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
end
|
|
@@ -47,7 +47,7 @@ RSpec.describe CustomersController, :type => :controller do
|
|
|
47
47
|
|
|
48
48
|
it "gets show in xml format" do
|
|
49
49
|
get :show, :format => 'xml', id: 1
|
|
50
|
-
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<
|
|
50
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customer>\n <name>fred</name>\n <age>21</age>\n </customer>\n <success>true</success>\n</response>\n")
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
end
|
|
@@ -33,4 +33,18 @@ describe Easy::Api::Error do
|
|
|
33
33
|
expect(subject[:message]).to eql message
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
|
+
|
|
37
|
+
describe "#errors" do
|
|
38
|
+
it "should allow the Easy::Api:Error.codes to be altered" do
|
|
39
|
+
Easy::Api::Error.codes[:new_error] = 1010
|
|
40
|
+
expect(Easy::Api::Error.codes[:new_error]).to eq(1010)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
describe "#messages" do
|
|
45
|
+
it "should allow the Easy::Api:Error.messages to be altered" do
|
|
46
|
+
Easy::Api::Error.messages[:new_error] = 'I am new'
|
|
47
|
+
expect(Easy::Api::Error.messages[:new_error]).to eq('I am new')
|
|
48
|
+
end
|
|
49
|
+
end
|
|
36
50
|
end
|
|
@@ -93,4 +93,38 @@ describe Easy::Api::Result do
|
|
|
93
93
|
end
|
|
94
94
|
end
|
|
95
95
|
end
|
|
96
|
+
|
|
97
|
+
describe "#to_xml" do
|
|
98
|
+
|
|
99
|
+
let(:result) { Easy::Api::Result.new }
|
|
100
|
+
subject { result.to_xml }
|
|
101
|
+
|
|
102
|
+
context "when result is unsuccessful" do
|
|
103
|
+
let(:api_error) { Easy::Api::Error.new(:unauthorized) }
|
|
104
|
+
|
|
105
|
+
before do
|
|
106
|
+
result.error = api_error
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "renders the object as " do
|
|
110
|
+
expect(subject).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <success>false</success>\n <error>\n <code>401</code>\n <message>Unauthorized request</message>\n </error>\n</response>\n")
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context "when result is successful" do
|
|
116
|
+
|
|
117
|
+
before do
|
|
118
|
+
result.success = true
|
|
119
|
+
result.customer = "Bob Loblaw"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "renders the object as " do
|
|
123
|
+
expect(subject).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customer>Bob Loblaw</customer>\n <success>true</success>\n</response>\n")
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
96
130
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: easy-api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shevaun Coker
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2015-08-28 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: activemodel
|
|
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
175
175
|
version: '0'
|
|
176
176
|
requirements: []
|
|
177
177
|
rubyforge_project:
|
|
178
|
-
rubygems_version: 2.
|
|
178
|
+
rubygems_version: 2.2.2
|
|
179
179
|
signing_key:
|
|
180
180
|
specification_version: 4
|
|
181
181
|
summary: Facilitates standard success and error behaviour in API responses
|