grid_rest 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,8 @@
1
+ = CHANGELOG
2
+
3
+ == 2011-06-08 v0.0.2
4
+ * Initialization fixes
5
+ * Setting default parameters
6
+
7
+ == 2011-06-07 v0.0.1
8
+ * Initial release
@@ -9,7 +9,7 @@ These configurations should be put in <tt>config/grid_rest.yml</tt>:
9
9
  === General REST interface
10
10
  include_in:
11
11
  - ActiveRecord::Base
12
- - ApplicationController
12
+ - ActionController::Base
13
13
  extend: ActiveRecord::Base
14
14
  development:
15
15
  url_prefix: '/api/1.0'
@@ -32,7 +32,7 @@ These configurations should be put in <tt>config/grid_rest.yml</tt>:
32
32
  === Multiple REST interfaces
33
33
  include_in:
34
34
  - ActiveRecord::Base
35
- - ApplicationController
35
+ - ActionController::Base
36
36
  extend: ActiveRecord::Base
37
37
  development:
38
38
  zieook_workflow:
@@ -72,7 +72,7 @@ These configurations should be put in <tt>config/grid_rest.yml</tt>:
72
72
  === Other ORM
73
73
  include_in:
74
74
  - MyOrm::Base
75
- - ApplicationController
75
+ - ActionController::Base
76
76
  extend: MyOrm::Base
77
77
  ....
78
78
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{grid_rest}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Benjamin ter Kuile"]
12
+ s.date = %q{2011-06-08}
13
+ s.description = %q{grid_rest is a gem that makes your rails application interact with other backends through a REST interface. It is build in a situation where one rails application was talking to 2 java backends.
14
+ The main development focus is JSON communication, but XML should be just as easy}
15
+ s.email = %q{bterkuile@gmail.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "CHANGELOG.rdoc",
23
+ "Gemfile",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "grid_rest.gemspec",
29
+ "lib/grid_rest.rb",
30
+ "lib/grid_rest/engine.rb",
31
+ "test/helper.rb",
32
+ "test/test_grid_rest.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/bterkuile/grid_rest}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{grid_rest is a gem that makes your rails application interact with other backends through a REST interface.}
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.7"])
46
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<activesupport>, [">= 3.0.7"])
53
+ s.add_dependency(%q<rest-client>, [">= 0"])
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
57
+ s.add_dependency(%q<rcov>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<activesupport>, [">= 3.0.7"])
61
+ s.add_dependency(%q<rest-client>, [">= 0"])
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -1,8 +1,9 @@
1
1
  require 'rest_client'
2
2
  require 'active_support/all'
3
- require 'grid_rest/railtie'
3
+ require 'grid_rest/engine'
4
4
  module GridRest
5
- mattr_accessor :grid_config, :log_file
5
+ mattr_accessor :grid_config, :log_file, :additional_parameters
6
+ self.additional_parameters = { :default => {:global => {}, :get => {}, :post => {}, :put => {}, :delete => {}} }
6
7
  class GridConfig < HashWithIndifferentAccess
7
8
  # This allows for method like calling of the configuration. For example:
8
9
  # GridRest.grid_config.host
@@ -14,6 +15,7 @@ module GridRest
14
15
  end
15
16
  end
16
17
  self.grid_config = GridConfig.new
18
+
17
19
  def self.include_in(klass)
18
20
  klass.send(:include, GridRestExtensions)
19
21
  self.grid_config.namespaces.keys.each do |k|
@@ -41,6 +43,10 @@ module GridRest
41
43
  def #{namespace}_delete(url, rparams = {})
42
44
  grid_rest_delete(url, rparams.merge(:grid_rest_namespace => '#{namespace}'))
43
45
  end
