dupe 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.rdoc +390 -147
  2. data/lib/dupe/active_resource_extensions.rb +25 -0
  3. data/lib/dupe/attribute_template.rb +71 -0
  4. data/lib/dupe/cucumber_hooks.rb +15 -7
  5. data/lib/dupe/custom_mocks.rb +102 -0
  6. data/lib/dupe/database.rb +51 -0
  7. data/lib/dupe/dupe.rb +359 -372
  8. data/lib/dupe/log.rb +38 -0
  9. data/lib/dupe/mock.rb +50 -0
  10. data/lib/dupe/model.rb +55 -0
  11. data/lib/dupe/network.rb +38 -0
  12. data/lib/dupe/record.rb +35 -56
  13. data/lib/dupe/rest_validation.rb +16 -0
  14. data/lib/dupe/schema.rb +36 -0
  15. data/lib/dupe/sequence.rb +11 -10
  16. data/lib/dupe/singular_plural_detection.rb +9 -0
  17. data/lib/dupe/string.rb +6 -8
  18. data/lib/dupe/symbol.rb +3 -0
  19. data/lib/dupe.rb +13 -12
  20. data/rails_generators/dupe/templates/custom_mocks.rb +4 -34
  21. data/rails_generators/dupe/templates/dupe_setup.rb +3 -23
  22. data/spec/lib_specs/active_resource_extensions_spec.rb +29 -0
  23. data/spec/lib_specs/attribute_template_spec.rb +173 -0
  24. data/spec/lib_specs/database_spec.rb +133 -0
  25. data/spec/lib_specs/dupe_spec.rb +307 -0
  26. data/spec/lib_specs/log_spec.rb +78 -0
  27. data/spec/lib_specs/logged_request_spec.rb +22 -0
  28. data/spec/lib_specs/mock_definitions_spec.rb +32 -0
  29. data/spec/lib_specs/mock_spec.rb +67 -0
  30. data/spec/lib_specs/model_spec.rb +90 -0
  31. data/spec/lib_specs/network_spec.rb +77 -0
  32. data/spec/lib_specs/record_spec.rb +70 -0
  33. data/spec/lib_specs/rest_validation_spec.rb +17 -0
  34. data/spec/lib_specs/schema_spec.rb +90 -0
  35. data/spec/lib_specs/sequence_spec.rb +26 -0
  36. data/spec/lib_specs/string_spec.rb +31 -0
  37. data/spec/lib_specs/symbol_spec.rb +17 -0
  38. data/spec/spec_helper.rb +2 -5
  39. metadata +29 -7
  40. data/lib/dupe/active_resource.rb +0 -135
  41. data/lib/dupe/attribute.rb +0 -17
  42. data/lib/dupe/configuration.rb +0 -20
  43. data/lib/dupe/mock_service_response.rb +0 -55
  44. data/spec/lib_specs/dupe_record_spec.rb +0 -57
