make_restful 0.1.0 → 0.1.1

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.
data/lib/make_restful.rb CHANGED
@@ -6,135 +6,34 @@ module MakeRestful
6
6
 
7
7
  autoload :ClassMethods
8
8
  autoload :ControllerAdditions
9
+ autoload :Loaders
10
+ autoload :RestMethods
11
+ autoload :Rendering
12
+ autoload :Specification
13
+
14
+ include Loaders
15
+ include RestMethods
16
+ include Rendering
17
+ include Specification
18
+
19
+ delegate :allow_method?, :allow_format?, to: :klass
9
20
 
10
21
  included do
11
22
 
12
- cattr_accessor :allowed_methods, :allowed_formats, :finder, :pagination, :resource_class
23
+ class_attribute :allowed_methods, :allowed_formats, :finder, :pagination, :resource_class
24
+ attr_reader :resource, :instance, :collection
13
25
 
14
26
  resource name.underscore.gsub(/(.*)_controller/,'\1').singularize
15
27
  allow_formats :json, :jsonp, :xml
16
28
  allow_methods :index, :get, :put, :post, :delete
17
29
  find_by :id
18
30
 
19
- ####################################
20
- ## RESTful Methods
21
- ####################################
22
-
23
- end
24
-
25
- # GET /resources
26
- def index
27
- head :method_not_allowed and return unless allow_method? :index, :list
28
- @response = { :"#{self.class.resource_name}" => @collection }
29
- @response.merge!(
30
- {
31
- current_page: results.current_page,
32
- per_page: results.per_page,
33
- total_entries: results.total_entries,
34
- }
35
- ) if self.class.pagination && !params.has_key?(:all)
36
-
37
- if @collection.empty?
38
- @response = { error: 'no results found' }
39
- @status = :not_found
40
- end
41
-
42
- render_formats
43
- end
44
-
45
- # POST /resources
46
- def create
47
- head :method_not_allowed and return unless allow_method? :post, :create
48
- @response = @resource.create
49
- render_formats
50
- end
51
-
52
- # PUT /resources/:id
53
- def show
54
- head :method_not_allowed and return unless allow_method? :get, :show
55
- @response = @instance
56
- unless @response
57
- @response = { error: 'record_not_found' }
58
- @status = :not_found
59
- end
60
- render_formats
61
- end
62
-
63
- # PUT /resources/:id
64
- def update
65
- head :method_not_allowed and return unless allow_method? :put, :patch, :update
66
- @response = @instance.update_attributes(params[@resource.to_s.underscore])
67
- render_formats
68
- end
69
-
70
- # DELETE /resources/:id
71
- def destroy
72
- head :method_not_allowed and return unless allow_method? :delete, :destroy
73
- @response = @instance.destroy
74
- render_formats
75
- end
76
-
77
- # OPTIONS /resources
78
- def spec
79
- @response = {
80
- supports: self.class.supported_methods,
81
- specification: ( @resource.specification if @resource.respond_to?(:specification) ) || "No Specification"
82
- }
83
- render_formats
84
31
  end
85
32
 
86
33
  private
87
34
 
88
- ## Rendering
89
-
90
- def render_formats
91
- respond_to do |format|
92
- format.json { render json: @response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
93
- format.xml { render xml: @response, status: @status } if allow_format? :xml
94
- format.html { render status: @status } if allow_format? :html
95
- end
96
- end
97
-
98
- # Is a format allowed?
99
-
100
- def allow_format?(*formats)
101
- true if formats.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
102
- end
103
-
104
- # Is a method allowed?
105
-
106
- def allow_method?(*methods)
107
- true if methods.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
108
- end
109
-
110
- def load_resource
111
- @resource = self.class.resource_class
112
- @resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=)
113
- end
114
-
115
- def load_collection
116
- @collection = if self.class.pagination && !params.has_key?(:all)
117
- @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
118
- else
119
- @resource.all
120
- end
121
- eval("@#{self.class.resource_name} = @collection")
122
- end
123
-
124
- def load_instance
125
- @instance = eval "@#{self.class.instance_name} = #{@resource.send(:"find_by_#{self.class.finder}", params[:id])}" if params[:id] && @resource.respond_to?(:find)
126
- end
127
-
128
- def resource
129
- @resource
130
- end
131
-
132
- def instance
133
- @instance
134
- end
135
-
136
- def collection
137
- @collection
35
+ def klass
36
+ self.class
138
37
  end
