ar_http_wrapper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
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
@@ -7,6 +7,10 @@ class ActiveRecordHttpWrapper
7
7
  @id = args[:id]
8
8
  end
9
9
 
10
+ def self.build
11
+ get(path("/new"))
12
+ end
13
+
10
14
  def self.create(args={})
11
15
  options = { :body => args[:params] }
12
16
  post(path("/"), options)
@@ -1,4 +1,5 @@
1
1
  require 'httparty'
2
+ require 'grape'
2
3
  require "ar_http_wrapper/engine"
3
4
 
4
5
  module ArHttpWrapper
@@ -6,6 +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
12
  end
10
13
  end
11
14
  end
@@ -1,3 +1,3 @@
1
1
  module ArHttpWrapper
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
4
+ version: 0.0.2
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-12-12 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: grape
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
  - !ruby/object:Gem::Dependency
47
63
  name: mysql2
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -163,7 +179,7 @@ executables: []
163
179
  extensions: []
164
180
  extra_rdoc_files: []
165
181
  files:
166
- - app/interfaces/active_record_api_wrapper.rb
182
+ - app/api/active_record_api_wrapper.rb
167
183
  - app/interfaces/active_record_http_wrapper.rb
168
184
  - config/routes.rb
169
185
  - lib/ar_http_wrapper/engine.rb
@@ -1,2 +0,0 @@
1
- class ActiveRecordApiWrapper
2
- end