@@ -1,135 +0,0 @@
1
- # this allows us to control when we flush out the HttpMock requests / responses
2
- ActiveResource::HttpMock.instance_eval do #:nodoc:
3
- def reset! #:nodoc:
4
- end
5
-
6
- def reset_from_dupe! #:nodoc:
7
- requests.clear
8
- responses.clear
9
- end
10
-
11
- def delete_mock(http_method, path)
12
- responses.reject! {|r| r[0].path == path && r[0].method == http_method}
13
- end
14
- end
15
-
16
- # makes it possible to override existing request/response definitions
17
- module ActiveResource #:nodoc:
18
- class HttpMock #:nodoc:
19
- class Responder #:nodoc:
20
- for method in [ :post, :put, :get, :delete, :head ]
21
- module_eval <<-EOE, __FILE__, __LINE__
22
- def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
23
- @responses.reject! {|r| r[0].path == path && r[0].method == :#{method}}
24
- @responses << [Request.new(:#{method}, path, nil, request_headers), Response.new(body || "", status, response_headers)]
25
- end
26
- EOE
27
- end
28
- end
29
- end
30
- end
31
-
32
- module CustomMocks
33
- class NotImplementedError < StandardError
34
- end
35
-
36
- def custom_service(url)
37
- raise NotImplementedError.new("you must implement the CustomMocks::custom_service method.")
38
- end
39
- end
40
-
41
- module ActiveResource
42
- class Connection #:nodoc:
43
-
44
- include CustomMocks
45
-
46
- class << self
47
- attr_reader :request_log
48
-
49
- def log_request(method, path, headers, response)
50
- @request_log ||= []
51
- @request_log << {
52
- :method => method,
53
- :path => path,
54
- :headers => headers,
55
- :response => response
56
- }
57
- end
58
-
59
- def flush_request_log
60
- @request_log = []
61
- end
62
-
63
- def print_request_log
64
- @request_log ||= []
65
- if @request_log.empty?
66
- puts("\n -----No request attempts logged for this scenario")
67
- return
68
- end
69
- puts "\n Request attempts logged for this scenario:\n --------------------------------------------\n\n"
70
- @request_log.each do |request|
71
- puts " Request: #{request[:method].to_s.upcase} #{request[:path]}"
72
- puts " Headers: #{request[:headers].inspect}"
73
- puts " Response Body:\n#{request[:response].body.split("\n").map {|s| (" "*6) + s}.join("\n")}"
74
- puts " Response Code: #{request[:response].code}"
75
- puts " Response Headers: #{request[:response].headers}"
76
- puts " Response Message: #{request[:response].message}"
77
- puts "\n\n"
78
- end
79
- end
80
- end
81
-
82
- # Execute a GET request.
83
- # Used to get (find) resources.
84
- def get(path, headers = {})
85
- begin
86
- response = request(:get, path, build_request_headers(headers, :get))
87
-
88
- # if the request threw an exception
89
- rescue
90
- # make sure that it's not a custom service that should be handled on the fly
91
- response_body = custom_service(path)
92
- response_body = response_body.to_xml(:root => path.match(/^\/([^\/\.]+)/)[1]) if response_body.is_a?(Hash) || response_body.is_a?(Array)
93
- ActiveResource::HttpMock.respond_to do |mock|
94
- mock.get path, {}, response_body
95
- end
96
- response = request(:get, path, build_request_headers(headers, :get))
97
- ActiveResource::HttpMock.delete_mock(:get, path)
98
- end
99
- ActiveResource::Connection.log_request(:get, path, build_request_headers(headers, :get), response)
100
- format.decode(response.body)
101
- end
102
-
103
- # Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
104
- # Used to delete resources.
105
- def delete(path, headers = {})
106
- response = request(:delete, path, build_request_headers(headers, :delete))
107
- ActiveResource::Connection.log_request(:delete, path, build_request_headers(headers, :delete), response)
108
- response
109
- end
110
-
111
- # Execute a PUT request (see HTTP protocol documentation if unfamiliar).
112
- # Used to update resources.
113
- def put(path, body = '', headers = {})
114
- response = request(:put, path, body.to_s, build_request_headers(headers, :put))
115
- ActiveResource::Connection.log_request(:put, path, build_request_headers(headers, :put), response)
116
- response
117
- end
118
-
119
- # Execute a POST request.
120
- # Used to create new resources.
121
- def post(path, body = '', headers = {})
122
- response = request(:post, path, body.to_s, build_request_headers(headers, :post))
123
- ActiveResource::Connection.log_request(:post, path, build_request_headers(headers, :post), response)
124
- response
125
- end
126
-
127
- # Execute a HEAD request.
128
- # Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
129
- def head(path, headers = {})
130
- response = request(:head, path, build_request_headers(headers))
131
- ActiveResource::Connection.log_request(:head, path, build_request_headers(headers), response)
132
- response
133
- end
134
- end
135
- end
@@ -1,17 +0,0 @@
1
- class Dupe
2
- class Attribute #:nodoc:
3
- def initialize(name, value=nil, prock=nil)
4
- @name, @value, @prock = name.to_sym, value, prock
5
- end
6
-
7
- def value(v=nil)
8
- v = @value.dup if @value and !v
9
- @prock && v ? @prock.call(v) : v
10
- end
11
-
12
- def to_hash
13
- {@name => value}
14
- end
15
-
16
- end
17
- end
@@ -1,20 +0,0 @@
1
- class Dupe
2
- class Configuration #:nodoc:
3
- attr_reader :config
4
-
5
- def initialize
6
- @config ||= {}
7
- @config[:record_identifiers] = [:id]
8
- end
9
-
10
- def method_missing(method_name, *args, &block)
11
- set_config_option(method_name.to_sym, method_name.to_s.plural? ? args : args.first)
12
- end
13
-
14
-
15
- private
16
- def set_config_option(key, value)
17
- @config[key] = value
18
- end
19
- end
20
- end
@@ -1,55 +0,0 @@
1
- class Dupe
2
- class MockServiceResponse #:nodoc:
3
- attr_reader :mocks
4
- attr_reader :resource_name
5
- attr_reader :format
6
-
7
- def initialize(resource_name, format=:xml)
8
- @mocks = []
9
- @resource_name = resource_name
10
- @to_format = "to_#{format}"
11
- end
12
-
13
- def define_mock(prock)
14
- @mocks << prock
15
- end
16
-
17
- def method_missing(method_name, *args, &block)
18
- @mocks << block
19
- end
20
-
21
- def run_mocks(records, identifiers)
22
- ActiveResource::HttpMock.respond_to do |mock|
23
- @mocks.each do |a_mock|
24
- a_mock.call mock, records
25
- end
26
- end
27
- find_all(records)
28
- records.each {|r| find_one(r, identifiers)}
29
- end
30
-
31
- private
32
- def find_all(records)
33
- ActiveResource::HttpMock.respond_to do |mock|
34
- mock.get "#{prefix}#{@resource_name.to_s.pluralize}.xml", {}, format_for_service_response(records)
35
- end
36
- end
37
-
38
- def find_one(record, identifiers)
39
- ActiveResource::HttpMock.respond_to do |mock|
40
- identifiers.each do |identifier|
41
- mock.get "#{prefix}#{@resource_name.to_s.pluralize}/#{record[identifier]}.xml", {}, format_for_service_response(record)
42
- end
43
- end
44
- end
45
-
46
- def format_for_service_response(records)
47
- root = (records.is_a? Array) ? @resource_name.to_s.pluralize : @resource_name.to_s
48
- @format == :json ? records.to_json({:root => root}): records.to_xml({:root => root})
49
- end
50
-
51
- def prefix
52
- @resource_name.to_s.camelize.constantize.prefix.gsub(/\/+/, '/') rescue "/"
53
- end
54
- end
55
- end
@@ -1,57 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Dupe::Record" do
4
- describe "the new method" do
5
- it "should setup an empty internal hash when created with no parameters" do
6
- r = Dupe::Record.new
7
- r.internal_attributes_hash.should == {}
8
- end
9
-
10
- it "should be accessible via missing methods" do
11
- book = Dupe::Record.new :title => 'Bible', :author => {:name => 'Jebus'}
12
- book.title.should == 'Bible'
13
- book.author.name.should == 'Jebus'
14
- end
15
-
16
- it "should be accessible via hash accessors" do
17
- book = Dupe::Record.new :title => 'Bible', :author => {:name => 'Jebus'}
18
- book[:title].should == 'Bible'
19
- book[:author][:name].should == 'Jebus'
20
- end
21
-
22
- it "should let you set attributes via method missing" do
23
- book = Dupe::Record.new :title => 'Bible', :author => {:name => 'Jebus'}
24
- book.genre = 'Superstition'
25
- book.genre.should == 'Superstition'
26
- book.chapters = [
27
- {:title => 'Hair', :start => 1},
28
- {:title => 'Carpet', :start => 15}
29
- ]
30
- book.chapters.first.title.should == 'Hair'
31
- book.chapters.first.start.should == 1
32
- book.chapters.last.title.should == 'Carpet'
33
- book.chapters.last.start.should == 15
34
- end
35
-
36
- it "should let you set attributes via has accessors" do
37
- book = Dupe::Record.new :title => 'Bible', :author => {:name => 'Jebus'}
38
-
39
- # setting existing attributes
40
- book[:title] = 'The Carpet Makers'
41
- book[:author][:name] = 'Andreas Eschbach'
42
- book[:title].should == 'The Carpet Makers'
43
- book[:author][:name].should == 'Andreas Eschbach'
44
-
45
- # setting new attributes
46
- book[:genre] = 'Science Fiction'
47
- book[:genre].should == 'Science Fiction'
48
-
49
- # setting hash attributes
50
- book[:chapters] = [
51
- {:title => 'Hair', :start => 1},
52
- {:title => 'Carpet', :start => 15}
53
- ]
54
- book[:chapters][0][:title].should == 'Hair'
55
- end
56
- end
57
- end