alexvollmer-httparty 0.3.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,9 @@ describe HTTParty::Response do
5
5
  before do
6
6
  @response_object = {'foo' => 'bar'}
7
7
  @body = "{foo:'bar'}"
8
- @code = 200
9
- @response = HTTParty::Response.new(@response_object, @body, @code)
8
+ @code = '200'
9
+ @message = 'OK'
10
+ @response = HTTParty::Response.new(@response_object, @body, @code, @message)
10
11
  end
11
12
 
12
13
  it "should set delegate" do
@@ -18,22 +19,30 @@ describe HTTParty::Response do
18
19
  end
19
20
 
20
21
  it "should set code" do
21
- @response.code.should == @code
22
+ @response.code.should.to_s == @code
23
+ end
24
+
25
+ it "should set code as a Fixnum" do
26
+ @response.code.should be_an_instance_of(Fixnum)
27
+ end
28
+
29
+ it "should set body" do
30
+ @response.body.should == @body
22
31
  end
23
32
  end
24
33
 
25
34
  it "should be able to set headers during initialization" do
26
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, {'foo' => 'bar'})
35
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK', {'foo' => 'bar'})
27
36
  response.headers.should == {'foo' => 'bar'}
28
37
  end
29
38
 
30
39
  it "should send missing methods to delegate" do
31
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
40
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
32
41
  response['foo'].should == 'bar'
33
42
  end
34
43
 
35
44
  it "should be able to iterate delegate if it is array" do
36
- response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200)
45
+ response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200, 'OK')
37
46
  response.size.should == 2
38
47
  lambda {
39
48
  response.each { |item| }
@@ -41,12 +50,12 @@ describe HTTParty::Response do
41
50
  end
42
51
 
43
52
  xit "should allow hashes to be accessed with dot notation" do
44
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
53
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
45
54
  response.foo.should == 'bar'
46
55
  end
47
56
 
48
57
  xit "should allow nested hashes to be accessed with dot notation" do
49
- response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200)
58
+ response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200, 'OK')
50
59
  response.foo.should == {'bar' => 'baz'}
51
60
  response.foo.bar.should == 'baz'
52
61
  end
@@ -21,6 +21,12 @@ describe HTTParty do
21
21
  @klass.base_uri('http://api.foobar.com')
22
22
  @klass.base_uri.should == 'http://api.foobar.com'
23
23
  end
24
+
25
+ it 'should not modify the parameter during assignment' do
26
+ uri = 'http://api.foobar.com'
27
+ @klass.base_uri(uri)
28
+ uri.should == 'http://api.foobar.com'
29
+ end
24
30
  end
25
31
 
26
32
  describe "#normalize_base_uri" do
@@ -38,6 +44,12 @@ describe HTTParty do
38
44
  uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
39
45
  uri.should == 'https://api.foo.com/v1:443'
40
46
  end
47
+
48
+ it 'should not modify the parameter' do
49
+ uri = 'http://api.foobar.com'
50
+ HTTParty.normalize_base_uri(uri)
51
+ uri.should == 'http://api.foobar.com'
52
+ end
41
53
  end
42
54
 
43
55
  describe "headers" do
@@ -151,11 +163,23 @@ describe HTTParty do
151
163
  @klass.default_options[:format].should == :yaml
152
164
  end
153
165
 
166
+ it "should allow plain" do
167
+ @klass.format :plain
168
+ @klass.default_options[:format].should == :plain
169
+ end
170
+
154
171
  it 'should not allow funky format' do
155
172
  lambda do
156
173
  @klass.format :foobar
157
174
  end.should raise_error(HTTParty::UnsupportedFormat)
158
175
  end
176
+
177
+ it 'should only print each format once with an exception' do
178
+ lambda do
179
+ @klass.format :foobar
180
+ end.should raise_error(HTTParty::UnsupportedFormat, "Must be one of: json, plain, html, yaml, xml")
181
+ end
182
+
159
183
  end
160
184
 
161
185
  describe "with multiple class definitions" do
@@ -182,9 +206,7 @@ describe HTTParty do
182
206
  describe "#get" do
183
207
  it "should be able to get html" do
184
208
  stub_http_response_with('google.html')
185
- resp = HTTParty.get('http://www.google.com')
186
- resp.should == file_fixture('google.html')
187
- resp.body.should == file_fixture('google.html')
209
+ HTTParty.get('http://www.google.com').should == file_fixture('google.html')
188
210
  end
189
211
 
190
212
  it "should be able parse response type json automatically" do
@@ -202,7 +224,6 @@ describe HTTParty do
202
224
  "location" => "Opera Plaza, California",
203
225
  "profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
204
226
  }
205
- tweets.body.should == file_fixture('twitter.json')
206
227
  end
207
228
 
208
229
  it "should be able parse response type xml automatically" do
