Skip to content

Commit d458578

Browse files
committed
Merge pull request #2 from AbleTech/block_syntax
new block syntax functionality
2 parents a2d4ee4 + fc51748 commit d458578

15 files changed

Lines changed: 420 additions & 39 deletions

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
rvm:
3+
- 1.9.3
4+
- 2.0.0
5+
- 2.1.2
6+
- jruby-19mode # JRuby in 1.9 mode

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Easy::Api
22

3+
[<img src="https://travis-ci.org/AbleTech/easy-api.png" />](https://travis-ci.org/AbleTech/easy-api)
4+
35
A repository of common, reusable API code. Its purpose is to make all of Abletech's APIs respond in a consistent manner.
46

57
## Installation
@@ -31,16 +33,59 @@ To initialise an error, pass in the type you want, e.g.
3133
If you want to override the default error message, pass in a custom message, e.g.
3234

3335
Easy::Api::Error.new(:invalid, @user.errors.full_messages.join(', '))
34-
36+
3537
Easy::Api::Error objects have a code (e.g. 404) and a message (e.g. 'Resource not found')
3638

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+
3779
### Using Easy::Api::ControllerMethods
80+
81+
**Depricated**
82+
3883
Add the following line to all Api Controllers:
39-
84+
4085
include Easy::Api::ControllerMethods
4186

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
4489

4590
@result.status_code = 200
4691
@result.success = true
@@ -57,6 +102,10 @@ and you also need to set error to be an instance of Easy::Api::Error, e.g.
57102

58103
@result.error = Easy::Api::Error.new(:unauthorized)
59104

105+
Then render the result
106+
107+
render_format
108+
60109
## Contributing
61110

62111
1. Fork it

easy-api.gemspec

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# -*- encoding: utf-8 -*-
2-
lib = File.expand_path('../lib', __FILE__)
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__)
53

64
Gem::Specification.new do |gem|
75
gem.name = "easy-api"
86
gem.version = Easy::Api::VERSION
9-
gem.authors = ["Shevaun Coker"]
10-
gem.email = ["shevaun.coker@abletech.co.nz"]
11-
gem.description = %q{A repository of common, reusable API code}
12-
gem.summary = %q{Provides consistent responses for Abletech APIs}
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.homepage = "https://github.com/AbleTech/easy-api"
1411

1512
gem.files = `git ls-files`.split($/)
1613
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
1714
gem.test_files = gem.files.grep(%r{^(spec|features)/})
1815
gem.require_paths = ["lib"]
1916

20-
gem.add_runtime_dependency 'rails', '>= 3.0.0'
21-
17+
gem.add_development_dependency 'activemodel', '>= 3.0.0'
18+
gem.add_development_dependency 'actionpack', '>= 3.0.0'
19+
gem.add_development_dependency 'activesupport', '>= 3.0.0'
20+
gem.add_development_dependency 'multi_json', '~> 1.0'
2221
gem.add_development_dependency 'bundler'
2322
gem.add_development_dependency 'pry'
24-
gem.add_development_dependency 'rspec', '~> 2.0'
23+
gem.add_development_dependency 'rspec', '>= 2.14'
24+
gem.add_development_dependency 'rspec-rails'
2525
end

lib/easy-api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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'

lib/easy/api.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
require 'easy/api/version'
22
require 'easy/api/controller_methods'
3+
require 'easy/api/block_wrapper'
34
require 'easy/api/result'
45
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

lib/easy/api/block_wrapper.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/easy/api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Easy
22
module Api
3-
VERSION = "0.1.3"
3+
VERSION = "0.2.0"
44
end
55
end

spec/fixtures/application.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/fixtures/controllers.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)