couchmodel 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/Rakefile +5 -5
- data/lib/core_extension/string.rb +10 -7
- data/lib/couch_model/base.rb +9 -9
- data/lib/couch_model/base/accessor.rb +2 -0
- data/lib/couch_model/collection.rb +3 -3
- data/lib/couch_model/database.rb +4 -4
- data/lib/couch_model/design.rb +11 -11
- data/lib/couch_model/server.rb +5 -5
- data/lib/transport/base.rb +65 -0
- data/lib/transport/json.rb +69 -0
- data/lib/transport/request/builder.rb +59 -0
- data/lib/transport/request/parameter/serializer.rb +60 -0
- data/spec/fake_transport_helper.rb +7 -2
- data/spec/lib/couch_model/active_model_spec.rb +6 -1
- data/spec/lib/couch_model/{core → base}/accessor_spec.rb +21 -9
- data/spec/lib/couch_model/{core → base}/association_spec.rb +0 -0
- data/spec/lib/couch_model/{core → base}/finder_spec.rb +0 -0
- data/spec/lib/couch_model/{core → base}/setup_spec.rb +0 -0
- data/spec/lib/couch_model/base_spec.rb +4 -4
- data/spec/lib/couch_model/collection_spec.rb +2 -2
- data/spec/lib/couch_model/database_spec.rb +4 -4
- data/spec/lib/transport/base_spec.rb +49 -0
- data/spec/lib/transport/json_spec.rb +56 -0
- data/spec/lib/transport/request/builder_spec.rb +51 -0
- data/spec/lib/transport/request/parameter/serializer_spec.rb +16 -0
- metadata +26 -15
- data/lib/couch_model/transport.rb +0 -178
- data/spec/lib/couch_model/transport_spec.rb +0 -114
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Philipp Bruell
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-06-12 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -25,8 +25,10 @@ dependencies:
|
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
28
30
|
- 0
|
29
|
-
version:
|
31
|
+
version: 1.3.0
|
30
32
|
type: :development
|
31
33
|
version_requirements: *id001
|
32
34
|
description: CouchModel provides an interface to easly handle CouchDB documents. It also comes with a ActiveModel implementation to integrate into an Rails 3 application.
|
@@ -52,22 +54,24 @@ files:
|
|
52
54
|
- lib/couch_model/collection.rb
|
53
55
|
- lib/couch_model/server.rb
|
54
56
|
- lib/couch_model/design.rb
|
55
|
-
- lib/couch_model/transport.rb
|
56
57
|
- lib/couch_model/base.rb
|
57
58
|
- lib/couch_model/database.rb
|
58
59
|
- lib/couch_model.rb
|
59
60
|
- lib/core_extension/string.rb
|
60
61
|
- lib/core_extension/array.rb
|
62
|
+
- lib/transport/request/builder.rb
|
63
|
+
- lib/transport/request/parameter/serializer.rb
|
64
|
+
- lib/transport/json.rb
|
65
|
+
- lib/transport/base.rb
|
61
66
|
- lib/code_smells.reek
|
62
67
|
- spec/spec_helper.rb
|
63
68
|
- spec/lib/couch_model/view_spec.rb
|
64
69
|
- spec/lib/couch_model/server_spec.rb
|
65
|
-
- spec/lib/couch_model/core/association_spec.rb
|
66
|
-
- spec/lib/couch_model/core/finder_spec.rb
|
67
|
-
- spec/lib/couch_model/core/accessor_spec.rb
|
68
|
-
- spec/lib/couch_model/core/setup_spec.rb
|
69
70
|
- spec/lib/couch_model/design_spec.rb
|
70
|
-
- spec/lib/couch_model/
|
71
|
+
- spec/lib/couch_model/base/association_spec.rb
|
72
|
+
- spec/lib/couch_model/base/finder_spec.rb
|
73
|
+
- spec/lib/couch_model/base/accessor_spec.rb
|
74
|
+
- spec/lib/couch_model/base/setup_spec.rb
|
71
75
|
- spec/lib/couch_model/active_model_spec.rb
|
72
76
|
- spec/lib/couch_model/collection_spec.rb
|
73
77
|
- spec/lib/couch_model/design/association_test_model_one.design
|
@@ -80,6 +84,10 @@ files:
|
|
80
84
|
- spec/lib/couch_model_spec.rb
|
81
85
|
- spec/lib/core_extension/string_spec.rb
|
82
86
|
- spec/lib/core_extension/array_spec.rb
|
87
|
+
- spec/lib/transport/request/builder_spec.rb
|
88
|
+
- spec/lib/transport/request/parameter/serializer_spec.rb
|
89
|
+
- spec/lib/transport/json_spec.rb
|
90
|
+
- spec/lib/transport/base_spec.rb
|
83
91
|
- spec/fake_transport_helper.rb
|
84
92
|
- spec/integration/views_spec.rb
|
85
93
|
- spec/integration/associations_spec.rb
|
@@ -122,12 +130,11 @@ summary: CouchModel provides an interface to easly handle CouchDB documents.
|
|
122
130
|
test_files:
|
123
131
|
- spec/lib/couch_model/view_spec.rb
|
124
132
|
- spec/lib/couch_model/server_spec.rb
|
125
|
-
- spec/lib/couch_model/core/association_spec.rb
|
126
|
-
- spec/lib/couch_model/core/finder_spec.rb
|
127
|
-
- spec/lib/couch_model/core/accessor_spec.rb
|
128
|
-
- spec/lib/couch_model/core/setup_spec.rb
|
129
133
|
- spec/lib/couch_model/design_spec.rb
|
130
|
-
- spec/lib/couch_model/
|
134
|
+
- spec/lib/couch_model/base/association_spec.rb
|
135
|
+
- spec/lib/couch_model/base/finder_spec.rb
|
136
|
+
- spec/lib/couch_model/base/accessor_spec.rb
|
137
|
+
- spec/lib/couch_model/base/setup_spec.rb
|
131
138
|
- spec/lib/couch_model/active_model_spec.rb
|
132
139
|
- spec/lib/couch_model/collection_spec.rb
|
133
140
|
- spec/lib/couch_model/configuration_spec.rb
|
@@ -137,6 +144,10 @@ test_files:
|
|
137
144
|
- spec/lib/couch_model_spec.rb
|
138
145
|
- spec/lib/core_extension/string_spec.rb
|
139
146
|
- spec/lib/core_extension/array_spec.rb
|
147
|
+
- spec/lib/transport/request/builder_spec.rb
|
148
|
+
- spec/lib/transport/request/parameter/serializer_spec.rb
|
149
|
+
- spec/lib/transport/json_spec.rb
|
150
|
+
- spec/lib/transport/base_spec.rb
|
140
151
|
- spec/integration/views_spec.rb
|
141
152
|
- spec/integration/associations_spec.rb
|
142
153
|
- spec/integration/saved_model_spec.rb
|
@@ -1,178 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'cgi'
|
3
|
-
require 'net/http'
|
4
|
-
require 'json'
|
5
|
-
|
6
|
-
module CouchModel
|
7
|
-
|
8
|
-
# Common transport layer for http transfers.
|
9
|
-
class Transport
|
10
|
-
|
11
|
-
attr_reader :http_method
|
12
|
-
attr_reader :url
|
13
|
-
attr_reader :options
|
14
|
-
attr_reader :headers
|
15
|
-
attr_reader :parameters
|
16
|
-
attr_reader :body
|
17
|
-
attr_reader :response
|
18
|
-
|
19
|
-
def initialize(http_method, url, options = { })
|
20
|
-
@http_method = http_method
|
21
|
-
@uri = URI.parse url
|
22
|
-
@headers = options[:headers] || { }
|
23
|
-
@parameters = options[:parameters] || { }
|
24
|
-
@body = options[:body]
|
25
|
-
end
|
26
|
-
|
27
|
-
def perform
|
28
|
-
initialize_request_class
|
29
|
-
initialize_request_path
|
30
|
-
initialize_request
|
31
|
-
initialize_request_body
|
32
|
-
perform_request
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def initialize_request_class
|
38
|
-
request_class_name = @http_method.to_s.capitalize
|
39
|
-
raise NotImplementedError, "the request method #{http_method} is not implemented" unless Net::HTTP.const_defined?(request_class_name)
|
40
|
-
@request_class = Net::HTTP.const_get request_class_name
|
41
|
-
end
|
42
|
-
|
43
|
-
def initialize_request_path
|
44
|
-
serialize_parameters
|
45
|
-
@request_path = @uri.path + @serialized_parameters
|
46
|
-
end
|
47
|
-
|
48
|
-
def serialize_parameters
|
49
|
-
quote_parameters
|
50
|
-
@serialized_parameters = if @parameters.nil? || @parameters.empty?
|
51
|
-
""
|
52
|
-
else
|
53
|
-
"?" + @quoted_parameters.collect do |key, value|
|
54
|
-
value.is_a?(Array) ?
|
55
|
-
value.map{ |element| "#{key}=#{element}" }.join("&") :
|
56
|
-
"#{key}=#{value}"
|
57
|
-
end.join("&")
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def quote_parameters
|
62
|
-
@quoted_parameters = { }
|
63
|
-
@parameters.each do |key, value|
|
64
|
-
encoded_key = CGI.escape(key.to_s)
|
65
|
-
@quoted_parameters[encoded_key] = value.is_a?(Array) ? value.map{ |element| CGI.escape element } : CGI.escape(value)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def initialize_request
|
70
|
-
@request = @request_class.new @request_path, @headers
|
71
|
-
end
|
72
|
-
|
73
|
-
def initialize_request_body
|
74
|
-
return unless [ :post, :put ].include?(@http_method.to_sym)
|
75
|
-
@request.body = @body ? @body : @serialized_parameters.sub(/^\?/, "")
|
76
|
-
end
|
77
|
-
|
78
|
-
def perform_request
|
79
|
-
@response = Net::HTTP.start(@uri.host, @uri.port) do |connection|
|
80
|
-
connection.request @request
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.request(http_method, url, options = { })
|
85
|
-
transport = new http_method, url, options
|
86
|
-
transport.perform
|
87
|
-
transport.response
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
# Extended transport layer for http transfers. Basic authorization and JSON transfers are supported.
|
93
|
-
class ExtendedTransport < Transport
|
94
|
-
|
95
|
-
# The UnexpectedStatusCodeError is raised if the :expected_status_code option is given to
|
96
|
-
# the :request method and the responded status code is different from the expected one.
|
97
|
-
class UnexpectedStatusCodeError < StandardError
|
98
|
-
|
99
|
-
attr_reader :status_code
|
100
|
-
attr_reader :message
|
101
|
-
|
102
|
-
def initialize(status_code, message = nil)
|
103
|
-
@status_code, @message = status_code, message
|
104
|
-
end
|
105
|
-
|
106
|
-
def to_s
|
107
|
-
"#{super} received status code #{self.status_code}" + (@message ? " [#{@message}]" : "")
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
attr_reader :expected_status_code
|
113
|
-
attr_reader :auth_type
|
114
|
-
attr_reader :username
|
115
|
-
attr_reader :password
|
116
|
-
|
117
|
-
def initialize(http_method, url, options = { })
|
118
|
-
super http_method, url, options
|
119
|
-
@expected_status_code = options[:expected_status_code]
|
120
|
-
@auth_type = options[:auth_type]
|
121
|
-
@username = options[:username]
|
122
|
-
@password = options[:password]
|
123
|
-
end
|
124
|
-
|
125
|
-
def perform
|
126
|
-
initialize_headers
|
127
|
-
super
|
128
|
-
check_status_code
|
129
|
-
parse_response
|
130
|
-
end
|
131
|
-
|
132
|
-
private
|
133
|
-
|
134
|
-
def initialize_headers
|
135
|
-
@headers["Accept"] = "application/json"
|
136
|
-
end
|
137
|
-
|
138
|
-
def initialize_request
|
139
|
-
super
|
140
|
-
if @auth_type == :basic
|
141
|
-
@request.basic_auth @username, @password
|
142
|
-
elsif @auth_type
|
143
|
-
raise NotImplementedError, "the given auth_type [#{@auth_type}] is not implemented"
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def quote_parameters
|
148
|
-
@parameters.each do |key, value|
|
149
|
-
@parameters[key] = value.to_json if value.respond_to?(:to_json)
|
150
|
-
end
|
151
|
-
super
|
152
|
-
end
|
153
|
-
|
154
|
-
def initialize_request_body
|
155
|
-
super
|
156
|
-
if @body
|
157
|
-
@request.body = @body.to_json
|
158
|
-
@request["Content-Type"] = "application/json"
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def check_status_code
|
163
|
-
return unless @expected_status_code
|
164
|
-
response_code = @response.code
|
165
|
-
response_body = @response.body
|
166
|
-
raise UnexpectedStatusCodeError.new(response_code.to_i, response_body) if @expected_status_code.to_s != response_code
|
167
|
-
end
|
168
|
-
|
169
|
-
def parse_response
|
170
|
-
body = @response.body
|
171
|
-
@response = body.nil? ? nil : JSON.parse(body)
|
172
|
-
rescue JSON::ParserError
|
173
|
-
@response = body.to_s
|
174
|
-
end
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
end
|
@@ -1,114 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "transport"))
|
3
|
-
|
4
|
-
describe CouchModel::Transport do
|
5
|
-
|
6
|
-
use_real_transport!
|
7
|
-
|
8
|
-
describe "request" do
|
9
|
-
|
10
|
-
before :each do
|
11
|
-
@http_method = :get
|
12
|
-
@url = "http://localhost:5984/"
|
13
|
-
@options = { }
|
14
|
-
|
15
|
-
@request = Net::HTTP::Get.new "/", { }
|
16
|
-
@response = Object.new
|
17
|
-
@response.stub!(:code).and_return("200")
|
18
|
-
@response.stub!(:body).and_return("test")
|
19
|
-
Net::HTTP.stub!(:start).and_return(@response)
|
20
|
-
end
|
21
|
-
|
22
|
-
def do_request(options = { })
|
23
|
-
CouchModel::Transport.request @http_method, @url, @options.merge(options)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should initialize the correct request object" do
|
27
|
-
Net::HTTP::Get.should_receive(:new).with("/", { }).and_return(@request)
|
28
|
-
do_request
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should perform the request" do
|
32
|
-
Net::HTTP.should_receive(:start).and_return(@response)
|
33
|
-
do_request
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should return the response" do
|
37
|
-
do_request.body.should == "test"
|
38
|
-
end
|
39
|
-
|
40
|
-
context "with parameters" do
|
41
|
-
|
42
|
-
before :each do
|
43
|
-
@options.merge! :parameters => { :foo => "bar", :test => [ "value1", "value2" ] }
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should initialize the correct request object" do
|
47
|
-
Net::HTTP::Get.should_receive(:new).with(
|
48
|
-
"/?foo=bar&test=value1&test=value2", { }
|
49
|
-
).and_return(@request)
|
50
|
-
do_request
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
describe CouchModel::ExtendedTransport do
|
60
|
-
|
61
|
-
use_real_transport!
|
62
|
-
|
63
|
-
describe "request" do
|
64
|
-
|
65
|
-
before :each do
|
66
|
-
@http_method = :get
|
67
|
-
@url = "http://localhost:5984/"
|
68
|
-
@options = {
|
69
|
-
:auth_type => :basic,
|
70
|
-
:username => "test",
|
71
|
-
:password => "test",
|
72
|
-
:expected_status_code => 200
|
73
|
-
}
|
74
|
-
|
75
|
-
@request = Net::HTTP::Get.new "/", { }
|
76
|
-
@response = Object.new
|
77
|
-
@response.stub!(:code).and_return("200")
|
78
|
-
@response.stub!(:body).and_return("{\"test\":\"test\"}")
|
79
|
-
Net::HTTP.stub!(:start).and_return(@response)
|
80
|
-
end
|
81
|
-
|
82
|
-
def do_request(options = { })
|
83
|
-
CouchModel::ExtendedTransport.request @http_method, @url, @options.merge(options)
|
84
|
-
end
|
85
|
-
|
86
|
-
it "should initialize the correct request object" do
|
87
|
-
Net::HTTP::Get.should_receive(:new).with("/", { "Accept" => "application/json" }).and_return(@request)
|
88
|
-
do_request
|
89
|
-
end
|
90
|
-
|
91
|
-
it "should perform the request" do
|
92
|
-
Net::HTTP.should_receive(:start).and_return(@response)
|
93
|
-
do_request
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should return the parsed response" do
|
97
|
-
do_request.should == { "test" => "test" }
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should raise NotImplementedError if the given auth_type is wrong" do
|
101
|
-
lambda do
|
102
|
-
do_request :auth_type => :invalid
|
103
|
-
end.should raise_error(NotImplementedError)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should raise UnexpectedStatusCodeError if responded status code is wrong" do
|
107
|
-
lambda do
|
108
|
-
do_request :expected_status_code => 201
|
109
|
-
end.should raise_error(CouchModel::ExtendedTransport::UnexpectedStatusCodeError)
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
end
|