biceps 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/biceps.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'biceps/core_ext/action_dispatch/routing/mapper'
2
- require 'biceps/core_ext/action_controller/base'
3
2
 
4
- module Biceps
5
- autoload :ApiVersion, 'biceps/api_version'
6
- autoload :ModelVersioning, 'biceps/model_versioning'
7
- autoload :Jsonp, 'biceps/jsonp'
8
- autoload :Serializer, 'biceps/serializer'
9
- autoload :Parser, 'biceps/parser'
10
- autoload :Builder, 'biceps/builder'
11
- end
3
+ module Biceps; end
4
+
5
+ require 'biceps/api_version'
6
+ require 'biceps/test_helper'
@@ -1,13 +1,36 @@
1
1
  module Biceps
2
2
  class ApiVersion
3
- attr_accessor :version
3
+ attr_accessor :version, :accept
4
4
 
5
5
  def initialize(version)
6
- @version = version
6
+ @version = [version].flatten
7
7
  end
8
8
 
9
9
  def matches?(request)
10
- Biceps::Parser.new(request, version).valid?
10
+ @accept = request.accept
11
+ valid_api_version?
12
+ end
13
+
14
+
15
+ private
16
+ def valid_api_version?
17
+ version.include?(request_version)
18
+ end
19
+
20
+ def request_version
21
+ is_api_call?[1].to_i if is_api_call?
22
+ end
23
+
24
+ def is_api_call?
25
+ @is_api_call = accept.match(regex)
26
+ end
27
+
28
+ def regex
29
+ Regexp.new("application/vnd.#{app_name};ver=([0-9]+)")
30
+ end
31
+
32
+ def app_name
33
+ Rails.application.class.to_s.split('::').first.underscore
11
34
  end
12
35
  end
13
36
  end
@@ -0,0 +1,38 @@
1
+ module Biceps
2
+ class << self
3
+ @@force_test_version = false
4
+
5
+ def force_test_version=(version)
6
+ @@force_test_version = version
7
+ end
8
+
9
+ def force_test_version
10
+ @@force_test_version
11
+ end
12
+
13
+ def clean_test_version
14
+ @@force_test_version = false
15
+ end
16
+
17
+ def force_test_version?
18
+ !!@@force_test_version
19
+ end
20
+ end
21
+
22
+ module TestHelper
23
+ def self.included(base)
24
+ base.class_eval do
25
+ def self.mock_api_version(version)
26
+ before do
27
+ ::Biceps.force_test_version = version
28
+ end
29
+
30
+ after do
31
+ ::Biceps.clean_test_version
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Biceps
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biceps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-27 00:00:00.000000000Z
12
+ date: 2012-02-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70142658651840 !ruby/object:Gem::Requirement
16
+ requirement: &70320728877400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70142658651840
24
+ version_requirements: *70320728877400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70142658648680 !ruby/object:Gem::Requirement
27
+ requirement: &70320728876760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,21 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70142658648680
36
- - !ruby/object:Gem::Dependency
37
- name: json
38
- requirement: &70142658645460 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: 1.6.1
44
- type: :development
45
- prerelease: false
46
- version_requirements: *70142658645460
35
+ version_requirements: *70320728876760
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: rake
49
- requirement: &70142658643020 !ruby/object:Gem::Requirement
38
+ requirement: &70320728876140 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,10 +43,10 @@ dependencies:
54
43
  version: 0.8.7
55
44
  type: :runtime
56
45
  prerelease: false
57
- version_requirements: *70142658643020
46
+ version_requirements: *70320728876140
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: rails
60
- requirement: &70142658638120 !ruby/object:Gem::Requirement
49
+ requirement: &70320728875580 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ! '>='
@@ -65,7 +54,7 @@ dependencies:
65
54
  version: 3.0.0
66
55
  type: :runtime
67
56
  prerelease: false
68
- version_requirements: *70142658638120
57
+ version_requirements: *70320728875580
69
58
  description: Create a versioned API with rails
70
59
  email: dev@evome.fr
71
60
  executables: []
@@ -74,13 +63,8 @@ extra_rdoc_files: []
74
63
  files:
75
64
  - lib/biceps.rb
76
65
  - lib/biceps/api_version.rb
77
- - lib/biceps/builder.rb
78
- - lib/biceps/core_ext/action_controller/base.rb
79
66
  - lib/biceps/core_ext/action_dispatch/routing/mapper.rb
80
- - lib/biceps/jsonp.rb
81
- - lib/biceps/model_versioning.rb
82
- - lib/biceps/parser.rb
83
- - lib/biceps/serializer.rb
67
+ - lib/biceps/test_helper.rb
84
68
  - lib/biceps/version.rb
85
69
  homepage: https://github.com/evome/biceps
86
70
  licenses: []
