ar_http_wrapper 0.0.2 → 0.0.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.
@@ -1,6 +1,8 @@
1
1
  require 'httparty'
2
2
  require 'grape'
3
3
  require "ar_http_wrapper/engine"
4
+ require "ar_http_wrapper/active_record_http_wrapper"
5
+ require "ar_http_wrapper/active_record_api_wrapper"
4
6
 
5
7
  module ArHttpWrapper
6
- end
8
+ end
@@ -0,0 +1,63 @@
1
+ module ActiveRecordApiWrapper
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def api(api_class)
6
+ klass = self
7
+ klass_name = self.to_s.underscore
8
+
9
+ api_class.resource :"#{klass_name}" do
10
+ api_class.desc "Builds a record."
11
+ api_class.get "/new" do
12
+ klass.new
13
+ end
14
+
15
+ api_class.desc "Creates a record."
16
+ api_class.params do
17
+ requires klass.to_s.underscore.to_sym, :type => Hash, :desc => "The new record attributes."
18
+ end
19
+ api_class.post "/" do
20
+ klass.create(params[klass.to_s.underscore.to_sym])
21
+ end
22
+
23
+ api_class.desc "Updates a record."
24
+ api_class.params do
25
+ requires :id, :desc => "The record id."
26
+ requires klass.to_s.underscore.to_sym, :type => Hash, :desc => "The updated record attributes."
27
+ end
28
+ api_class.put "/:id" do
29
+ updated_attr = params[klass.to_s.underscore.to_sym]
30
+ instance = klass.find_by_id(params[:id])
31
+ instance.try(:update_attributes,updated_attr)
32
+ end
33
+
34
+ api_class.desc "Shows a record."
35
+ api_class.params do
36
+ requires :id, :desc => "The record id."
37
+ end
38
+ api_class.get "/:id" do
39
+ klass.find_by_id(params[:id])
40
+ end
41
+
42
+ api_class.desc "Destroys a record."
43
+ api_class.params do
44
+ requires :id, :desc => "The record id."
45
+ end
46
+ api_class.delete "/:id" do
47
+ instance = klass.find_by_id(params[:id])
48
+ instance.try(:destroy)
49
+ end
50
+
51
+ api_class.desc "Collects records."
52
+ api_class.get "/" do
53
+ conditions = params[:conditions]
54
+ offset = params[:offset]
55
+ limit = params[:limit]
56
+ records = klass.find(:all, :conditions => conditions, :offset => offset, :limit => limit)
57
+ params[:count] ? records.count : records
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ ActiveRecord::Base.send(:include,ActiveRecordApiWrapper)
@@ -6,9 +6,9 @@ module ArHttpWrapper
6
6
  app.config.autoload_paths += Dir[Rails.root.join('app', 'interfaces')]
7
7
  app.config.autoload_paths += Dir[Rails.root.join('app', 'interfaces', '**')]
8
8
  app.config.autoload_paths += Dir[Rails.root.join('app', 'interfaces', '**', '**')]
9
- app.config.autoload_paths += Dir[Rails.root.join('app', 'api')]
10
- app.config.autoload_paths += Dir[Rails.root.join('app', 'api', '**')]
11
- app.config.autoload_paths += Dir[Rails.root.join('app', 'api', '**', '**')]
9
+ app.config.autoload_paths += Dir[Rails.root.join('app', 'apis')]
10
+ app.config.autoload_paths += Dir[Rails.root.join('app', 'apis', '**')]
11
+ app.config.autoload_paths += Dir[Rails.root.join('app', 'apis', '**', '**')]
12
12
  end
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module ArHttpWrapper
2
- VERSION = "0.0.2"
3
- end
2
+ VERSION = "0.0.3"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_http_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -179,9 +179,9 @@ executables: []
179
179
  extensions: []
180
180
  extra_rdoc_files: []
181
181
  files:
182
- - app/api/active_record_api_wrapper.rb
183
- - app/interfaces/active_record_http_wrapper.rb
184
182
  - config/routes.rb
183
+ - lib/ar_http_wrapper/active_record_api_wrapper.rb
184
+ - lib/ar_http_wrapper/active_record_http_wrapper.rb
185
185
  - lib/ar_http_wrapper/engine.rb
186
186
  - lib/ar_http_wrapper/version.rb
187
187
  - lib/ar_http_wrapper.rb
@@ -1,70 +0,0 @@
1
- class ActiveRecordApiWrapper
2
-
3
- attr_accessor :klass, :klass_name
4
-
5
- def initialize(args = {})
6
- @klass = args[:klass]
7
- @klass_name = @klass.try(:to_s).try(:underscore)
8
- end
9
-
10
- def api(api_class)
11
- klass = self.klass
12
- klass_name = self.klass_name
13
-
14
- api_class.resource :"#{klass_name}" do
15
- api_class.desc "Builds a record."
16
- api_class.get "/new" do
17
- klass.new
18
- end
19
-
20
- api_class.desc "Creates a record."
21
- api_class.params do
22
- requires klass.to_s.underscore.to_sym, :type => Hash, :desc => "The new record attributes."
23
- end
24
- api_class.post "/" do
25
- klass.create(params[klass.to_s.underscore.to_sym])
26
- end
27
-
28
- api_class.desc "Updates a record."
29
- api_class.params do
30
- requires :id, :desc => "The record id."
31
- requires klass.to_s.underscore.to_sym, :type => Hash, :desc => "The updated record attributes."
32
- end
33
- api_class.put "/:id" do
34
- updated_attr = params[klass.to_s.underscore.to_sym]
35
- instance = klass.find_by_id(params[:id])
36
- instance.try(:update_attributes,updated_attr)
37
- end
38
-
39
- api_class.desc "Shows a record."
40
- api_class.params do
41
- requires :id, :desc => "The record id."
42
- end
43
- api_class.get "/:id" do
44
- klass.find_by_id(params[:id])
45
- end
46
-
47
- api_class.desc "Destroys a record."
48
- api_class.params do
49
- requires :id, :desc => "The record id."
50
- end
51
- api_class.delete "/:id" do
52
- instance = klass.find_by_id(params[:id])
53
- instance.try(:destroy)
54
- end
55
-
56
- api_class.desc "Collects records."
57
- api_class.get "/" do
58
- conditions = params[:conditions]
59
- offset = params[:offset]
60
- limit = params[:limit]
61
- records = klass.find(:all, :conditions => conditions, :offset => offset, :limit => limit)
62
- params[:count] ? records.count : records
63
- end
64
- end
65
- end
66
-
67
- def self.api_for(_klass,api_class)
68
- new({:klass => _klass}).api(api_class)
69
- end
70
- end