46
+ def #{namespace}_default_parameters(params, request_types = :global)
47
+ set_namespaced_default_grid_rest_parameters(:#{namespace}, params, request_types)
48
+ end
49
+ alias_method :set_#{namespace}_default_parameters, :#{namespace}_default_parameters
44
50
  END
45
51
  end
46
52
 
@@ -89,6 +95,50 @@ module GridRest
89
95
  @current_namespace
90
96
  end
91
97
 
98
+ # Global setter valid for all request/namespaces
99
+ def default_grid_rest_parameters(params, request_types = :global)
100
+ set_namespaced_default_grid_rest_parameters(:default, params, request_types)
101
+ end
102
+ alias_method :set_default_grid_rest_parameters, :default_grid_rest_parameters
103
+
104
+ # Return an array of additional parameters. This request is namespace aware
105
+ # It will return an array given the additional default parameters for:
106
+ # ALL, GET, POST, PUT and DELETE
107
+ # requests in that order
108
+ def get_additional_grid_rest_parameters
109
+ globals = get_request_specific_additional_grid_rest_parameters(:global)
110
+ return [
111
+ globals,
112
+ globals.merge(get_request_specific_additional_grid_rest_parameters(:get)),
113
+ globals.merge(get_request_specific_additional_grid_rest_parameters(:post)),
114
+ globals.merge(get_request_specific_additional_grid_rest_parameters(:put)),
115
+ globals.merge(get_request_specific_additional_grid_rest_parameters(:delete))
116
+ ]
117
+ end
118
+
119
+ # Get all the global parameters. The defaults, extended/overwritten by the
120
+ # namespace specific ones
121
+ def get_request_specific_additional_grid_rest_parameters(request = :global)
122
+ if current_namespace
123
+ additional_grid_rest_parameters[:default][request].merge(additional_grid_rest_parameters[current_namespace.to_sym][request] || {})
124
+ else
125
+ additional_grid_rest_parameters[:default][request]
126
+ end
127
+ end
128
+
129
+ # Getter of additional parameters. Contains defaults and namespace specific versions
130
+ def additional_grid_rest_parameters
131
+ GridRest.additional_parameters
132
+ end
133
+
134
+ def set_namespaced_default_grid_rest_parameters(ns, params, request_types = :global)
135
+ additional_grid_rest_parameters[ns] ||= {}
136
+ for request_type in Array.wrap(request_types).map(&:to_sym)
137
+ additional_grid_rest_parameters[ns][request_type] ||= {}
138
+ additional_grid_rest_parameters[ns][request_type].update(params)
139
+ end
140
+ end
141
+
92
142
  # Wrapper for grid_rest_log_message to write a log message in a consitant manner given the request parameters
93
143
  def grid_rest_log(method, url, rparams = {}, emsg = "")
94
144
  if current_namespace
@@ -114,48 +164,55 @@ module GridRest
114
164
  format = rparams.delete(:format) || {:get => :json, :post => :json, :put => :json}[method]
115
165
  accept = get_accept_header(format)
116
166
  @current_namespace = rparams.delete(:grid_rest_namespace) # Remove this setting from request parameters
167
+ additional_get_parameters, additional_post_parameters, additional_put_parameters, additional_delete_parameters = get_additional_grid_rest_parameters
117
168
  begin
118
169
  r = benchmark "Fetching #{method.to_s.upcase} #{relative_url} #{rparams.inspect}", :level => :debug do
119
170
  case method
120
- when :get then RestClient.get rest_url, :params => rparams, :accept => accept
171
+ when :get then RestClient.get rest_url, :params => rparams.update(additional_get_parameters), :accept => accept
121
172
  when :post then
122
173
  if rparams[:json_data]
123
- RestClient.post rest_url, rparams[:json_data].is_a?(Hash) ? rparams[:json_data].to_json : rparams[:json_data], :content_type => :json, :accept => :json
174
+ rparams[:json_data] = rparams[:json_data].merge(additional_post_parameters).to_json if rparams[:json_data].is_a?(Hash)
175
+ RestClient.post rest_url, rparams[:json_data], :content_type => :json, :accept => :json
124
176
  elsif rparams[:xml_data]
125
- RestClient.post rest_url, rparams[:xml_data].is_a?(Hash) ? rparams[:xml_data].to_xml : rparams[:xml_data], :content_type => :xml, :accept => :xml
177
+ rparams[:xml_data] = rparams[:xml_data].merge(additional_post_parameters).to_xml if rparams[:xml_data].is_a?(Hash)
178
+ RestClient.post rest_url, rparams[:xml_data], :content_type => :xml, :accept => :xml
126
179
  elsif rparams[:binary]
127
180
  RestClient.post rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
128
181
  else
129
182
  rparams[:headers] ||= {}
130
183
  rparams[:headers][:accept] = accept
131
184
  rparams[:multipart] = true
132
- RestClient.post rest_url, rparams
185
+ RestClient.post rest_url, rparams.update(additional_post_parameters)
133
186
  end
134
- when :put then
187
+ when :put then
135
188
  if rparams[:json_data]
136
- RestClient.put rest_url, rparams[:json_data].is_a?(Hash) ? rparams[:json_data].to_json : rparams[:json_data], :content_type => :json, :accept => :json
189
+ rparams[:json_data] = rparams[:json_data].merge(additional_put_parameters).to_json if rparams[:json_data].is_a?(Hash)
190
+ RestClient.put rest_url, rparams[:json_data], :content_type => :json, :accept => :json
137
191
  elsif rparams[:xml_data]
138
- RestClient.put rest_url, rparams[:xml_data].is_a?(Hash) ? rparams[:xml_data].to_xml : rparams[:xml_data], :content_type => :xml, :accept => :xml
192
+ rparams[:xml_data] = rparams[:xml_data].merge(additional_put_parameters).to_xml if rparams[:xml_data].is_a?(Hash)
193
+ RestClient.put rest_url, rparams[:xml_data], :content_type => :xml, :accept => :xml
139
194
  elsif rparams[:binary]
140
195
  RestClient.put rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
141
196
  else
142
197
  rparams[:headers] ||= {}
143
198
  rparams[:headers][:accept] = accept
144
199
  rparams[:multipart] = true
145
- RestClient.put rest_url, rparams
200
+ RestClient.put rest_url, rparams.update(additional_put_parameters)
146
201
  end
147
202
  when :delete then
148
203
  if rparams[:json_data]
149
- RestClient.delete rest_url, rparams[:json_data].is_a?(Hash) ? rparams[:json_data].to_json : rparams[:json_data], :content_type => :json, :accept => :json
204
+ rparams[:json_data] = rparams[:json_data].merge(additional_delete_parameters).to_json if rparams[:json_data].is_a?(Hash)
205
+ RestClient.delete rest_url, rparams[:json_data], :content_type => :json, :accept => :json
150
206
  elsif rparams[:xml_data]
151
- RestClient.delete rest_url, rparams[:xml_data].is_a?(Hash) ? rparams[:xml_data].to_xml : rparams[:xml_data], :content_type => :xml, :accept => :xml
207
+ rparams[:xml_data] = rparams[:xml_data].merge(additional_delete_parameters).to_xml if rparams[:xml_data].is_a?(Hash)
208
+ RestClient.delete rest_url, rparams[:xml_data], :content_type => :xml, :accept => :xml
152
209
  elsif rparams[:binary]
153
210
  RestClient.delete rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
154
211
  else
155
212
  rparams[:headers] ||= {}
156
213
  rparams[:headers][:accept] = accept
157
214
  rparams[:multipart] = true
158
- RestClient.delete rest_url, :params => rparams
215
+ RestClient.delete rest_url, rparams.update(additional_delete_parameters)
159
216
  end
160
217
  else
161
218
  raise "No proper method (#{method}) for a grid_rest_request call"
@@ -240,7 +297,9 @@ module GridRest
240
297
  false
241
298
  end
242
299
 
243
- def try(*args)
300
+ def try(m, *args)
301
+ return send(m, *args) if respond_to?(m)
302
+ # Behave like a nil object otherwise
244
303
  nil
245
304
  end
246
305
 
@@ -266,7 +325,9 @@ module GridRest
266
325
  def valid?
267
326
  false
268
327
  end
269
- def try(*args)
328
+ def try(m, *args)
329
+ return send(m, *args) if respond_to?(m)
330
+ # Behave like a nil object otherwise
270
331
  nil
271
332
  end
272
333
  end
@@ -0,0 +1,17 @@
1
+ module GridRest
2
+ class Engine < Rails::Engine
3
+ #rake_tasks do
4
+ # load "tasks/tiny_mce.rake"
5
+ #end
6
+ config.grid_rest = GridRest
7
+
8
+ initializer "grid_rest.load_config" do |app|
9
+ app.config.after_initialize do
10
+ GridRest.load_config!
11
+ end
12
+ #ActiveSupport.on_load(:action_controller) do
13
+ # GridRest.include_in(self)
14
+ #end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Benjamin ter Kuile
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-07 00:00:00 +02:00
17
+ date: 2011-06-08 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -108,13 +108,15 @@ extra_rdoc_files:
108
108
  - README.rdoc
109
109
  files:
110
110
  - .document
111
+ - CHANGELOG.rdoc
111
112
  - Gemfile
112
113
  - LICENSE.txt
113
114
  - README.rdoc
114
115
  - Rakefile
115
116
  - VERSION
117
+ - grid_rest.gemspec
116
118
  - lib/grid_rest.rb
117
- - lib/grid_rest/railtie.rb
119
+ - lib/grid_rest/engine.rb
118
120
  - test/helper.rb
119
121
  - test/test_grid_rest.rb
120
122
  has_rdoc: true
@@ -1,13 +0,0 @@
1
- module GridRest
2
- class Railtie < Rails::Railtie
3
- #rake_tasks do
4
- # load "tasks/tiny_mce.rake"
5
- #end
6
-
7
- initializer :grid_rest do |app|
8
- app.config.after_initialize do
9
- GridRest.load_config!
10
- end
11
- end
12
- end
13
- end