@@ -220,7 +241,6 @@ describe HTTParty do
220
241
  "profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
221
242
  "location" => nil
222
243
  }
223
- tweets.body.should == file_fixture('twitter.xml')
224
244
  end
225
245
 
226
246
  it "should not get undefined method add_node for nil class for the following xml" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexvollmer-httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-10 00:00:00 -08:00
12
+ date: 2009-04-23 00:00:00 -07:00
13
13
  default_executable: httparty
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: crack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.1
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: echoe
17
27
  type: :development
@@ -30,13 +40,10 @@ extensions: []
30
40
 
31
41
  extra_rdoc_files:
32
42
  - bin/httparty
33
- - lib/core_extensions.rb
34
43
  - lib/httparty/cookie_hash.rb
44
+ - lib/httparty/core_extensions.rb
35
45
  - lib/httparty/exceptions.rb
36
46
  - lib/httparty/module_inheritable_attributes.rb
37
- - lib/httparty/parsers/json.rb
38
- - lib/httparty/parsers/xml.rb
39
- - lib/httparty/parsers.rb
40
47
  - lib/httparty/request.rb
41
48
  - lib/httparty/response.rb
42
49
  - lib/httparty/version.rb
@@ -64,13 +71,10 @@ files:
64
71
  - features/supports_redirection.feature
65
72
  - History
66
73
  - httparty.gemspec
67
- - lib/core_extensions.rb
68
74
  - lib/httparty/cookie_hash.rb
75
+ - lib/httparty/core_extensions.rb
69
76
  - lib/httparty/exceptions.rb
70
77
  - lib/httparty/module_inheritable_attributes.rb
71
- - lib/httparty/parsers/json.rb
72
- - lib/httparty/parsers/xml.rb
73
- - lib/httparty/parsers.rb
74
78
  - lib/httparty/request.rb
75
79
  - lib/httparty/response.rb
76
80
  - lib/httparty/version.rb
@@ -88,8 +92,6 @@ files:
88
92
  - spec/fixtures/undefined_method_add_node_for_nil.xml
89
93
  - spec/hash_spec.rb
90
94
  - spec/httparty/cookie_hash_spec.rb
91
- - spec/httparty/parsers/json_spec.rb
92
- - spec/httparty/parsers/xml_spec.rb
93
95
  - spec/httparty/request_spec.rb
94
96
  - spec/httparty/response_spec.rb
95
97
  - spec/httparty_spec.rb
