samlown-couchrest 0.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +46 -0
  3. data/Rakefile +67 -0
  4. data/THANKS.md +19 -0
  5. data/examples/model/example.rb +144 -0
  6. data/examples/word_count/markov +38 -0
  7. data/examples/word_count/views/books/chunked-map.js +3 -0
  8. data/examples/word_count/views/books/united-map.js +1 -0
  9. data/examples/word_count/views/markov/chain-map.js +6 -0
  10. data/examples/word_count/views/markov/chain-reduce.js +7 -0
  11. data/examples/word_count/views/word_count/count-map.js +6 -0
  12. data/examples/word_count/views/word_count/count-reduce.js +3 -0
  13. data/examples/word_count/word_count.rb +46 -0
  14. data/examples/word_count/word_count_query.rb +40 -0
  15. data/examples/word_count/word_count_views.rb +26 -0
  16. data/history.txt +114 -0
  17. data/lib/couchrest/commands/generate.rb +71 -0
  18. data/lib/couchrest/commands/push.rb +103 -0
  19. data/lib/couchrest/core/adapters/restclient.rb +35 -0
  20. data/lib/couchrest/core/database.rb +377 -0
  21. data/lib/couchrest/core/design.rb +79 -0
  22. data/lib/couchrest/core/document.rb +84 -0
  23. data/lib/couchrest/core/http_abstraction.rb +48 -0
  24. data/lib/couchrest/core/response.rb +16 -0
  25. data/lib/couchrest/core/rest_api.rb +49 -0
  26. data/lib/couchrest/core/server.rb +88 -0
  27. data/lib/couchrest/core/view.rb +4 -0
  28. data/lib/couchrest/helper/pager.rb +103 -0
  29. data/lib/couchrest/helper/streamer.rb +51 -0
  30. data/lib/couchrest/helper/upgrade.rb +51 -0
  31. data/lib/couchrest/middlewares/logger.rb +263 -0
  32. data/lib/couchrest/mixins/attachments.rb +31 -0
  33. data/lib/couchrest/mixins/attribute_protection.rb +74 -0
  34. data/lib/couchrest/mixins/callbacks.rb +532 -0
  35. data/lib/couchrest/mixins/class_proxy.rb +124 -0
  36. data/lib/couchrest/mixins/collection.rb +260 -0
  37. data/lib/couchrest/mixins/design_doc.rb +103 -0
  38. data/lib/couchrest/mixins/document_queries.rb +80 -0
  39. data/lib/couchrest/mixins/extended_attachments.rb +70 -0
  40. data/lib/couchrest/mixins/extended_document_mixins.rb +9 -0
  41. data/lib/couchrest/mixins/properties.rb +154 -0
  42. data/lib/couchrest/mixins/validation.rb +246 -0
  43. data/lib/couchrest/mixins/views.rb +173 -0
  44. data/lib/couchrest/mixins.rb +4 -0
  45. data/lib/couchrest/monkeypatches.rb +113 -0
  46. data/lib/couchrest/more/casted_model.rb +58 -0
  47. data/lib/couchrest/more/extended_document.rb +310 -0
  48. data/lib/couchrest/more/property.rb +50 -0
  49. data/lib/couchrest/more/typecast.rb +175 -0
  50. data/lib/couchrest/support/blank.rb +42 -0
  51. data/lib/couchrest/support/class.rb +190 -0
  52. data/lib/couchrest/support/rails.rb +42 -0
  53. data/lib/couchrest/validation/auto_validate.rb +157 -0
  54. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  55. data/lib/couchrest/validation/validation_errors.rb +125 -0
  56. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  57. data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
  58. data/lib/couchrest/validation/validators/format_validator.rb +122 -0
  59. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  60. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  61. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  62. data/lib/couchrest/validation/validators/length_validator.rb +139 -0
  63. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  64. data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
  65. data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
  66. data/lib/couchrest.rb +162 -0
  67. data/spec/couchrest/core/couchrest_spec.rb +184 -0
  68. data/spec/couchrest/core/database_spec.rb +840 -0
  69. data/spec/couchrest/core/design_spec.rb +138 -0
  70. data/spec/couchrest/core/document_spec.rb +275 -0
  71. data/spec/couchrest/core/server_spec.rb +35 -0
  72. data/spec/couchrest/helpers/pager_spec.rb +122 -0
  73. data/spec/couchrest/helpers/streamer_spec.rb +52 -0
  74. data/spec/couchrest/more/attribute_protection_spec.rb +150 -0
  75. data/spec/couchrest/more/casted_extended_doc_spec.rb +79 -0
  76. data/spec/couchrest/more/casted_model_spec.rb +406 -0
  77. data/spec/couchrest/more/extended_doc_attachment_spec.rb +135 -0
  78. data/spec/couchrest/more/extended_doc_inherited_spec.rb +40 -0
  79. data/spec/couchrest/more/extended_doc_spec.rb +797 -0
  80. data/spec/couchrest/more/extended_doc_subclass_spec.rb +98 -0
  81. data/spec/couchrest/more/extended_doc_view_spec.rb +456 -0
  82. data/spec/couchrest/more/property_spec.rb +628 -0
  83. data/spec/fixtures/attachments/README +3 -0
  84. data/spec/fixtures/attachments/couchdb.png +0 -0
  85. data/spec/fixtures/attachments/test.html +11 -0
  86. data/spec/fixtures/more/article.rb +35 -0
  87. data/spec/fixtures/more/card.rb +22 -0
  88. data/spec/fixtures/more/cat.rb +20 -0
  89. data/spec/fixtures/more/course.rb +22 -0
  90. data/spec/fixtures/more/event.rb +8 -0
  91. data/spec/fixtures/more/invoice.rb +17 -0
  92. data/spec/fixtures/more/person.rb +9 -0
  93. data/spec/fixtures/more/question.rb +6 -0
  94. data/spec/fixtures/more/service.rb +12 -0
  95. data/spec/fixtures/more/user.rb +22 -0
  96. data/spec/fixtures/views/lib.js +3 -0
  97. data/spec/fixtures/views/test_view/lib.js +3 -0
  98. data/spec/fixtures/views/test_view/only-map.js +4 -0
  99. data/spec/fixtures/views/test_view/test-map.js +3 -0
  100. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  101. data/spec/spec.opts +6 -0
  102. data/spec/spec_helper.rb +49 -0
  103. data/utils/remap.rb +27 -0
  104. data/utils/subset.rb +30 -0
  105. metadata +223 -0
