active_resource_simulator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_resource_simulator.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_resource_simulator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active_resource_simulator"
7
+ s.version = ActiveResourceSimulator::VERSION
8
+ s.authors = ["Robert Lail"]
9
+ s.email = ["robert.lail@cph.org"]
10
+ s.homepage = ""
11
+ s.summary = %q{A smarter way of testing ActiveResource: a wrapper around HttpMock}
12
+ s.description = %q{A wrapper around HttpMock}
13
+
14
+ s.rubyforge_project = "active_resource_simulator"
15
+
16
+ s.add_dependency "activeresource"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,83 @@
1
+ module ActiveResource
2
+ class Simulator
3
+
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ @simulations = []
8
+ end
9
+
10
+ attr_reader :model, :simulations
11
+
12
+
13
+ def simulate_request(method, path, body=nil, options={})
14
+ @simulations << prepare_simulation(method, path, body, options)
15
+ load_simulations!
16
+ end
17
+
18
+ alias :simulate :simulate_request
19
+
20
+
21
+ def create(body=nil, options={})
22
+ path = options.delete(:path) || model.collection_path
23
+ options.reverse_merge!({:status => 201})
24
+ simulate_request(:post, path, body, options)
25
+ end
26
+
27
+ def show(param, body=nil, options={})
28
+ path = options.delete(:path) || model.element_path(param)
29
+ options.reverse_merge!({:status => 200})
30
+ simulate_request(:get, path, body, options)
31
+ end
32
+
33
+ def update(param, options={})
34
+ path = options.delete(:path) || model.element_path(param)
35
+ options.reverse_merge!({:status => 204})
36
+ simulate_request(:put, path, options[:body], options)
37
+ end
38
+
39
+ def destroy(param, options={})
40
+ path = options.delete(:path) || model.element_path(param)
41
+ options.reverse_merge!({:status => 200})
42
+ simulate_request(:delete, path, options[:body], options)
43
+ end
44
+
45
+
46
+ private
47
+
48
+
49
+ def prepare_simulation(method, path, body, options)
50
+ # See the default values for HttpMock
51
+ # http://api.rubyonrails.org/classes/ActiveResource/HttpMock.html
52
+ [
53
+ method,
54
+ path,
55
+ build_request_headers(method, path, options[:headers]),
56
+ format_body(body),
57
+ options[:status] || 200,
58
+ options[:response_headers] || {}
59
+ ]
60
+ end
61
+
62
+ def build_request_headers(method, path, headers)
63
+ model.connection.send(:build_request_headers,
64
+ (headers||{}),
65
+ method,
66
+ model.connection.site.merge(path))
67
+ end
68
+
69
+ def format_body(body)
70
+ body && model.format.encode(body)
71
+ end
72
+
73
+ def load_simulations!
74
+ ActiveResource::HttpMock.respond_to do |mock|
75
+ @simulations.each do |simulation|
76
+ mock.send(*simulation)
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ end
83
+ end
@@ -0,0 +1,14 @@
1
+ require "active_resource"
2
+ require "active_resource/simulator"
3
+ require "active_resource_simulator/version"
4
+
5
+ module ActiveResourceSimulator
6
+
7
+ def run_simulation
8
+ yield ActiveResource::Simulator.new(self)
9
+ ActiveResource::HttpMock.reset!
10
+ end
11
+
12
+ end
13
+
14
+ ActiveResource::Base.extend(ActiveResourceSimulator)
@@ -0,0 +1,3 @@
1
+ module ActiveResourceSimulator
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_resource_simulator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Robert Lail
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-18 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activeresource
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: A wrapper around HttpMock
28
+ email:
29
+ - robert.lail@cph.org
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Rakefile
40
+ - active_resource_simulator.gemspec
41
+ - lib/active_resource/simulator.rb
42
+ - lib/active_resource_simulator.rb
43
+ - lib/active_resource_simulator/version.rb
44
+ has_rdoc: true
45
+ homepage: ""
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: active_resource_simulator
68
+ rubygems_version: 1.6.2
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: "A smarter way of testing ActiveResource: a wrapper around HttpMock"
72
+ test_files: []
73
+