grid_rest 0.0.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/.document +6 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +92 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/grid_rest/railtie.rb +13 -0
- data/lib/grid_rest.rb +306 -0
- data/test/helper.rb +18 -0
- data/test/test_grid_rest.rb +7 -0
- metadata +151 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
gem "activesupport", ">= 3.0.7"
|
6
|
+
gem "rest-client"
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "shoulda", ">= 0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.6.0"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Benjamin ter Kuile
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= grid_rest
|
2
|
+
|
3
|
+
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.
|
4
|
+
The main development focus is JSON communication, but XML should be just as easy.
|
5
|
+
|
6
|
+
== Example configurations
|
7
|
+
These configurations should be put in <tt>config/grid_rest.yml</tt>:
|
8
|
+
|
9
|
+
=== General REST interface
|
10
|
+
include_in:
|
11
|
+
- ActiveRecord::Base
|
12
|
+
- ApplicationController
|
13
|
+
extend: ActiveRecord::Base
|
14
|
+
development:
|
15
|
+
url_prefix: '/api/1.0'
|
16
|
+
host: localhost
|
17
|
+
port: 20400
|
18
|
+
logging: true
|
19
|
+
|
20
|
+
production:
|
21
|
+
url_prefix: '/api/1.0'
|
22
|
+
host: localhost
|
23
|
+
port: 20400
|
24
|
+
logging: true
|
25
|
+
|
26
|
+
test:
|
27
|
+
url_prefix: '/api/1.0'
|
28
|
+
host: localhost
|
29
|
+
port: 20400
|
30
|
+
logging: true
|
31
|
+
|
32
|
+
=== Multiple REST interfaces
|
33
|
+
include_in:
|
34
|
+
- ActiveRecord::Base
|
35
|
+
- ApplicationController
|
36
|
+
extend: ActiveRecord::Base
|
37
|
+
development:
|
38
|
+
zieook_workflow:
|
39
|
+
url_prefix: "/api/1.0/"
|
40
|
+
host: localhost
|
41
|
+
port: 20200
|
42
|
+
logging: true
|
43
|
+
zieook_data:
|
44
|
+
url_prefix: "/api/1.0"
|
45
|
+
host: localhost
|
46
|
+
port: 20100
|
47
|
+
logging: true
|
48
|
+
|
49
|
+
production:
|
50
|
+
zieook_workflow:
|
51
|
+
url_prefix: "/api/1.0/"
|
52
|
+
host: localhost
|
53
|
+
port: 20200
|
54
|
+
logging: true
|
55
|
+
zieook_data:
|
56
|
+
url_prefix: "/api/1.0"
|
57
|
+
host: localhost
|
58
|
+
port: 20100
|
59
|
+
logging: false
|
60
|
+
|
61
|
+
test:
|
62
|
+
zieook_workflow:
|
63
|
+
url_prefix: "/api/1.0/"
|
64
|
+
host: localhost
|
65
|
+
port: 20200
|
66
|
+
logging: true
|
67
|
+
zieook_data:
|
68
|
+
url_prefix: "/api/1.0"
|
69
|
+
host: localhost
|
70
|
+
port: 20100
|
71
|
+
logging: true
|
72
|
+
=== Other ORM
|
73
|
+
include_in:
|
74
|
+
- MyOrm::Base
|
75
|
+
- ApplicationController
|
76
|
+
extend: MyOrm::Base
|
77
|
+
....
|
78
|
+
|
79
|
+
== Contributing to grid_rest
|
80
|
+
|
81
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
82
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
83
|
+
* Fork the project
|
84
|
+
* Start a feature/bugfix branch
|
85
|
+
* Commit and push until you are happy with your contribution
|
86
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
87
|
+
|
88
|
+
== Copyright
|
89
|
+
|
90
|
+
Copyright (c) 2011 GridLine. See LICENSE.txt for
|
91
|
+
further details.
|
92
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "grid_rest"
|
18
|
+
gem.homepage = "http://github.com/bterkuile/grid_rest"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{grid_rest is a gem that makes your rails application interact with other backends through a REST interface.}
|
21
|
+
gem.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.
|
22
|
+
The main development focus is JSON communication, but XML should be just as easy}
|
23
|
+
gem.email = "bterkuile@gmail.com"
|
24
|
+
gem.authors = ["Benjamin ter Kuile"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
test.rcov_opts << '--exclude "gems/*"'
|
42
|
+
end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "grid_rest #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/grid_rest.rb
ADDED
@@ -0,0 +1,306 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'active_support/all'
|
3
|
+
require 'grid_rest/railtie'
|
4
|
+
module GridRest
|
5
|
+
mattr_accessor :grid_config, :log_file
|
6
|
+
class GridConfig < HashWithIndifferentAccess
|
7
|
+
# This allows for method like calling of the configuration. For example:
|
8
|
+
# GridRest.grid_config.host
|
9
|
+
# or
|
10
|
+
# GridRest.grid_config.namespaces
|
11
|
+
def method_missing(m, *args)
|
12
|
+
return self.send('[]=', m.to_s.chop, args.first) if m.to_s.last == '=' && args.size == 1
|
13
|
+
return self.send('[]', m)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
self.grid_config = GridConfig.new
|
17
|
+
def self.include_in(klass)
|
18
|
+
klass.send(:include, GridRestExtensions)
|
19
|
+
self.grid_config.namespaces.keys.each do |k|
|
20
|
+
klass.send(:class_eval, namespace_methods(k))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.extend_class(klass)
|
25
|
+
klass.send(:extend, GridRestExtensions)
|
26
|
+
self.grid_config.namespaces.keys.each do |k|
|
27
|
+
klass.send(:class_eval, "class << self; #{namespace_methods(k)}; end")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def self.namespace_methods(namespace)
|
31
|
+
expand_str = <<-END
|
32
|
+
def #{namespace}_get(url, rparams = {})
|
33
|
+
grid_rest_get(url, rparams.merge(:grid_rest_namespace => '#{namespace}'))
|
34
|
+
end
|
35
|
+
def #{namespace}_post(url, rparams = {})
|
36
|
+
grid_rest_post(url, rparams.merge(:grid_rest_namespace => '#{namespace}'))
|
37
|
+
end
|
38
|
+
def #{namespace}_put(url, rparams = {})
|
39
|
+
grid_rest_put(url, rparams.merge(:grid_rest_namespace => '#{namespace}'))
|
40
|
+
end
|
41
|
+
def #{namespace}_delete(url, rparams = {})
|
42
|
+
grid_rest_delete(url, rparams.merge(:grid_rest_namespace => '#{namespace}'))
|
43
|
+
end
|
44
|
+
END
|
45
|
+
end
|
46
|
+
|
47
|
+
# Very important method. This will set the appropriate settings for the current
|
48
|
+
# rails environment and appends this system to the specified classes
|
49
|
+
def self.load_config!
|
50
|
+
config_path = File.join(Rails.root, 'config', 'grid_rest.yml')
|
51
|
+
raise "File #{config_path} does not exist" unless File.exist?(config_path)
|
52
|
+
h = YAML::load_file(config_path) rescue nil
|
53
|
+
raise "Config file #{config_path} could not be parsed" unless h
|
54
|
+
raise "Config file #{config_path} needs an environment" unless h[Rails.env]
|
55
|
+
self.grid_config.namespaces = HashWithIndifferentAccess.new unless self.grid_config.namespaces.is_a?(Hash)
|
56
|
+
h[Rails.env].each do |k, v|
|
57
|
+
if v.is_a?(Hash)
|
58
|
+
self.grid_config.namespaces[k.to_sym] = HashWithIndifferentAccess.new(v)
|
59
|
+
else
|
60
|
+
self.grid_config[k.to_sym] = v
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Now extend/include in configured classes
|
65
|
+
for inclusion in Array.wrap(h['include_in'])
|
66
|
+
self.include_in inclusion.constantize
|
67
|
+
end
|
68
|
+
for extension in Array.wrap(h['extend'])
|
69
|
+
self.extend_class extension.constantize
|
70
|
+
end
|
71
|
+
end
|
72
|
+
module GridRestExtensions
|
73
|
+
include ActiveSupport::Benchmarkable
|
74
|
+
unless respond_to?(:logger)
|
75
|
+
def logger
|
76
|
+
Rails.logger
|
77
|
+
end
|
78
|
+
end
|
79
|
+
# This one is on the deprication list
|
80
|
+
def grid_rest?(rparams = {})
|
81
|
+
ns = rparams[:grid_rest_namespace].present? ? "grid_rest_#{rparams[:grid_rest_namespace]}" : 'grid_rest'
|
82
|
+
raise "APP_CONFIG not available, make sure that config/config.yml and config/initializers/load_config.rb are available" unless defined?(APP_CONFIG)
|
83
|
+
raise "Namespace #{ns} not available in config/config.yml" unless APP_CONFIG[ns]
|
84
|
+
@grid_rest_active ||= Net::HTTP.new(APP_CONFIG[ns]['host'], APP_CONFIG[ns]['port']).start rescue nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# This will get the current namespace or nil during grid_rest_request processing
|
88
|
+
def current_namespace
|
89
|
+
@current_namespace
|
90
|
+
end
|
91
|
+
|
92
|
+
# Wrapper for grid_rest_log_message to write a log message in a consitant manner given the request parameters
|
93
|
+
def grid_rest_log(method, url, rparams = {}, emsg = "")
|
94
|
+
if current_namespace
|
95
|
+
return unless GridRest.grid_config.namespaces[current_namespace]['logging']
|
96
|
+
else
|
97
|
+
return unless GridRest.grid_config['logging']
|
98
|
+
end
|
99
|
+
grid_rest_log_message(rparams.any? ? "#{Time.now.to_s(:db)} #{method.to_s.upcase} #{url} with #{rparams.inspect} #{emsg}" : "#{Time.now.to_s(:db)} #{method.to_s.upcase} #{url} #{emsg}")
|
100
|
+
end
|
101
|
+
|
102
|
+
# Write msg to the log file. Should only be called from grid_rest_log unless you know what you are doing
|
103
|
+
def grid_rest_log_message(msg)
|
104
|
+
GridRest.log_file ||= File.open(File.join(Rails.root, 'log', "#{Rails.env}_grid_rest.log"), 'a+')
|
105
|
+
GridRest.log_file.puts msg
|
106
|
+
GridRest.log_file.flush unless Rails.env == 'production'
|
107
|
+
end
|
108
|
+
|
109
|
+
def grid_rest_request(method, relative_url, rparams = {})
|
110
|
+
#return DummyResponse.new # test return
|
111
|
+
rest_url = generate_url(relative_url, rparams)
|
112
|
+
#return Error.new('unavailable', :url => rest_url) unless grid_rest?(rparams)
|
113
|
+
# Specify defaults per method for format
|
114
|
+
format = rparams.delete(:format) || {:get => :json, :post => :json, :put => :json}[method]
|
115
|
+
accept = get_accept_header(format)
|
116
|
+
@current_namespace = rparams.delete(:grid_rest_namespace) # Remove this setting from request parameters
|
117
|
+
begin
|
118
|
+
r = benchmark "Fetching #{method.to_s.upcase} #{relative_url} #{rparams.inspect}", :level => :debug do
|
119
|
+
case method
|
120
|
+
when :get then RestClient.get rest_url, :params => rparams, :accept => accept
|
121
|
+
when :post then
|
122
|
+
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
|
124
|
+
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
|
126
|
+
elsif rparams[:binary]
|
127
|
+
RestClient.post rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
|
128
|
+
else
|
129
|
+
rparams[:headers] ||= {}
|
130
|
+
rparams[:headers][:accept] = accept
|
131
|
+
rparams[:multipart] = true
|
132
|
+
RestClient.post rest_url, rparams
|
133
|
+
end
|
134
|
+
when :put then
|
135
|
+
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
|
137
|
+
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
|
139
|
+
elsif rparams[:binary]
|
140
|
+
RestClient.put rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
|
141
|
+
else
|
142
|
+
rparams[:headers] ||= {}
|
143
|
+
rparams[:headers][:accept] = accept
|
144
|
+
rparams[:multipart] = true
|
145
|
+
RestClient.put rest_url, rparams
|
146
|
+
end
|
147
|
+
when :delete then
|
148
|
+
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
|
150
|
+
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
|
152
|
+
elsif rparams[:binary]
|
153
|
+
RestClient.delete rest_url, rparams[:binary], :content_type => 'binary/octet-stream'
|
154
|
+
else
|
155
|
+
rparams[:headers] ||= {}
|
156
|
+
rparams[:headers][:accept] = accept
|
157
|
+
rparams[:multipart] = true
|
158
|
+
RestClient.delete rest_url, :params => rparams
|
159
|
+
end
|
160
|
+
else
|
161
|
+
raise "No proper method (#{method}) for a grid_rest_request call"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
grid_rest_log method, rest_url, rparams, "response code: #{r.code}"
|
165
|
+
if format == :json
|
166
|
+
#r = benchmark("decoding response JSON", :level => :debug ){ ActiveSupport::JSON.decode(r.body) rescue r } # Slow
|
167
|
+
r = benchmark("decoding response JSON", :level => :debug ){ JSON.parse(r.body) rescue r }
|
168
|
+
end
|
169
|
+
# Singleton class extensions
|
170
|
+
def r.valid?
|
171
|
+
true
|
172
|
+
end
|
173
|
+
rescue RestClient::ResourceNotFound => e
|
174
|
+
r = Error.new('resource not found', :url => rest_url, :code => e.http_code, :method => method)
|
175
|
+
grid_rest_log method, rest_url, rparams, "resource not found response"
|
176
|
+
rescue Errno::ECONNREFUSED
|
177
|
+
r = Error.new('connection refused', :url => rest_url, :code => 404, :method => method)
|
178
|
+
grid_rest_log method, rest_url, rparams, "connection refused response"
|
179
|
+
rescue => e
|
180
|
+
r = Error.new "general", :url => rest_url, :code => e.respond_to?(:http_code) ? e.http_code : 500, :method => method
|
181
|
+
grid_rest_log method, rest_url, rparams, "error in request"
|
182
|
+
end
|
183
|
+
r
|
184
|
+
end
|
185
|
+
|
186
|
+
def generate_url(url, rparams = {})
|
187
|
+
host = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'host') || GridRest.grid_config['host']
|
188
|
+
port = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'port') || GridRest.grid_config['port'] || 80
|
189
|
+
url_prefix = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'url_prefix') || GridRest.grid_config['url_prefix'] || ''
|
190
|
+
raise "No host specified for GridRest" unless host
|
191
|
+
gurl = File.join( "#{host}:#{port}", url_prefix, URI.encode(url) )
|
192
|
+
gurl = "http://#{gurl}" unless gurl =~ /^http/
|
193
|
+
gurl
|
194
|
+
end
|
195
|
+
|
196
|
+
def grid_rest_get(url, rparams = {})
|
197
|
+
return grid_rest_request(:get, url, rparams)
|
198
|
+
end
|
199
|
+
|
200
|
+
def grid_rest_put(url, rparams = {})
|
201
|
+
grid_rest_request(:put, url, rparams)
|
202
|
+
end
|
203
|
+
|
204
|
+
def grid_rest_delete(url, rparams = {})
|
205
|
+
grid_rest_request(:delete, url, rparams)
|
206
|
+
end
|
207
|
+
def grid_rest_post(url, rparams={})
|
208
|
+
return grid_rest_request(:post, url, rparams)
|
209
|
+
end
|
210
|
+
def get_accept_header(f)
|
211
|
+
case f
|
212
|
+
when :json then :json #'application/json'
|
213
|
+
when :xml then :xml #'application/xml'
|
214
|
+
else :json #'application/json'
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
# Error class for a rest request. Has some nice features like
|
220
|
+
# internationalisation of messages, and basic methods to correspond
|
221
|
+
# with a normal request, but most importantly returns false on the
|
222
|
+
# valid? question.
|
223
|
+
class Error
|
224
|
+
attr_reader :message, :code, :url, :request_method
|
225
|
+
def initialize(message = nil, rparams = {})
|
226
|
+
@message = I18n.t(message, :scope => [:grid_rest, :error])
|
227
|
+
@code = rparams.delete(:code)
|
228
|
+
@url = rparams.delete(:url)
|
229
|
+
@request_method = rparams.delete(:request_method)
|
230
|
+
end
|
231
|
+
def code
|
232
|
+
@code || 500
|
233
|
+
end
|
234
|
+
def to_s
|
235
|
+
''
|
236
|
+
end
|
237
|
+
alias to_str to_s
|
238
|
+
|
239
|
+
def valid?
|
240
|
+
false
|
241
|
+
end
|
242
|
+
|
243
|
+
def try(*args)
|
244
|
+
nil
|
245
|
+
end
|
246
|
+
|
247
|
+
# Call this on error if the result should be an empty array, but wit the
|
248
|
+
# invalid metadata
|
249
|
+
def array
|
250
|
+
ErrorArray.new(self)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
class ErrorArray < Array
|
255
|
+
attr_reader :message, :code, :url, :request_method
|
256
|
+
def initialize(e)
|
257
|
+
@message = e.message
|
258
|
+
@code = e.code
|
259
|
+
@url = e.url
|
260
|
+
@request_method = e.request_method
|
261
|
+
end
|
262
|
+
def to_s
|
263
|
+
''
|
264
|
+
end
|
265
|
+
alias to_str to_s
|
266
|
+
def valid?
|
267
|
+
false
|
268
|
+
end
|
269
|
+
def try(*args)
|
270
|
+
nil
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# This class can be used in testing environments. It will always be valid and behaves a
|
275
|
+
# bit like a normal response when this is a json hash.
|
276
|
+
class DummyResponse < Hash
|
277
|
+
def code
|
278
|
+
200
|
279
|
+
end
|
280
|
+
def valid?
|
281
|
+
true
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
# Arrays are valid, unless defined otherwise
|
287
|
+
class Object
|
288
|
+
def valid?
|
289
|
+
true
|
290
|
+
end
|
291
|
+
def invalid?
|
292
|
+
!valid?
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
class NilClass
|
297
|
+
def valid?
|
298
|
+
false
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
class FalseClass
|
303
|
+
def valid?
|
304
|
+
false
|
305
|
+
end
|
306
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'grid_rest'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grid_rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Benjamin ter Kuile
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-06-07 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
segments:
|
26
|
+
- 3
|
27
|
+
- 0
|
28
|
+
- 7
|
29
|
+
version: 3.0.7
|
30
|
+
requirement: *id001
|
31
|
+
prerelease: false
|
32
|
+
type: :runtime
|
33
|
+
name: activesupport
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
requirement: *id002
|
43
|
+
prerelease: false
|
44
|
+
type: :runtime
|
45
|
+
name: rest-client
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
requirement: *id003
|
55
|
+
prerelease: false
|
56
|
+
type: :development
|
57
|
+
name: shoulda
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
- 0
|
67
|
+
version: 1.0.0
|
68
|
+
requirement: *id004
|
69
|
+
prerelease: false
|
70
|
+
type: :development
|
71
|
+
name: bundler
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 1
|
79
|
+
- 6
|
80
|
+
- 0
|
81
|
+
version: 1.6.0
|
82
|
+
requirement: *id005
|
83
|
+
prerelease: false
|
84
|
+
type: :development
|
85
|
+
name: jeweler
|
86
|
+
- !ruby/object:Gem::Dependency
|
87
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirement: *id006
|
95
|
+
prerelease: false
|
96
|
+
type: :development
|
97
|
+
name: rcov
|
98
|
+
description: |-
|
99
|
+
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.
|
100
|
+
The main development focus is JSON communication, but XML should be just as easy
|
101
|
+
email: bterkuile@gmail.com
|
102
|
+
executables: []
|
103
|
+
|
104
|
+
extensions: []
|
105
|
+
|
106
|
+
extra_rdoc_files:
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.rdoc
|
109
|
+
files:
|
110
|
+
- .document
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.rdoc
|
114
|
+
- Rakefile
|
115
|
+
- VERSION
|
116
|
+
- lib/grid_rest.rb
|
117
|
+
- lib/grid_rest/railtie.rb
|
118
|
+
- test/helper.rb
|
119
|
+
- test/test_grid_rest.rb
|
120
|
+
has_rdoc: true
|
121
|
+
homepage: http://github.com/bterkuile/grid_rest
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
segments:
|
141
|
+
- 0
|
142
|
+
version: "0"
|
143
|
+
requirements: []
|
144
|
+
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 1.3.6
|
147
|
+
signing_key:
|
148
|
+
specification_version: 3
|
149
|
+
summary: grid_rest is a gem that makes your rails application interact with other backends through a REST interface.
|
150
|
+
test_files: []
|
151
|
+
|