openrain-active_racksource 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 OpenRain, LLC. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,45 @@
1
+ ActiveRacksource
2
+ ================
3
+
4
+ NOT COMPLETE - CHECK BACK REAL SOON!
5
+ ====================================
6
+
7
+ Active**Rack**source is a [Rack][] backend for [ActiveResource][].
8
+
9
+ Ok ... What does that mean?
10
+ ---------------------------
11
+
12
+ Here's an example, let's define a simple [ActiveResource][] library
13
+
14
+ ActiveResource::Base.site = 'http://lots-of-dogs.com/'
15
+
16
+ class Dog < ActiveResource::Base
17
+ end
18
+
19
+ If you run the following command, it runs over HTTP and actually requests http://lots-of-dogs.com/dogs.xml
20
+
21
+ >> Dog.find( :all ).length
22
+ => 1462
23
+
24
+ But, if you give [ActiveResource][] a [Rack][] application to use as a backend ...
25
+
26
+ require 'active_racksource'
27
+
28
+ ActiveResource::Base.app = @my_rack_application
29
+
30
+ And run the command again ...
31
+
32
+ >> Dog.find( :all ).length
33
+ => 5
34
+
35
+ Then it runs against the [Rack][] application, not over any TCP or anything!
36
+ This is great for creating & testing [ActiveResource][] APIs for your web applications.
37
+
38
+ Cool, how should I get started?
39
+ -------------------------------
40
+
41
+ ... ( documentation coming soon! ) ...
42
+
43
+
44
+ [rack]: http://rack.rubyforge.org
45
+ [activeresource]: http://api.rubyonrails.org/classes/ActiveResource/Base.html
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rake'
2
+ require 'rubygems'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |s|
9
+ s.name = "active_racksource"
10
+ s.summary = "Rack backend for ActiveResource for testing RESTful Rack APIs"
11
+ s.email = "remi@remitaylor.com"
12
+ s.homepage = "http://github.com/openrain/active_racksource"
13
+ s.description = "Rack backend for ActiveResource for testing RESTful Rack APIs"
14
+ s.authors = %w( remi )
15
+ s.files = FileList["[A-Z]*", "{lib,spec,examples,rails_generators}/**/*"]
16
+ s.add_dependency 'remi-rackbox'
17
+ # s.executables = "neato"
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
22
+
23
+ Spec::Rake::SpecTask.new do |t|
24
+ t.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ desc "Run all examples with RCov"
28
+ Spec::Rake::SpecTask.new('rcov') do |t|
29
+ t.spec_files = FileList['spec/**/*_spec.rb']
30
+ t.rcov = true
31
+ end
32
+
33
+ Rake::RDocTask.new do |rdoc|
34
+ rdoc.rdoc_dir = 'rdoc'
35
+ rdoc.title = 'active_racksource'
36
+ rdoc.options << '--line-numbers' << '--inline-source'
37
+ rdoc.rdoc_files.include('RDOC_README.rdoc')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
40
+
41
+ desc 'Confirm that gemspec is $SAFE'
42
+ task :safe do
43
+ require 'yaml'
44
+ require 'rubygems/specification'
45
+ data = File.read('active_racksource.gemspec')
46
+ spec = nil
47
+ if data !~ %r{!ruby/object:Gem::Specification}
48
+ Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
49
+ else
50
+ spec = YAML.load(data)
51
+ end
52
+ spec.validate
53
+ puts spec
54
+ puts "OK"
55
+ end
56
+
57
+ task :default => :spec
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,24 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'rubygems' # i'd like to remove this - it's becoming less conventional to have this in ruby gems / libraries
4
+
5
+ begin
6
+ require 'active_resource'
7
+ rescue LoadError
8
+ raise "active_racksource requires active_resource, try: gem install activeresource"
9
+ end
10
+
11
+ begin
12
+ require 'rackbox'
13
+ rescue LoadError
14
+ raise "active_racksource requires rackbox, try: gem install remi-rackbox -s http://gems.github.com"
15
+ end
16
+
17
+ require 'active_racksource/active_racksource'
18
+ require 'active_racksource/response'
19
+ require 'active_racksource/http'
20
+ require 'active_racksource/base'
21
+ require 'active_racksource/connection'
22
+
23
+ ActiveResource::Base.send :include, ActiveRacksource::Base
24
+ ActiveResource::Connection.send :include, ActiveRacksource::Connection
@@ -0,0 +1,7 @@
1
+ # global namespace for ActiveRacksource
2
+ #
3
+ # if we add any global configuration options,
4
+ # they'll go here
5
+ #
6
+ class ActiveRacksource
7
+ end
@@ -0,0 +1,24 @@
1
+ class ActiveRacksource #:nodoc:
2
+
3
+ # Overrides parts of ActiveResource::Base for ActiveRacksource (to use Rack instead of HTTP)
4
+ module Base
5
+
6
+ def self.included base
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def app= rack_app
13
+ @rack_app = rack_app
14
+ end
15
+
16
+ def app
17
+ @rack_app
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,36 @@
1
+ class ActiveRacksource #:nodoc:
2
+
3
+ # Overrides parts of ActiveResource::Connection for ActiveRacksource (to use Rack instead of HTTP)
4
+ module Connection
5
+
6
+ def self.included base
7
+ base.instance_eval {
8
+ alias_method_chain :http, :rack_instead_of_real_http
9
+ }
10
+ end
11
+
12
+ # Overrides ActiveResource::Connection#http, returning our own object
13
+ # that uses a Rack application instead of actually going over HTTP
14
+ #
15
+ # example of what the HTTP object might get sent:
16
+ # http.send( :get, "/dogs.xml", [{"Accept"=>"application/xml"}] )
17
+ # http.send( :post, "/dogs.xml", ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<dog>\n <name>Rover</name>\n</dog>\n", {"Content-Type"=>"application/xml"}])
18
+ # http.send( :delete, "/dogs/6.xml", [{"Accept"=>"application/xml"}])
19
+ # http.send( :put, "/dogs/1.xml", ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<dog>\n <name>spot</name>\n <created-at type=\"datetime\">2009-02-09T18:27:11Z</created-at>\n <updated-at type=\"datetime\">2009-02-09T18:27:11Z</updated-at>\n <id type=\"integer\">1</id>\n</dog>\n", {"Content-Type"=>"application/xml"}])
20
+ #
21
+ # the object http.send :foo returns should have
22
+ # #code: string representation of status code, eg. '200' or '404'
23
+ # #message: string message, eg. 'OK' or 'Not Found'
24
+ # #body: string response body
25
+ #
26
+ def http_with_rack_instead_of_real_http
27
+ if ActiveResource::Base.app
28
+ ActiveRacksource::HTTP.new ActiveResource::Base.app
29
+ else
30
+ http_without_rack_instead_of_real_http
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,48 @@
1
+ class ActiveRacksource #:nodoc:
2
+
3
+ # Adapter ... acts like Net::HTTP but actually works against a Rack application
4
+ class HTTP
5
+
6
+ # Rack application
7
+ attr_accessor :app
8
+
9
+ # Initialized a new ActiveRacksource::HTTP
10
+ #
11
+ # ==== Parameters
12
+ # app: a Rack application
13
+ #
14
+ def initialize app = ActiveResource::Base.app
15
+ @app = RackBox::App.new app
16
+ end
17
+
18
+ def method_missing method, url, *args
19
+
20
+ case args.length
21
+ when 0
22
+ headers = { }
23
+ when 1
24
+ headers = args.first
25
+ when 2
26
+ if args.last.is_a?Hash
27
+ headers = args.last
28
+ data = args.first
29
+ else
30
+ raise "not sure howto #{ method.inspect } url:#{ url.inspect } with these args:#{ args.inspect }"
31
+ end
32
+ else
33
+ raise "not sure howto #{ method.inspect } url:#{ url.inspect } with these args:#{ args.inspect }"
34
+ end
35
+
36
+ request_options = { }
37
+ request_options[:method] = method
38
+ request_options[:data] = data if data
39
+ request_options.merge!( headers ) if headers
40
+
41
+ rack_response = @app.request url, request_options
42
+
43
+ ActiveRacksource::Response.new rack_response
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,49 @@
1
+ class ActiveRacksource #:nodoc:
2
+
3
+ # A thin wrapper around Rack::Response
4
+ #
5
+ # ActiveResource expects an HTTP Response object to be
6
+ # formatted in a certain way with the right methods, etc.
7
+ #
8
+ # This object wraps a Rack::Response and implements an
9
+ # inferface that supports ActiveResource
10
+ #
11
+ # Some methods called by ActiveResource:
12
+ # code:: string representation of status code, eg. '200' or '404'
13
+ # message:: string message, eg. 'OK' or 'Not Found'
14
+ # body:: string response body
15
+ #
16
+ class Response
17
+
18
+ # initialize a new ActiveRacksource::Response
19
+ #
20
+ # ==== Parameters
21
+ # rack_response:: a Rack::Response instance
22
+ #
23
+ def initialize rack_response
24
+ @rack_response = rack_response
25
+ end
26
+
27
+ def code
28
+ @rack_response.status.to_s
29
+ end
30
+
31
+ def message
32
+ if code.start_with?'2'
33
+ 'OK'
34
+ elsif code.start_with?'4'
35
+ 'Not Found'
36
+ elsif code.start_with?'3'
37
+ 'Redirect'
38
+ else
39
+ 'Error'
40
+ end
41
+ end
42
+
43
+ # by default, fall back to methods on the Rack::Response
44
+ def method_missing name, *args
45
+ @rack_response.send name, *args
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openrain-active_racksource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - remi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: remi-rackbox
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Rack backend for ActiveResource for testing RESTful Rack APIs
25
+ email: remi@remitaylor.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - Rakefile
34
+ - VERSION.yml
35
+ - README.markdown
36
+ - LICENSE
37
+ - lib/active_racksource.rb
38
+ - lib/active_racksource
39
+ - lib/active_racksource/http.rb
40
+ - lib/active_racksource/active_racksource.rb
41
+ - lib/active_racksource/base.rb
42
+ - lib/active_racksource/connection.rb
43
+ - lib/active_racksource/response.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/openrain/active_racksource
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --inline-source
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Rack backend for ActiveResource for testing RESTful Rack APIs
71
+ test_files: []
72
+