@@ -1,175 +0,0 @@
1
- require 'time'
2
-
3
- # Copyright (c) 2008 Sam Smoot.
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
- class Object #:nodoc:
25
- # @return <TrueClass, FalseClass>
26
- #
27
- # @example [].blank? #=> true
28
- # @example [1].blank? #=> false
29
- # @example [nil].blank? #=> false
30
- #
31
- # Returns true if the object is nil or empty (if applicable)
32
- def blank?
33
- nil? || (respond_to?(:empty?) && empty?)
34
- end unless method_defined?(:blank?)
35
- end # class Object
36
-
37
- class Numeric #:nodoc:
38
- # @return <TrueClass, FalseClass>
39
- #
40
- # Numerics can't be blank
41
- def blank?
42
- false
43
- end unless method_defined?(:blank?)
44
- end # class Numeric
45
-
46
- class NilClass #:nodoc:
47
- # @return <TrueClass, FalseClass>
48
- #
49
- # Nils are always blank
50
- def blank?
51
- true
52
- end unless method_defined?(:blank?)
53
- end # class NilClass
54
-
55
- class TrueClass #:nodoc:
56
- # @return <TrueClass, FalseClass>
57
- #
58
- # True is not blank.
59
- def blank?
60
- false
61
- end unless method_defined?(:blank?)
62
- end # class TrueClass
63
-
64
- class FalseClass #:nodoc:
65
- # False is always blank.
66
- def blank?
67
- true
68
- end unless method_defined?(:blank?)
69
- end # class FalseClass
70
-
71
- class String #:nodoc:
72
- # @example "".blank? #=> true
73
- # @example " ".blank? #=> true
74
- # @example " hey ho ".blank? #=> false
75
- #
76
- # @return <TrueClass, FalseClass>
77
- #
78
- # Strips out whitespace then tests if the string is empty.
79
- def blank?
80
- strip.empty?
81
- end unless method_defined?(:blank?)
82
-
83
- def snake_case
84
- return self.downcase if self =~ /^[A-Z]+$/
85
- self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
86
- return $+.downcase
87
- end unless method_defined?(:snake_case)
88
- end # class String
89
-
90
- class Hash #:nodoc:
91
- # @return <String> This hash as a query string
92
- #
93
- # @example
94
- # { :name => "Bob",
95
- # :address => {
96
- # :street => '111 Ruby Ave.',
97
- # :city => 'Ruby Central',
98
- # :phones => ['111-111-1111', '222-222-2222']
99
- # }
100
- # }.to_params
101
- # #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
102
- def to_params
103
- params = self.map { |k,v| normalize_param(k,v) }.join
104
- params.chop! # trailing &
105
- params
106
- end
107
-
108
- # @param key<Object> The key for the param.
109
- # @param value<Object> The value for the param.
110
- #
111
- # @return <String> This key value pair as a param
112
- #
113
- # @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
114
- def normalize_param(key, value)
115
- param = ''
116
- stack = []
117
-
118
- if value.is_a?(Array)
119
- param << value.map { |element| normalize_param("#{key}[]", element) }.join
120
- elsif value.is_a?(Hash)
121
- stack << [key,value]
122
- else
123
- param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
124
- end
125
-
126
- stack.each do |parent, hash|
127
- hash.each do |key, value|
128
- if value.is_a?(Hash)
129
- stack << ["#{parent}[#{key}]", value]
130
- else
131
- param << normalize_param("#{parent}[#{key}]", value)
132
- end
133
- end
134
- end
135
-
136
- param
137
- end
138
-
139
- # @return <String> The hash as attributes for an XML tag.
140
- #
141
- # @example
142
- # { :one => 1, "two"=>"TWO" }.to_xml_attributes
143
- # #=> 'one="1" two="TWO"'
144
- def to_xml_attributes
145
- map do |k,v|
146
- %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
147
- end.join(' ')
148
- end
149
- end
150
-
151
- class BlankSlate #:nodoc:
152
- instance_methods.each { |m| undef_method m unless m =~ /^__/ }
153
- end
154
-
155
- # 1.8.6 has mistyping of transitive in if statement
156
- require "rexml/document"
157
- module REXML #:nodoc:
158
- class Document < Element #:nodoc:
159
- def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
160
- if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
161
- output = Output.new( output, xml_decl.encoding )
162
- end
163
- formatter = if indent > -1
164
- if transitive
165
- REXML::Formatters::Transitive.new( indent, ie_hack )
166
- else
167
- REXML::Formatters::Pretty.new( indent, ie_hack )
168
- end
169
- else
170
- REXML::Formatters::Default.new( ie_hack )
171
- end
172
- formatter.write( self, output )
173
- end
174
- end
175
- end
@@ -1,4 +0,0 @@
1
- Dir[File.dirname(__FILE__) + "/parsers/*.rb"].sort.each do |path|
2
- filename = File.basename(path)
3
- require "httparty/parsers/#{filename}"
4
- end
@@ -1,74 +0,0 @@
1
- # Copyright (c) 2004-2008 David Heinemeier Hansson
2
- # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5
-
6
- require 'yaml'
7
- require 'strscan'
8
-
9
- module HTTParty
10
- module Parsers #:nodoc:
11
- module JSON #:nodoc:
12
- class ParseError < StandardError #:nodoc:
13
- end
14
-
15
- def self.decode(json)
16
- YAML.load(unescape(convert_json_to_yaml(json)))
17
- rescue ArgumentError => e
18
- raise ParseError, "Invalid JSON string"
19
- end
20
-
21
- protected
22
-
23
- def self.unescape(str)
24
- str.gsub(/\\u([0-9a-f]{4})/) {
25
- [$1.hex].pack("U")
26
- }
27
- end
28
-
29
- # matches YAML-formatted dates
30
- DATE_REGEX = /^\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?$/
31
-
32
- # Ensure that ":" and "," are always followed by a space
33
- def self.convert_json_to_yaml(json) #:nodoc:
34
- scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
35
- while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
36
- case char = scanner[1]
37
- when '"', "'"
38
- if !quoting
39
- quoting = char
40
- pos = scanner.pos
41
- elsif quoting == char
42
- if json[pos..scanner.pos-2] =~ DATE_REGEX
43
- # found a date, track the exact positions of the quotes so we can remove them later.
44
- # oh, and increment them for each current mark, each one is an extra padded space that bumps
45
- # the position in the final YAML output
46
- total_marks = marks.size
47
- times << pos+total_marks << scanner.pos+total_marks
48
- end
49
- quoting = false
50
- end
51
- when ":",","
52
- marks << scanner.pos - 1 unless quoting
53
- end
54
- end
55
-
56
- if marks.empty?
57
- json.gsub(/\\\//, '/')
58
- else
59
- left_pos = [-1].push(*marks)
60
- right_pos = marks << json.length
61
- output = []
62
- left_pos.each_with_index do |left, i|
63
- output << json[left.succ..right_pos[i]]
64
- end
65
- output = output * " "
66
-
67
- times.each { |i| output[i-1] = ' ' }
68
- output.gsub!(/\\\//, '/')
69
- output
70
- end
71
- end
72
- end
73
- end
74
- end