@@ -1,32 +0,0 @@
1
- module Biceps
2
- module Builder
3
-
4
- # instiantiate_for_api(User, params[:user]) == Constructor.new(User,params[:user], 1)
5
- def self.new(resource, params, version)
6
- begin
7
- constant = "Builders::#{resource}::V#{version}".constantize
8
- instance = constant.new(resource.new, params).resource
9
- rescue NameError
10
- #
11
- # The builder for that version is not defined
12
- # Use the model's internal initialize
13
- instance = resource.new params
14
- end
15
-
16
- instance
17
- end
18
-
19
- class Base
20
- attr_reader :resource, :params
21
-
22
- extend ActiveModel::Callbacks
23
- define_model_callbacks :initialize, :only => :after
24
-
25
- def initialize(resource, params)
26
- run_callbacks :initialize do
27
- @resource, @params = resource, params
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,7 +0,0 @@
1
- ActionController::Base.class_eval do
2
-
3
- def initialize_for_api(resource, params)
4
- api_version = Biceps::Parser.new(request).version
5
- Biceps::Builder.new(resource, params, api_version)
6
- end
7
- end
data/lib/biceps/jsonp.rb DELETED
@@ -1,67 +0,0 @@
1
- module Biceps
2
- class Jsonp
3
- attr_reader :app, :callback_param
4
-
5
- def initialize(app, options = {})
6
- @app = app
7
- @callback_param = options[:callback_param] || 'callback'
8
- end
9
-
10
- # Proxies the request to the application, stripping out the JSON-P callback
11
- # method and padding the response with the appropriate callback format.
12
- #
13
- # Changes nothing if no <tt>callback</tt> param is specified.
14
- #
15
- def call(env)
16
- Parser.new(self, env).perform
17
- end
18
-
19
- class Parser
20
- attr_reader :jsonp, :env, :request
21
- attr_accessor :status, :headers, :response
22
-
23
- delegate :callback_param, :to => :jsonp
24
- delegate :app, :to => :jsonp
25
-
26
- def initialize(jsonp, env)
27
- @jsonp, @env = jsonp, env
28
- @request = Rack::Request.new(@env)
29
- @status, @headers, @response = app.call(env)
30
- end
31
-
32
- def perform
33
- if callback && headers['Content-Type'] =~ /json/i
34
- @response = pad
35
- headers['Content-Length'] = response.first.length.to_s
36
- headers['Content-Type'] = 'application/javascript'
37
- end
38
- [status, headers, response]
39
- end
40
-
41
- private
42
- def callback
43
- @callback ||= request.params.delete(callback_param)
44
- end
45
-
46
- # Pads the response with the appropriate callback format according to the
47
- # JSON-P spec/requirements.
48
- #
49
- # The Rack response spec indicates that it should be enumerable. The method
50
- # of combining all of the data into a single string makes sense since JSON
51
- # is returned as a full string.
52
- #
53
- def pad(body = "")
54
- response.each {|s| body << s }
55
- ["#{callback}({'metadata': #{metadata}, 'data': #{body}})"]
56
- end
57
-
58
- def metadata
59
- # TODO : add all the non-core HTTP headers
60
- {
61
- "status" => status
62
- }.to_json
63
- end
64
-
65
- end
66
- end
67
- end
@@ -1,33 +0,0 @@
1
- module Biceps
2
- module ModelVersioning
3
-
4
- def self.included(klass)
5
- klass.class_eval do
6
-
7
- protected
8
- alias :old_api_behavior :api_behavior
9
- def api_behavior(error)
10
- if api_version
11
- begin
12
- constant = "Serializers::#{resource.class}::V#{api_version}".constantize
13
-
14
- @resource = constant.new(resource)
15
- rescue NameError
16
- #
17
- # The serializer for that version is not defined
18
- # Use the model's internal serialize
19
- #
20
- end
21
- end
22
-
23
- old_api_behavior(error)
24
- end
25
-
26
- private
27
- def api_version
28
- Biceps::Parser.new(request).version
29
- end
30
- end
31
- end
32
- end
33
- end
data/lib/biceps/parser.rb DELETED
@@ -1,31 +0,0 @@
1
- module Biceps
2
- class Parser
3
- attr_accessor :request, :valid_versions
4
-
5
- def initialize(request, valid_versions=nil)
6
- @request = request
7
- @valid_versions = [valid_versions].flatten
8
- end
9
-
10
- def valid?
11
- valid_versions.include?(version)
12
- end
13
-
14
- def version
15
- is_api_call?[1].to_i if is_api_call?
16
- end
17
-
18
- private
19
- def is_api_call?
20
- request.accept.match(regex) if request.accept
21
- end
22
-
23
- def regex
24
- Regexp.new("application/vnd.#{app_name};ver=([0-9]+)")
25
- end
26
-
27
- def app_name
28
- Rails.application.class.to_s.split('::').first.underscore
29
- end
30
- end
31
- end
@@ -1,22 +0,0 @@
1
- module Biceps
2
- class Serializer
3
- include ActiveModel::Serialization
4
- include ActiveModel::Serializers::JSON
5
- include ActiveModel::Serializers::Xml
6
-
7
- attr_accessor :parent
8
- delegate :respond_to?, :to => :parent
9
-
10
- def initialize(parent)
11
- @parent = parent
12
- end
13
-
14
- def method_missing(method, *args, &block)
15
- if parent.respond_to?(method)
16
- parent.send(method, *args, &block)
17
- else
18
- super
19
- end
20
- end
21
- end
22
- end