139
38
 
140
39
  end
@@ -8,6 +8,26 @@ module MakeRestful::ClassMethods
8
8
  resource_name.singularize
9
9
  end
10
10
 
11
+ def supported_methods
12
+ methods ||= []
13
+ methods << { method: :get, path: "/", returns: "collection" } if allow_method?(:list) || allow_method?(:index)
14
+ methods << { method: :post, path: "/", returns: "resource" } if allow_method?(:create) || allow_method?(:post)
15
+ methods << { method: :get, path: "/:id", returns: "resource" } if allow_method?(:show) || allow_method?(:get)
16
+ methods << { method: :put, path: "/:id", returns: "resource" } if allow_method?(:update) || allow_method?(:put)
17
+ methods << { method: :delete, path: "/:id", returns: "success" } if allow_method?(:destroy) || allow_method?(:delete)
18
+ methods
19
+ end
20
+
21
+ # Is a format allowed?
22
+ def allow_format?(*formats)
23
+ true if formats.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
24
+ end
25
+
26
+ # Is a method allowed?
27
+ def allow_method?(*methods)
28
+ true if methods.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
29
+ end
30
+
11
31
  private
12
32
 
13
33
  def resource(klass)
@@ -0,0 +1,25 @@
1
+ module MakeRestful::Loaders
2
+
3
+ def load_resource
4
+ @resource = self.class.resource_class
5
+ @resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=)
6
+ end
7
+
8
+ def load_collection
9
+ @collection = if self.class.pagination && !params.has_key?(:all)
10
+ @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
11
+ else
12
+ @resource.all
13
+ end
14
+ eval("@#{self.class.resource_name} = @collection")
15
+ end
16
+
17
+ def load_instance(opts={})
18
+ if (params[opts[:finder]] || params[:id]) && @resource.respond_to?(:find)
19
+ finder = (opts[:finder] || self.class.finder) == :id ? :find : "find_by_#{(opts[:finder] || self.class.finder)}".to_sym
20
+ @instance = @resource.send finder, (params[opts[:finder]] || params[:id])
21
+ eval "@#{self.class.instance_name} = @instance"
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,14 @@
1
+ module MakeRestful::Rendering
2
+
3
+ private
4
+
5
+ ## Rendering
6
+ def render_formats
7
+ respond_to do |format|
8
+ format.json { render json: @response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
9
+ format.xml { render xml: @response, status: @status } if allow_format? :xml
10
+ format.html { render status: @status } if allow_format? :html
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,68 @@
1
+ module MakeRestful::RestMethods
2
+
3
+ ####################################
4
+ ## RESTful Methods
5
+ ####################################
6
+
7
+ # GET /resources
8
+ def index
9
+ head :method_not_allowed and return unless allow_method? :index, :list
10
+ @response = { :"#{self.class.resource_name}" => @collection }
11
+ @response.merge!(
12
+ {
13
+ pagination: {
14
+ current_page: @collection.current_page,
15
+ per_page: @collection.per_page,
16
+ total_entries: @collection.total_entries,
17
+ total_pages: @collection.total_pages
18
+ }
19
+ }
20
+ ) if self.class.pagination && !params.has_key?(:all)
21
+
22
+ if @collection.empty?
23
+ @response = { error: 'no results found' }
24
+ @status = :not_found
25
+ end
26
+
27
+ render_formats
28
+ end
29
+
30
+ # POST /resources
31
+ def create
32
+ head :method_not_allowed and return unless allow_method? :post, :create
33
+ @response = @resource.create
34
+ render_formats
35
+ end
36
+
37
+ # PUT /resources/:id
38
+ def show
39
+ head :method_not_allowed and return unless allow_method? :get, :show
40
+ @response = @instance
41
+ unless @response
42
+ @response = { error: 'record_not_found' }
43
+ @status = :not_found
44
+ end
45
+ render_formats
46
+ end
47
+
48
+ # PUT /resources/:id
49
+ def update
50
+ head :method_not_allowed and return unless allow_method? :put, :patch, :update
51
+ @response = @instance.update_attributes(params[@resource.to_s.underscore])
52
+ render_formats
53
+ end
54
+
55
+ # DELETE /resources/:id
56
+ def destroy
57
+ head :method_not_allowed and return unless allow_method? :delete, :destroy
58
+ @response = @instance.destroy
59
+ render_formats
60
+ end
61
+
62
+ # OPTIONS /resources
63
+ def spec
64
+ @response = specification
65
+ render_formats
66
+ end
67
+
68
+ end
@@ -0,0 +1,25 @@
1
+ module MakeRestful::Specification
2
+ extend ActiveSupport::Concern
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Builder
6
+ # autoload :MethodBuilder
7
+
8
+ def specification
9
+ nil
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def specification(&block)
15
+
16
+ spec_result = MakeRestful::Specification::Builder.new(self, &block).freeze
17
+
18
+ define_method :specification do
19
+ spec_result.render
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,67 @@
1
+ class MakeRestful::Specification::Builder
2
+
3
+ attr_reader :controller, :_methods, :_description, :_formats, :_attributes, :_resource, :_default_methods, :_default_attributes, :_default_formats
4
+
5
+ def initialize(controller, &block)
6
+ @_controller = controller
7
+ @_methods = []
8
+ @_default_methods = controller.supported_methods.collect do |m|
9
+ method m[:method], path: m[:path], returns: m[:returns]
10
+ end
11
+ @_methods = []
12
+ @_attributes = []
13
+ @_default_attributes = controller.resource_class.new.attributes.keys
14
+ @_default_formats = controller.allowed_formats
15
+ @_formats = []
16
+ @_description = "Api for #{controller.resource_name.titleize}"
17
+ @_resource = controller.instance_name
18
+ instance_eval(&block) if block_given?
19
+ end
20
+
21
+ def method(method, options={})
22
+ method = {
23
+ method: method,
24
+ path: options[:path],
25
+ returns: options[:returns]
26
+ }
27
+ @_methods << method
28
+ method
29
+ end
30
+
31
+ def description(string)
32
+ @_description = string
33
+ end
34
+
35
+ def resource(string)
36
+ @_resource = string
37
+ end
38
+
39
+ def attribute(attr)
40
+ @_attributes << attr
41
+ attr
42
+ end
43
+
44
+ def attributes(*args)
45
+ @_attributes += args.flatten
46
+ end
47
+
48
+ def format(attr)
49
+ @_formats << attr
50
+ attr
51
+ end
52
+
53
+ def formats(*args)
54
+ @_formats += args
55
+ end
56
+
57
+ def render
58
+ {
59
+ resource: _resource,
60
+ formats: _formats.present? ? _formats : _default_formats,
61
+ description: _description,
62
+ attributes: _attributes.present? ? _attributes : _default_attributes,
63
+ methods: _methods.present? ? _methods : _default_methods
64
+ }
65
+ end
66
+
67
+ end
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/make_restful.gemspec CHANGED
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
  gem.add_dependency "activesupport"
20
20
  gem.add_dependency "will_paginate"
21
+ gem.add_dependency "sourcify"
21
22
 
22
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_restful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
12
+ date: 2012-09-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sourcify
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description: ! ' Enables rest on a resource '
47
63
  email:
48
64
  - jason@waldrip.net
@@ -59,6 +75,11 @@ files:
59
75
  - lib/make_restful/class_methods.rb
60
76
  - lib/make_restful/controller_additions.rb
61
77
  - lib/make_restful/controller_additions/oauth_resource.rb
78
+ - lib/make_restful/loaders.rb
79
+ - lib/make_restful/rendering.rb
80
+ - lib/make_restful/rest_methods.rb
81
+ - lib/make_restful/specification.rb
82
+ - lib/make_restful/specification/builder.rb
62
83
  - lib/make_restful/version.rb
63
84
  - make_restful.gemspec
64
85
  homepage: http://github.com/jwaldrip/make_restful