@@ -0,0 +1,109 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class NumericValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ @options[:integer_only] = false unless @options.has_key?(:integer_only)
37
+ end
38
+
39
+ def call(target)
40
+ value = target.send(field_name)
41
+ return true if @options[:allow_nil] && value.nil?
42
+
43
+ value = (defined?(BigDecimal) && value.kind_of?(BigDecimal)) ? value.to_s('F') : value.to_s
44
+
45
+ error_message = @options[:message]
46
+ precision = @options[:precision]
47
+ scale = @options[:scale]
48
+
49
+ if @options[:integer_only]
50
+ return true if value =~ /\A[+-]?\d+\z/
51
+ error_message ||= ValidationErrors.default_error_message(:not_an_integer, field_name)
52
+ else
53
+ # FIXME: if precision and scale are not specified, can we assume that it is an integer?
54
+ # probably not, as floating point numbers don't have hard
55
+ # defined scale. the scale floats with the length of the
56
+ # integral and precision. Ie. if precision = 10 and integral
57
+ # portion of the number is 9834 (4 digits), the max scale will
58
+ # be 6 (10 - 4). But if the integral length is 1, max scale
59
+ # will be (10 - 1) = 9, so 1.234567890.
60
+ if precision && scale
61
+ #handles both Float when it has scale specified and BigDecimal
62
+ if precision > scale && scale > 0
63
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
64
+ elsif precision > scale && scale == 0
65
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
66
+ elsif precision == scale
67
+ return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
68
+ else
69
+ raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})"
70
+ end
71
+ elsif precision && scale.nil?
72
+ # for floats, if scale is not set
73
+
74
+ #total number of digits is less or equal precision
75
+ return true if value.gsub(/[^\d]/, '').length <= precision
76
+
77
+ #number of digits before decimal == precision, and the number is x.0. same as scale = 0
78
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
79
+ else
80
+ return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/
81
+ end
82
+ error_message ||= ValidationErrors.default_error_message(:not_a_number, field_name)
83
+ end
84
+
85
+ add_error(target, error_message, field_name)
86
+
87
+ # TODO: check the gt, gte, lt, lte, and eq options
88
+
89
+ return false
90
+ end
91
+ end # class NumericValidator
92
+
93
+ module ValidatesIsNumber
94
+
95
+ # Validate whether a field is numeric
96
+ #
97
+ def validates_numericality_of(*fields)
98
+ opts = opts_from_validator_args(fields)
99
+ add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator)
100
+ end
101
+
102
+ def validates_is_number(*fields)
103
+ warn "[DEPRECATION] `validates_is_number` is deprecated. Please use `validates_numericality_of` instead."
104
+ validates_numericality_of(*fields)
105
+ end
106
+
107
+ end # module ValidatesIsNumber
108
+ end # module Validation
109
+ end # module CouchRest
@@ -0,0 +1,114 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class RequiredFieldValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ end
37
+
38
+ def call(target)
39
+ value = target.validation_property_value(field_name)
40
+ property = target.validation_property(field_name.to_s)
41
+ return true if present?(value, property)
42
+
43
+ error_message = @options[:message] || default_error(property)
44
+ add_error(target, error_message, field_name)
45
+
46
+ false
47
+ end
48
+
49
+ protected
50
+
51
+ # Boolean property types are considered present if non-nil.
52
+ # Other property types are considered present if non-blank.
53
+ # Non-properties are considered present if non-blank.
54
+ def present?(value, property)
55
+ boolean_type?(property) ? !value.nil? : !value.blank?
56
+ end
57
+
58
+ def default_error(property)
59
+ actual = boolean_type?(property) ? :nil : :blank
60
+ ValidationErrors.default_error_message(actual, field_name)
61
+ end
62
+
63
+ # Is +property+ a boolean property?
64
+ #
65
+ # Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties.
66
+ # Returns false for other property types.
67
+ # Returns false for non-properties.
68
+ def boolean_type?(property)
69
+ property ? property.type == 'Boolean' : false
70
+ end
71
+
72
+ end # class RequiredFieldValidator
73
+
74
+ module ValidatesPresent
75
+
76
+ ##
77
+ # Validates that the specified attribute is present.
78
+ #
79
+ # For most property types "being present" is the same as being "not
80
+ # blank" as determined by the attribute's #blank? method. However, in
81
+ # the case of Boolean, "being present" means not nil; i.e. true or
82
+ # false.
83
+ #
84
+ # @note
85
+ # dm-core's support lib adds the blank? method to many classes,
86
+ # @see lib/dm-core/support/blank.rb (dm-core) for more information.
87
+ #
88
+ # @example [Usage]
89
+ #
90
+ # class Page
91
+ #
92
+ # property :required_attribute, String
93
+ # property :another_required, String
94
+ # property :yet_again, String
95
+ #
96
+ # validates_presence_of :required_attribute
97
+ # validates_presence_of :another_required, :yet_again
98
+ #
99
+ # # a call to valid? will return false unless
100
+ # # all three attributes are !blank?
101
+ # end
102
+ def validates_presence_of(*fields)
103
+ opts = opts_from_validator_args(fields)
104
+ add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
105
+ end
106
+
107
+ def validates_present(*fields)
108
+ warn "[DEPRECATION] `validates_present` is deprecated. Please use `validates_presence_of` instead."
109
+ validates_presence_of(*fields)
110
+ end
111
+
112
+ end # module ValidatesPresent
113
+ end # module Validation
114
+ end # module CouchRest
data/lib/couchrest.rb ADDED
@@ -0,0 +1,162 @@
1
+ # Copyright 2008 J. Chris Anderson
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rubygems'
16
+ begin
17
+ require 'json'
18
+ rescue LoadError
19
+ raise "You need install and require your own json compatible library since couchrest rest couldn't load the json/json_pure gem" unless Kernel.const_defined?("JSON")
20
+ end
21
+ require 'rest_client'
22
+
23
+ $:.unshift File.dirname(__FILE__) unless
24
+ $:.include?(File.dirname(__FILE__)) ||
25
+ $:.include?(File.expand_path(File.dirname(__FILE__)))
26
+
27
+ require 'couchrest/monkeypatches'
28
+
29
+ # = CouchDB, close to the metal
30
+ module CouchRest
31
+ VERSION = '0.35' unless self.const_defined?("VERSION")
32
+
33
+ autoload :Server, 'couchrest/core/server'
34
+ autoload :Database, 'couchrest/core/database'
35
+ autoload :Response, 'couchrest/core/response'
36
+ autoload :Document, 'couchrest/core/document'
37
+ autoload :Design, 'couchrest/core/design'
38
+ autoload :View, 'couchrest/core/view'
39
+ autoload :Model, 'couchrest/core/model'
40
+ autoload :Pager, 'couchrest/helper/pager'
41
+ autoload :FileManager, 'couchrest/helper/file_manager'
42
+ autoload :Streamer, 'couchrest/helper/streamer'
43
+ autoload :Upgrade, 'couchrest/helper/upgrade'
44
+
45
+ autoload :ExtendedDocument, 'couchrest/more/extended_document'
46
+ autoload :CastedModel, 'couchrest/more/casted_model'
47
+
48
+ require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'rest_api')
49
+ require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'http_abstraction')
50
+ require File.join(File.dirname(__FILE__), 'couchrest', 'mixins')
51
+ require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'rails') if defined?(Rails)
52
+
53
+ # we extend CouchRest with the RestAPI module which gives us acess to
54
+ # the get, post, put, delete and copy
55
+ CouchRest.extend(::RestAPI)
56
+
57
+ # The CouchRest module methods handle the basic JSON serialization
58
+ # and deserialization, as well as query parameters. The module also includes
59
+ # some helpers for tasks like instantiating a new Database or Server instance.
60
+ class << self
61
+
62
+ # extracted from Extlib
63
+ #
64
+ # Constantize tries to find a declared constant with the name specified
65
+ # in the string. It raises a NameError when the name is not in CamelCase
66
+ # or is not initialized.
67
+ #
68
+ # @example
69
+ # "Module".constantize #=> Module
70
+ # "Class".constantize #=> Class
71
+ def constantize(camel_cased_word)
72
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
73
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
74
+ end
75
+
76
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
77
+ end
78
+
79
+ # extracted from Extlib
80
+ #
81
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
82
+ # Like titleize, this is meant for creating pretty output.
83
+ #
84
+ # @example
85
+ # "employee_salary" #=> "Employee salary"
86
+ # "author_id" #=> "Author"
87
+ def humanize(lower_case_and_underscored_word)
88
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
89
+ end
90
+
91
+ # todo, make this parse the url and instantiate a Server or Database instance
92
+ # depending on the specificity.
93
+ def new(*opts)
94
+ Server.new(*opts)
95
+ end
96
+
97
+ def parse url
98
+ case url
99
+ when /^(https?:\/\/)(.*)\/(.*)\/(.*)/
100
+ scheme = $1
101
+ host = $2
102
+ db = $3
103
+ docid = $4
104
+ when /^(https?:\/\/)(.*)\/(.*)/
105
+ scheme = $1
106
+ host = $2
107
+ db = $3
108
+ when /^(https?:\/\/)(.*)/
109
+ scheme = $1
110
+ host = $2
111
+ when /(.*)\/(.*)\/(.*)/
112
+ host = $1
113
+ db = $2
114
+ docid = $3
115
+ when /(.*)\/(.*)/
116
+ host = $1
117
+ db = $2
118
+ else
119
+ db = url
120
+ end
121
+
122
+ db = nil if db && db.empty?
123
+
124
+ {
125
+ :host => (scheme || "http://") + (host || "127.0.0.1:5984"),
126
+ :database => db,
127
+ :doc => docid
128
+ }
129
+ end
130
+
131
+ # set proxy to use
132
+ def proxy url
133
+ HttpAbstraction.proxy = url
134
+ end
135
+
136
+ # ensure that a database exists
137
+ # creates it if it isn't already there
138
+ # returns it after it's been created
139
+ def database! url
140
+ parsed = parse url
141
+ cr = CouchRest.new(parsed[:host])
142
+ cr.database!(parsed[:database])
143
+ end
144
+
145
+ def database url
146
+ parsed = parse url
147
+ cr = CouchRest.new(parsed[:host])
148
+ cr.database(parsed[:database])
149
+ end
150
+
151
+ def paramify_url url, params = {}
152
+ if params && !params.empty?
153
+ query = params.collect do |k,v|
154
+ v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
155
+ "#{k}=#{CGI.escape(v.to_s)}"
156
+ end.join("&")
157
+ url = "#{url}?#{query}"
158
+ end
159
+ url
160
+ end
161
+ end # class << self
162
+ end
@@ -0,0 +1,184 @@
1
+ require File.expand_path("../../../spec_helper", __FILE__)
2
+
3
+ describe CouchRest do
4
+
5
+ before(:each) do
6
+ @cr = CouchRest.new(COUCHHOST)
7
+ begin
8
+ @db = @cr.database(TESTDB)
9
+ @db.delete! rescue nil
10
+ end
11
+ end
12
+
13
+ after(:each) do
14
+ begin
15
+ @db.delete! rescue nil
16
+ end
17
+ end
18
+
19
+ describe "getting info" do
20
+ it "should list databases" do
21
+ @cr.databases.should be_an_instance_of(Array)
22
+ end
23
+ it "should get info" do
24
+ @cr.info["couchdb"].should == "Welcome"
25
+ @cr.info.class.should == Hash
26
+ end
27
+ end
28
+
29
+ it "should restart" do
30
+ @cr.restart!
31
+ end
32
+
33
+ it "should provide one-time access to uuids" do
34
+ @cr.next_uuid.should_not be_nil
35
+ end
36
+
37
+ describe "initializing a database" do
38
+ it "should return a db" do
39
+ db = @cr.database(TESTDB)
40
+ db.should be_an_instance_of(CouchRest::Database)
41
+ db.host.should == @cr.uri
42
+ end
43
+ end
44
+
45
+ describe "parsing urls" do
46
+ it "should parse just a dbname" do
47
+ db = CouchRest.parse "my-db"
48
+ db[:database].should == "my-db"
49
+ db[:host].should == "http://127.0.0.1:5984"
50
+ end
51
+ it "should parse a host and db" do
52
+ db = CouchRest.parse "127.0.0.1/my-db"
53
+ db[:database].should == "my-db"
54
+ db[:host].should == "http://127.0.0.1"
55
+ end
56
+ it "should parse a host and db with http" do
57
+ db = CouchRest.parse "http://127.0.0.1/my-db"
58
+ db[:database].should == "my-db"
59
+ db[:host].should == "http://127.0.0.1"
60
+ end
61
+ it "should parse a host and db with https" do
62
+ db = CouchRest.parse "https://127.0.0.1/my-db"
63
+ db[:database].should == "my-db"
64
+ db[:host].should == "https://127.0.0.1"
65
+ end
66
+ it "should parse a host with a port and db" do
67
+ db = CouchRest.parse "127.0.0.1:5555/my-db"
68
+ db[:database].should == "my-db"
69
+ db[:host].should == "http://127.0.0.1:5555"
70
+ end
71
+ it "should parse a host with a port and db with http" do
72
+ db = CouchRest.parse "http://127.0.0.1:5555/my-db"
73
+ db[:database].should == "my-db"
74
+ db[:host].should == "http://127.0.0.1:5555"
75
+ end
76
+ it "should parse a host with a port and db with https" do
77
+ db = CouchRest.parse "https://127.0.0.1:5555/my-db"
78
+ db[:database].should == "my-db"
79
+ db[:host].should == "https://127.0.0.1:5555"
80
+ end
81
+ it "should parse just a host" do
82
+ db = CouchRest.parse "http://127.0.0.1:5555/"
83
+ db[:database].should be_nil
84
+ db[:host].should == "http://127.0.0.1:5555"
85
+ end
86
+ it "should parse just a host with https" do
87
+ db = CouchRest.parse "https://127.0.0.1:5555/"
88
+ db[:database].should be_nil
89
+ db[:host].should == "https://127.0.0.1:5555"
90
+ end
91
+ it "should parse just a host no slash" do
92
+ db = CouchRest.parse "http://127.0.0.1:5555"
93
+ db[:host].should == "http://127.0.0.1:5555"
94
+ db[:database].should be_nil
95
+ end
96
+ it "should parse just a host no slash and https" do
97
+ db = CouchRest.parse "https://127.0.0.1:5555"
98
+ db[:host].should == "https://127.0.0.1:5555"
99
+ db[:database].should be_nil
100
+ end
101
+ it "should get docid" do
102
+ db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc"
103
+ db[:database].should == "my-db"
104
+ db[:host].should == "http://127.0.0.1:5555"
105
+ db[:doc].should == "my-doc"
106
+ end
107
+ it "should get docid with http" do
108
+ db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc"
109
+ db[:database].should == "my-db"
110
+ db[:host].should == "http://127.0.0.1:5555"
111
+ db[:doc].should == "my-doc"
112
+ end
113
+ it "should get docid with https" do
114
+ db = CouchRest.parse "https://127.0.0.1:5555/my-db/my-doc"
115
+ db[:database].should == "my-db"
116
+ db[:host].should == "https://127.0.0.1:5555"
117
+ db[:doc].should == "my-doc"
118
+ end
119
+ end
120
+
121
+ describe "easy initializing a database adapter" do
122
+ it "should be possible without an explicit CouchRest instantiation" do
123
+ db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
124
+ db.should be_an_instance_of(CouchRest::Database)
125
+ db.host.should == "http://127.0.0.1:5984"
126
+ end
127
+ # TODO add https support (need test environment...)
128
+ # it "should work with https" # do
129
+ # db = CouchRest.database "https://127.0.0.1:5984/couchrest-test"
130
+ # db.host.should == "https://127.0.0.1:5984"
131
+ # end
132
+ it "should not create the database automatically" do
133
+ db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
134
+ lambda{db.info}.should raise_error(RestClient::ResourceNotFound)
135
+ end
136
+ end
137
+
138
+ describe "ensuring the db exists" do
139
+ it "should be super easy" do
140
+ db = CouchRest.database! "http://127.0.0.1:5984/couchrest-test-2"
141
+ db.name.should == 'couchrest-test-2'
142
+ db.info["db_name"].should == 'couchrest-test-2'
143
+ end
144
+ end
145
+
146
+ describe "successfully creating a database" do
147
+ it "should start without a database" do
148
+ @cr.databases.should_not include(TESTDB)
149
+ end
150
+ it "should return the created database" do
151
+ db = @cr.create_db(TESTDB)
152
+ db.should be_an_instance_of(CouchRest::Database)
153
+ end
154
+ it "should create the database" do
155
+ db = @cr.create_db(TESTDB)
156
+ @cr.databases.should include(TESTDB)
157
+ end
158
+ end
159
+
160
+ describe "failing to create a database because the name is taken" do
161
+ before(:each) do
162
+ db = @cr.create_db(TESTDB)
163
+ end
164
+ it "should start with the test database" do
165
+ @cr.databases.should include(TESTDB)
166
+ end
167
+ it "should PUT the database and raise an error" do
168
+ lambda{
169
+ @cr.create_db(TESTDB)
170
+ }.should raise_error(RestClient::Request::RequestFailed)
171
+ end
172
+ end
173
+
174
+ describe "using a proxy for RestClient connections" do
175
+ it "should set proxy url for RestClient" do
176
+ CouchRest.proxy 'http://localhost:8888/'
177
+ proxy_uri = URI.parse(HttpAbstraction.proxy)
178
+ proxy_uri.host.should eql( 'localhost' )
179
+ proxy_uri.port.should eql( 8888 )
180
+ CouchRest.proxy nil
181
+ end
182
+ end
183
+
184
+ end