easy-api 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/CHANGELOG.md +13 -0
- data/README.md +53 -4
- data/easy-api.gemspec +12 -11
- data/lib/easy/api/block_wrapper.rb +42 -0
- data/lib/easy/api/version.rb +1 -1
- data/lib/easy/api.rb +7 -0
- data/lib/easy-api.rb +5 -0
- data/spec/fixtures/application.rb +22 -0
- data/spec/fixtures/controllers.rb +43 -0
- data/spec/fixtures/models.rb +38 -0
- data/spec/lib/easy/api/customers_controller_spec.rb +55 -0
- data/spec/lib/easy/api/error_spec.rb +17 -6
- data/spec/lib/easy/api/result_spec.rb +52 -17
- data/spec/lib/easy/api/users_controller_spec.rb +55 -0
- metadata +95 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6225c8e362543f69674de074583414734e53bab0
|
4
|
+
data.tar.gz: 5b0c791d62a1df723f0768dd629e6881bcc3fb3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99b7b389382b946f208ddf5f9a19f096c0f4be6a8fc6cc8e8278a0a8f2222de803813d6cd98be0e2b8aaff78e9258287fe92441cf485617b09207592ee2432a0
|
7
|
+
data.tar.gz: f50f43b54d728207ef406c5cb588c90e8f0d701974862ff17e793ffa570e57d1ca135e4ed559004b70b04fff5979e26ff9949108991e807fb7a914d52a885649
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# EasyAPI 0.2.0 (July 28, 2014) #
|
2
|
+
|
3
|
+
* Add support for new block-style syntax
|
4
|
+
* Add support for JRuby
|
5
|
+
|
6
|
+
# ActiveAttr 0.1.3 (April 16, 2014) #
|
7
|
+
|
8
|
+
* Return correct content_type for jsonp requests
|
9
|
+
|
10
|
+
# ActiveAttr 0.1.2 (March 11, 2013) #
|
11
|
+
|
12
|
+
* Genericize error messages
|
13
|
+
* Add support for Ruby 1.8.7
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Easy::Api
|
2
2
|
|
3
|
+
[<img src="https://travis-ci.org/AbleTech/easy-api.png" />](https://travis-ci.org/AbleTech/easy-api)
|
4
|
+
|
3
5
|
A repository of common, reusable API code. Its purpose is to make all of Abletech's APIs respond in a consistent manner.
|
4
6
|
|
5
7
|
## Installation
|
@@ -31,16 +33,59 @@ To initialise an error, pass in the type you want, e.g.
|
|
31
33
|
If you want to override the default error message, pass in a custom message, e.g.
|
32
34
|
|
33
35
|
Easy::Api::Error.new(:invalid, @user.errors.full_messages.join(', '))
|
34
|
-
|
36
|
+
|
35
37
|
Easy::Api::Error objects have a code (e.g. 404) and a message (e.g. 'Resource not found')
|
36
38
|
|
39
|
+
### Using Easy::Api
|
40
|
+
|
41
|
+
Add the following line to all Api Controllers:
|
42
|
+
|
43
|
+
include Easy::Api
|
44
|
+
|
45
|
+
Then in your Api actions, do your logic inside a block:
|
46
|
+
|
47
|
+
easy_api do |api|
|
48
|
+
api.parcel = Parcel.first
|
49
|
+
api.status_code = 200
|
50
|
+
api.success = true
|
51
|
+
api.render_result(format: params[:format])
|
52
|
+
end
|
53
|
+
|
54
|
+
If the request is a success, you must set
|
55
|
+
|
56
|
+
api.status_code = 200
|
57
|
+
api.success = true
|
58
|
+
|
59
|
+
and you can also set any other values you want to send back, e.g.
|
60
|
+
|
61
|
+
api.parcel = Parcel.first
|
62
|
+
|
63
|
+
If the request is unsuccessful, you must set the status_code, e.g.
|
64
|
+
|
65
|
+
api.status_code = 401
|
66
|
+
|
67
|
+
and you also need to set error to be an instance of Easy::Api::Error, e.g.
|
68
|
+
|
69
|
+
api.error = Easy::Api::Error.new(:unauthorized)
|
70
|
+
|
71
|
+
Then render the result
|
72
|
+
|
73
|
+
api.render_result(format: params[:format])
|
74
|
+
|
75
|
+
If your API supports callbacks these can also be passed
|
76
|
+
|
77
|
+
api.render_result(format: params[:format], callback: params[:callback])
|
78
|
+
|
37
79
|
### Using Easy::Api::ControllerMethods
|
80
|
+
|
81
|
+
**Depricated**
|
82
|
+
|
38
83
|
Add the following line to all Api Controllers:
|
39
|
-
|
84
|
+
|
40
85
|
include Easy::Api::ControllerMethods
|
41
86
|
|
42
|
-
then in your Api actions, add values to the @result (Easy::Api::Result) object.
|
43
|
-
If the request is a success, you must set
|
87
|
+
then in your Api actions, add values to the @result (Easy::Api::Result) object.
|
88
|
+
If the request is a success, you must set
|
44
89
|
|
45
90
|
@result.status_code = 200
|
46
91
|
@result.success = true
|
@@ -57,6 +102,10 @@ and you also need to set error to be an instance of Easy::Api::Error, e.g.
|
|
57
102
|
|
58
103
|
@result.error = Easy::Api::Error.new(:unauthorized)
|
59
104
|
|
105
|
+
Then render the result
|
106
|
+
|
107
|
+
render_format
|
108
|
+
|
60
109
|
## Contributing
|
61
110
|
|
62
111
|
1. Fork it
|
data/easy-api.gemspec
CHANGED
@@ -1,25 +1,26 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'easy/api/version'
|
2
|
+
require File.expand_path('../lib/easy/api/version', __FILE__)
|
5
3
|
|
6
4
|
Gem::Specification.new do |gem|
|
7
5
|
gem.name = "easy-api"
|
8
6
|
gem.version = Easy::Api::VERSION
|
9
|
-
gem.authors = ["Shevaun Coker"]
|
10
|
-
gem.email = ["shevaun.coker@abletech.co.nz"]
|
11
|
-
gem.description = %q{
|
12
|
-
gem.summary = %q{
|
13
|
-
gem.homepage = ""
|
7
|
+
gem.authors = ["Shevaun Coker", "Joseph Leniston", "Nigel Ramsay"]
|
8
|
+
gem.email = ["shevaun.coker@abletech.co.nz", "joseph.leniston@abletech.co.nz", "nigel.ramsay@abletech.co.nz"]
|
9
|
+
gem.description = %q{Enables consistent responses for API calls}
|
10
|
+
gem.summary = %q{Facilitates standard success and error behaviour in API responses}
|
11
|
+
gem.homepage = "https://github.com/AbleTech/easy-api"
|
14
12
|
|
15
13
|
gem.files = `git ls-files`.split($/)
|
16
14
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
15
|
gem.test_files = gem.files.grep(%r{^(spec|features)/})
|
18
16
|
gem.require_paths = ["lib"]
|
19
17
|
|
20
|
-
gem.
|
21
|
-
|
18
|
+
gem.add_development_dependency 'activemodel', '>= 3.0.0'
|
19
|
+
gem.add_development_dependency 'actionpack', '>= 3.0.0'
|
20
|
+
gem.add_development_dependency 'activesupport', '>= 3.0.0'
|
21
|
+
gem.add_development_dependency 'multi_json', '~> 1.0'
|
22
22
|
gem.add_development_dependency 'bundler'
|
23
23
|
gem.add_development_dependency 'pry'
|
24
|
-
gem.add_development_dependency 'rspec', '
|
24
|
+
gem.add_development_dependency 'rspec', '>= 2.14'
|
25
|
+
gem.add_development_dependency 'rspec-rails'
|
25
26
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Include this module in all API controllers to get consistent responses
|
2
|
+
module Easy::Api::BlockWrapper
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
# Initialises a new Easy::Api::Wrapper object takes a block of code to be rendered using the formatter
|
6
|
+
def easy_api &block
|
7
|
+
wrapper ||= Wrapper.new(self)
|
8
|
+
yield(wrapper)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.send :include, InstanceMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
# A class to encapulate the API code so it can be called from the block.
|
18
|
+
class Wrapper
|
19
|
+
|
20
|
+
def initialize(controller)
|
21
|
+
@controller = controller
|
22
|
+
@result ||= Easy::Api::Result.new
|
23
|
+
end
|
24
|
+
|
25
|
+
# use the controller to render the response
|
26
|
+
def render_result(render_params)
|
27
|
+
format = (render_params[:format] || 'json').try(:to_sym)
|
28
|
+
if render_params[:callback].blank?
|
29
|
+
@controller.render(format => @result, :status => @result.status_code)
|
30
|
+
else
|
31
|
+
@controller.render(format => @result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Delegate other method calls to the result object
|
36
|
+
def method_missing(name, *args, &block)
|
37
|
+
@result.send(name, *args, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/easy/api/version.rb
CHANGED
data/lib/easy/api.rb
CHANGED
@@ -1,4 +1,11 @@
|
|
1
1
|
require 'easy/api/version'
|
2
2
|
require 'easy/api/controller_methods'
|
3
|
+
require 'easy/api/block_wrapper'
|
3
4
|
require 'easy/api/result'
|
4
5
|
require 'easy/api/error'
|
6
|
+
|
7
|
+
module Easy::Api
|
8
|
+
def self.included(base)
|
9
|
+
base.send :include, Easy::Api::BlockWrapper
|
10
|
+
end
|
11
|
+
end
|
data/lib/easy-api.rb
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/easy/api/version'
|
2
|
+
require File.dirname(__FILE__) + '/easy/api/controller_methods'
|
3
|
+
require File.dirname(__FILE__) + '/easy/api/block_wrapper'
|
4
|
+
require File.dirname(__FILE__) + '/easy/api/result'
|
5
|
+
require File.dirname(__FILE__) + '/easy/api/error'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_dispatch'
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
class App
|
7
|
+
def env_config; {} end
|
8
|
+
def routes
|
9
|
+
return @routes if defined?(@routes)
|
10
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
11
|
+
@routes.draw do
|
12
|
+
resources :customers
|
13
|
+
resources :users
|
14
|
+
end
|
15
|
+
@routes
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.application
|
20
|
+
@app ||= App.new
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class TestController < ActionController::Base
|
2
|
+
include Rails.application.routes.url_helpers
|
3
|
+
end
|
4
|
+
|
5
|
+
class UsersController < TestController
|
6
|
+
include Easy::Api::ControllerMethods
|
7
|
+
|
8
|
+
def index
|
9
|
+
@result.users = [User.new("bob", 25), User.new('sally',40)]
|
10
|
+
@result.success = true
|
11
|
+
@result.status_code = 200
|
12
|
+
render_format
|
13
|
+
end
|
14
|
+
|
15
|
+
def show
|
16
|
+
@result.user = User.new("bob", 25)
|
17
|
+
@result.success = true
|
18
|
+
@result.status_code = 200
|
19
|
+
render_format
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class CustomersController < TestController
|
24
|
+
include Easy::Api
|
25
|
+
|
26
|
+
def index
|
27
|
+
easy_api do |api|
|
28
|
+
api.customers = [Customer.new("fred", 19), Customer.new('jackie',21)]
|
29
|
+
api.success = true
|
30
|
+
api.status_code = 200
|
31
|
+
api.render_result(format: params[:format], callback: params[:callback])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def show
|
36
|
+
easy_api do |api|
|
37
|
+
api.customer = Customer.new("fred", 21)
|
38
|
+
api.success = true
|
39
|
+
api.status_code = 200
|
40
|
+
api.render_result(format: params[:format], callback: params[:callback])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class User
|
2
|
+
attr_reader :age, :name
|
3
|
+
|
4
|
+
def initialize(name, age)
|
5
|
+
@name = name
|
6
|
+
@age = age
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_xml(options = {})
|
10
|
+
require 'builder'
|
11
|
+
options[:indent] ||= 2
|
12
|
+
xml = options[:builder] ||= ::Builder::XmlMarkup.new(indent: options[:indent])
|
13
|
+
xml.instruct! unless options[:skip_instruct]
|
14
|
+
xml.user do
|
15
|
+
xml.tag!(:name, name)
|
16
|
+
xml.tag!(:age, age)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Customer
|
22
|
+
attr_reader :age, :name
|
23
|
+
|
24
|
+
def initialize(name, age)
|
25
|
+
@name = name
|
26
|
+
@age = age
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_xml(options = {})
|
30
|
+
require 'builder'
|
31
|
+
xml = options[:builder] ||= ::Builder::XmlMarkup.new(indent: options[:indent])
|
32
|
+
xml.instruct! unless options[:skip_instruct]
|
33
|
+
xml.customer do
|
34
|
+
xml.tag!(:name, name)
|
35
|
+
xml.tag!(:age, age)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/application'
|
3
|
+
require 'fixtures/controllers'
|
4
|
+
require 'fixtures/models'
|
5
|
+
require 'rspec/rails'
|
6
|
+
require 'multi_json'
|
7
|
+
|
8
|
+
RSpec.describe CustomersController, :type => :controller do
|
9
|
+
|
10
|
+
context 'GET #index' do
|
11
|
+
|
12
|
+
it "gets the index in json format" do
|
13
|
+
get :index, :format => 'json'
|
14
|
+
|
15
|
+
parsed_response = MultiJson.load(response.body)
|
16
|
+
expect(parsed_response['customers'].size).to eq(2)
|
17
|
+
|
18
|
+
customer_names = parsed_response['customers'].collect{|c| c['name']}
|
19
|
+
expect(customer_names).to include('fred')
|
20
|
+
expect(customer_names).to include('jackie')
|
21
|
+
|
22
|
+
customer_ages = parsed_response['customers'].collect{|c| c['age']}
|
23
|
+
expect(customer_ages).to include(19)
|
24
|
+
expect(customer_ages).to include(21)
|
25
|
+
|
26
|
+
expect(parsed_response['success']).to eq(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "gets the index in xml format" do
|
30
|
+
get :index, :format => 'xml'
|
31
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <customers type=\"array\">\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 type=\"boolean\">true</success>\n</hash>\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'GET #show' do
|
37
|
+
|
38
|
+
it "gets show in json format" do
|
39
|
+
get :show, :format => 'json', id: 1
|
40
|
+
|
41
|
+
parsed_response = MultiJson.load(response.body)
|
42
|
+
expect(parsed_response['customer']).to_not be(nil)
|
43
|
+
expect(parsed_response['customer']['name']).to eq('fred')
|
44
|
+
expect(parsed_response['customer']['age']).to eq(21)
|
45
|
+
expect(parsed_response['success']).to eq(true)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "gets show in xml format" do
|
49
|
+
get :show, :format => 'xml', id: 1
|
50
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <customer>\n <name>fred</name>\n <age>21</age>\n </customer>\n <success type=\"boolean\">true</success>\n</hash>\n")
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -6,20 +6,31 @@ describe Easy::Api::Error do
|
|
6
6
|
|
7
7
|
context "when a message is passed in" do
|
8
8
|
let(:message) { "Hello, world!" }
|
9
|
-
|
9
|
+
|
10
|
+
it "has sets the message" do
|
11
|
+
expect(subject.message).to eql message
|
12
|
+
end
|
10
13
|
end
|
11
14
|
|
12
15
|
context "when no message is passed in" do
|
13
16
|
let(:message) { nil }
|
14
|
-
|
17
|
+
|
18
|
+
it "the message is invalid" do
|
19
|
+
expect(subject.message).to eql Easy::Api::Error.messages[:invalid]
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
17
23
|
|
18
24
|
describe "#as_json" do
|
19
|
-
subject { Easy::Api::Error.new(:unexpected,
|
20
|
-
let(:
|
25
|
+
subject { Easy::Api::Error.new(:unexpected, message).as_json }
|
26
|
+
let(:message) { 'uh, oh' }
|
21
27
|
|
22
|
-
|
23
|
-
|
28
|
+
it "the code is returned" do
|
29
|
+
expect(subject[:code]).to eql Easy::Api::Error.codes[:unexpected]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "the message is returned" do
|
33
|
+
expect(subject[:message]).to eql message
|
34
|
+
end
|
24
35
|
end
|
25
36
|
end
|
@@ -7,36 +7,68 @@ describe Easy::Api::Result do
|
|
7
7
|
|
8
8
|
context "when result is unsuccessful" do
|
9
9
|
let(:api_error) { Easy::Api::Error.new(:unauthorized) }
|
10
|
-
|
10
|
+
|
11
11
|
before do
|
12
12
|
result.error = api_error
|
13
13
|
end
|
14
14
|
|
15
|
-
it
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
it
|
15
|
+
it "has a success key" do
|
16
|
+
expect(subject).to have_key(:success)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "is not successful" do
|
20
|
+
expect(subject[:success]).to be(false)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "has no status code" do
|
24
|
+
expect(subject).to_not have_key(:status_code)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has an error" do
|
28
|
+
expect(subject).to have_key(:error)
|
29
|
+
end
|
20
30
|
|
21
|
-
context "
|
31
|
+
context "json error" do
|
22
32
|
subject { result.as_json[:error] }
|
23
|
-
|
24
|
-
|
33
|
+
|
34
|
+
it "has an error" do
|
35
|
+
expect(subject.code).to eql api_error.code
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has an error code" do
|
39
|
+
expect(subject.message).to eql api_error.message
|
40
|
+
end
|
41
|
+
|
25
42
|
end
|
26
43
|
end
|
27
44
|
|
28
45
|
context "when result is successful" do
|
46
|
+
|
29
47
|
before do
|
30
48
|
result.success = true
|
31
49
|
result.customer = "Bob Loblaw"
|
32
50
|
end
|
33
51
|
|
34
|
-
it
|
35
|
-
|
52
|
+
it "has a success key" do
|
53
|
+
expect(subject).to have_key(:success)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "is successful" do
|
57
|
+
expect(subject[:success]).to be(true)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "has a customer key" do
|
61
|
+
expect(subject).to have_key(:customer)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "has a customer name" do
|
65
|
+
expect(subject[:customer]).to eql "Bob Loblaw"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "has no status code" do
|
69
|
+
expect(subject).to_not have_key(:status_code)
|
70
|
+
end
|
36
71
|
|
37
|
-
it { should have_key :customer }
|
38
|
-
its([:customer]) { should == "Bob Loblaw" }
|
39
|
-
it { should_not have_key :status_code }
|
40
72
|
end
|
41
73
|
|
42
74
|
end
|
@@ -44,10 +76,10 @@ describe Easy::Api::Result do
|
|
44
76
|
describe "#status_code" do
|
45
77
|
let(:result) { Easy::Api::Result.new }
|
46
78
|
subject { result.status_code }
|
47
|
-
|
79
|
+
|
48
80
|
context "when it doesn't get set" do
|
49
81
|
it "should throw an exception" do
|
50
|
-
|
82
|
+
expect { subject }.to raise_error("Easy::Api::Result needs a status_code!")
|
51
83
|
end
|
52
84
|
end
|
53
85
|
|
@@ -55,7 +87,10 @@ describe Easy::Api::Result do
|
|
55
87
|
before do
|
56
88
|
result.status_code = 200
|
57
89
|
end
|
58
|
-
|
90
|
+
|
91
|
+
it "should reuturn the code" do
|
92
|
+
expect(subject).to eql 200
|
93
|
+
end
|
59
94
|
end
|
60
95
|
end
|
61
96
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/application'
|
3
|
+
require 'fixtures/controllers'
|
4
|
+
require 'fixtures/models'
|
5
|
+
require 'rspec/rails'
|
6
|
+
|
7
|
+
RSpec.describe UsersController, :type => :controller do
|
8
|
+
context 'depricated render_format method' do
|
9
|
+
|
10
|
+
context 'GET #index' do
|
11
|
+
|
12
|
+
it "gets the index in json format" do
|
13
|
+
get :index, :format => 'json'
|
14
|
+
|
15
|
+
parsed_response = MultiJson.load(response.body)
|
16
|
+
expect(parsed_response['users'].size).to eq(2)
|
17
|
+
|
18
|
+
user_names = parsed_response['users'].collect{|c| c['name']}
|
19
|
+
expect(user_names).to include('bob')
|
20
|
+
expect(user_names).to include('sally')
|
21
|
+
|
22
|
+
user_ages = parsed_response['users'].collect{|c| c['age']}
|
23
|
+
expect(user_ages).to include(25)
|
24
|
+
expect(user_ages).to include(40)
|
25
|
+
|
26
|
+
expect(parsed_response['success']).to eq(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "gets the index in xml format" do
|
30
|
+
get :index, :format => 'xml'
|
31
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <users>\n <user>\n <name>bob</name>\n <age>25</age>\n </user>\n <user>\n <name>sally</name>\n <age>40</age>\n </user>\n </users>\n <success>true</success>\n</response>\n")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'GET #show' do
|
36
|
+
it "gets show in json format" do
|
37
|
+
get :show, :format => 'json', id: 1
|
38
|
+
|
39
|
+
parsed_response = MultiJson.load(response.body)
|
40
|
+
expect(parsed_response['user']).to_not be(nil)
|
41
|
+
expect(parsed_response['user']['name']).to eq('bob')
|
42
|
+
expect(parsed_response['user']['age']).to eq(25)
|
43
|
+
expect(parsed_response['success']).to eq(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "gets show in xml format" do
|
47
|
+
get :show, :format => 'xml', id: 1
|
48
|
+
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <user>\n <name>bob</name>\n <age>25</age>\n </user>\n <success>true</success>\n</response>\n")
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
metadata
CHANGED
@@ -1,93 +1,162 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shevaun Coker
|
8
|
+
- Joseph Leniston
|
9
|
+
- Nigel Ramsay
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
date: 2014-
|
13
|
+
date: 2014-07-28 00:00:00.000000000 Z
|
12
14
|
dependencies:
|
13
15
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
16
|
+
name: activemodel
|
15
17
|
requirement: !ruby/object:Gem::Requirement
|
16
18
|
requirements:
|
17
|
-
- -
|
19
|
+
- - '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: 3.0.0
|
20
|
-
type: :
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.0.0
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: actionpack
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0
|
36
|
+
type: :development
|
21
37
|
prerelease: false
|
22
38
|
version_requirements: !ruby/object:Gem::Requirement
|
23
39
|
requirements:
|
24
|
-
- -
|
40
|
+
- - '>='
|
25
41
|
- !ruby/object:Gem::Version
|
26
42
|
version: 3.0.0
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: activesupport
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.0.0
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.0.0
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: multi_json
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '1.0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '1.0'
|
27
71
|
- !ruby/object:Gem::Dependency
|
28
72
|
name: bundler
|
29
73
|
requirement: !ruby/object:Gem::Requirement
|
30
74
|
requirements:
|
31
|
-
- -
|
75
|
+
- - '>='
|
32
76
|
- !ruby/object:Gem::Version
|
33
77
|
version: '0'
|
34
78
|
type: :development
|
35
79
|
prerelease: false
|
36
80
|
version_requirements: !ruby/object:Gem::Requirement
|
37
81
|
requirements:
|
38
|
-
- -
|
82
|
+
- - '>='
|
39
83
|
- !ruby/object:Gem::Version
|
40
84
|
version: '0'
|
41
85
|
- !ruby/object:Gem::Dependency
|
42
86
|
name: pry
|
43
87
|
requirement: !ruby/object:Gem::Requirement
|
44
88
|
requirements:
|
45
|
-
- -
|
89
|
+
- - '>='
|
46
90
|
- !ruby/object:Gem::Version
|
47
91
|
version: '0'
|
48
92
|
type: :development
|
49
93
|
prerelease: false
|
50
94
|
version_requirements: !ruby/object:Gem::Requirement
|
51
95
|
requirements:
|
52
|
-
- -
|
96
|
+
- - '>='
|
53
97
|
- !ruby/object:Gem::Version
|
54
98
|
version: '0'
|
55
99
|
- !ruby/object:Gem::Dependency
|
56
100
|
name: rspec
|
57
101
|
requirement: !ruby/object:Gem::Requirement
|
58
102
|
requirements:
|
59
|
-
- -
|
103
|
+
- - '>='
|
60
104
|
- !ruby/object:Gem::Version
|
61
|
-
version: '2.
|
105
|
+
version: '2.14'
|
62
106
|
type: :development
|
63
107
|
prerelease: false
|
64
108
|
version_requirements: !ruby/object:Gem::Requirement
|
65
109
|
requirements:
|
66
|
-
- -
|
110
|
+
- - '>='
|
67
111
|
- !ruby/object:Gem::Version
|
68
|
-
version: '2.
|
69
|
-
|
112
|
+
version: '2.14'
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rspec-rails
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
description: Enables consistent responses for API calls
|
70
128
|
email:
|
71
129
|
- shevaun.coker@abletech.co.nz
|
130
|
+
- joseph.leniston@abletech.co.nz
|
131
|
+
- nigel.ramsay@abletech.co.nz
|
72
132
|
executables: []
|
73
133
|
extensions: []
|
74
134
|
extra_rdoc_files: []
|
75
135
|
files:
|
76
|
-
-
|
136
|
+
- .gitignore
|
137
|
+
- .travis.yml
|
138
|
+
- CHANGELOG.md
|
77
139
|
- Gemfile
|
78
140
|
- LICENSE
|
79
141
|
- README.md
|
80
142
|
- Rakefile
|
81
143
|
- easy-api.gemspec
|
144
|
+
- lib/easy-api.rb
|
82
145
|
- lib/easy/api.rb
|
146
|
+
- lib/easy/api/block_wrapper.rb
|
83
147
|
- lib/easy/api/controller_methods.rb
|
84
148
|
- lib/easy/api/error.rb
|
85
149
|
- lib/easy/api/result.rb
|
86
150
|
- lib/easy/api/version.rb
|
151
|
+
- spec/fixtures/application.rb
|
152
|
+
- spec/fixtures/controllers.rb
|
153
|
+
- spec/fixtures/models.rb
|
154
|
+
- spec/lib/easy/api/customers_controller_spec.rb
|
87
155
|
- spec/lib/easy/api/error_spec.rb
|
88
156
|
- spec/lib/easy/api/result_spec.rb
|
157
|
+
- spec/lib/easy/api/users_controller_spec.rb
|
89
158
|
- spec/spec_helper.rb
|
90
|
-
homepage:
|
159
|
+
homepage: https://github.com/AbleTech/easy-api
|
91
160
|
licenses: []
|
92
161
|
metadata: {}
|
93
162
|
post_install_message:
|
@@ -96,22 +165,26 @@ require_paths:
|
|
96
165
|
- lib
|
97
166
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
167
|
requirements:
|
99
|
-
- -
|
168
|
+
- - '>='
|
100
169
|
- !ruby/object:Gem::Version
|
101
170
|
version: '0'
|
102
171
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
172
|
requirements:
|
104
|
-
- -
|
173
|
+
- - '>='
|
105
174
|
- !ruby/object:Gem::Version
|
106
175
|
version: '0'
|
107
176
|
requirements: []
|
108
177
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
178
|
+
rubygems_version: 2.0.6
|
110
179
|
signing_key:
|
111
180
|
specification_version: 4
|
112
|
-
summary:
|
181
|
+
summary: Facilitates standard success and error behaviour in API responses
|
113
182
|
test_files:
|
183
|
+
- spec/fixtures/application.rb
|
184
|
+
- spec/fixtures/controllers.rb
|
185
|
+
- spec/fixtures/models.rb
|
186
|
+
- spec/lib/easy/api/customers_controller_spec.rb
|
114
187
|
- spec/lib/easy/api/error_spec.rb
|
115
188
|
- spec/lib/easy/api/result_spec.rb
|
189
|
+
- spec/lib/easy/api/users_controller_spec.rb
|
116
190
|
- spec/spec_helper.rb
|
117
|
-
has_rdoc:
|