hugs 1.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -8,5 +8,5 @@ group :development do
8
8
  gem "rake"
9
9
  gem "jeweler", "~> 1.5.1"
10
10
  gem "minitest", "~> 2.0.0"
11
- gem "mocha", "~> 0.9.10"
11
+ gem "webmock", "1.6.1"
12
12
  end
data/Gemfile.lock CHANGED
@@ -1,17 +1,20 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ addressable (2.2.2)
5
+ crack (0.1.8)
4
6
  git (1.2.5)
5
7
  jeweler (1.5.1)
6
8
  bundler (~> 1.0.0)
7
9
  git (>= 1.2.5)
8
10
  rake
9
11
  minitest (2.0.0)
10
- mocha (0.9.10)
11
- rake
12
12
  net-http-persistent (1.4.1)
13
13
  nokogiri (1.4.4)
14
14
  rake (0.8.7)
15
+ webmock (1.6.1)
16
+ addressable (>= 2.2.2)
17
+ crack (>= 0.1.7)
15
18
  yajl-ruby (0.7.8)
16
19
 
17
20
  PLATFORMS
@@ -20,8 +23,8 @@ PLATFORMS
20
23
  DEPENDENCIES
21
24
  jeweler (~> 1.5.1)
22
25
  minitest (~> 2.0.0)
23
- mocha (~> 0.9.10)
24
26
  net-http-persistent (~> 1.4.1)
25
27
  nokogiri (~> 1.4.4)
26
28
  rake
29
+ webmock (= 1.6.1)
27
30
  yajl-ruby (~> 0.7.8)
data/README.md CHANGED
@@ -30,8 +30,3 @@ will be implemented against an XML OCCI API.
30
30
  ### Examples
31
31
 
32
32
  See the 'Examples' section in the [wiki](http://github.com/retr0h/hugs/wiki/).
33
-
34
- ### TODO
35
-
36
- * Move @request instance method in #response_for to local. It was set to an instance
37
- for ease of testing but it bothers me.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 2.0.0
data/hugs.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hugs}
8
- s.version = "1.3.1"
8
+ s.version = "2.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["retr0h"]
12
- s.date = %q{2010-12-15}
12
+ s.date = %q{2010-12-16}
13
13
  s.email = %q{john@dewey.ws}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -48,7 +48,7 @@ Gem::Specification.new do |s|
48
48
  s.add_development_dependency(%q<rake>, [">= 0"])
49
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
50
50
  s.add_development_dependency(%q<minitest>, ["~> 2.0.0"])
51
- s.add_development_dependency(%q<mocha>, ["~> 0.9.10"])
51
+ s.add_development_dependency(%q<webmock>, ["= 1.6.1"])
52
52
  else
53
53
  s.add_dependency(%q<yajl-ruby>, ["~> 0.7.8"])
54
54
  s.add_dependency(%q<nokogiri>, ["~> 1.4.4"])
@@ -56,7 +56,7 @@ Gem::Specification.new do |s|
56
56
  s.add_dependency(%q<rake>, [">= 0"])
57
57
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
58
58
  s.add_dependency(%q<minitest>, ["~> 2.0.0"])
59
- s.add_dependency(%q<mocha>, ["~> 0.9.10"])
59
+ s.add_dependency(%q<webmock>, ["= 1.6.1"])
60
60
  end
61
61
  else
62
62
  s.add_dependency(%q<yajl-ruby>, ["~> 0.7.8"])
@@ -65,7 +65,7 @@ Gem::Specification.new do |s|
65
65
  s.add_dependency(%q<rake>, [">= 0"])
66
66
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
67
67
  s.add_dependency(%q<minitest>, ["~> 2.0.0"])
68
- s.add_dependency(%q<mocha>, ["~> 0.9.10"])
68
+ s.add_dependency(%q<webmock>, ["= 1.6.1"])
69
69
  end
70
70
  end
71
71
 
data/lib/hugs.rb CHANGED
@@ -8,6 +8,8 @@ class Hugs
8
8
  :xml => "application/xml",
9
9
  }.freeze
10
10
 
11
+ Verbs = %w(get delete post put).freeze
12
+
11
13
  ##
12
14
  # Required options:
13
15
  # +host+: A String with the host to connect.
@@ -34,13 +36,15 @@ class Hugs
34
36
  # - +:body+: A sub Hash to be JSON encoded, and posted in
35
37
  # the message body.
36
38
 
37
- %w(get delete post put).each do |verb|
39
+ Verbs.each do |verb|
38
40
  define_method verb do |*args|
39
41
  path = args[0]
40
42
  params = args[1] || {}
41
- clazz = eval "::Net::HTTP::#{verb.capitalize}"
43
+ clazz = eval "Net::HTTP::#{verb.capitalize}"
42
44
 
43
- parse response_for(clazz, path, params)
45
+ response = response_for(clazz, path, params)
46
+ response.body = parse response.body
47
+ response
44
48
  end
45
49
  end
46
50
 
@@ -65,22 +69,22 @@ private
65
69
  query = params[:query] && params.delete(:query)
66
70
  body = params[:body] && params.delete(:body)
67
71
 
68
- @http ||= ::Net::HTTP::Persistent.new
69
- @url ||= ::URI.parse "#{@scheme}://#{@host}:#{@port}"
72
+ @http ||= Net::HTTP::Persistent.new
73
+ @url ||= URI.parse "#{@scheme}://#{@host}:#{@port}"
70
74
 
71
- @request = request.new [path, query].compact.join "?"
72
- @request.body = encode(body) if body
73
- common_headers @request
75
+ request = request.new [path, query].compact.join "?"
76
+ request.body = encode(body) if body
77
+ common_headers request
74
78
 
75
- @http.request(@url, @request).body
79
+ @http.request(@url, request)
76
80
  end
77
81
 
78
82
  def common_headers request
79
83
  request.basic_auth(@user, @password) if requires_authentication?
80
84
  case request.class.to_s
81
- when ::Net::HTTP::Get.to_s, ::Net::HTTP::Delete.to_s
85
+ when Net::HTTP::Get.to_s, Net::HTTP::Delete.to_s
82
86
  request.add_field "accept", Headers[@type]
83
- when ::Net::HTTP::Post.to_s, ::Net::HTTP::Put.to_s
87
+ when Net::HTTP::Post.to_s, Net::HTTP::Put.to_s
84
88
  request.add_field "accept", Headers[@type]
85
89
  request.add_field "content-type", Headers[@type]
86
90
  end
@@ -88,14 +92,14 @@ private
88
92
 
89
93
  def parse data
90
94
  if is_json?
91
- ::Yajl::Parser.parse data
95
+ Yajl::Parser.parse data
92
96
  elsif is_xml?
93
- ::Nokogiri::XML.parse data
97
+ Nokogiri::XML.parse data
94
98
  end
95
99
  end
96
100
 
97
- def encode hash
98
- ::Yajl::Encoder.encode hash if is_json?
101
+ def encode body
102
+ is_json? ? (Yajl::Encoder.encode body) : body
99
103
  end
100
104
 
101
105
  def requires_authentication?
data/test/support.rb CHANGED
@@ -1,11 +1,9 @@
1
- ::Bundler.setup :default, :test
1
+ Bundler.setup :default, :test
2
2
 
3
- require "minitest/spec"
4
- require "mocha"
5
-
6
- require "./lib/hugs"
3
+ %w(minitest/spec webmock ./lib/hugs).each { |r| require r }
7
4
 
8
5
  class MiniTest::Unit::TestCase
6
+ include WebMock::API
9
7
  end
10
8
 
11
- ::MiniTest::Unit.autorun
9
+ MiniTest::Unit.autorun
data/test/test_hugs.rb CHANGED
@@ -1,76 +1,103 @@
1
- require "support"
2
- require "base64"
3
- require "hugs"
1
+ %w(support base64 hugs).each { |r| require r }
4
2
 
5
3
  describe Hugs do
6
4
  before do
7
- @user = "user",
8
- @password = "credentials"
9
5
  @valid_options = {
10
- :user => @user,
11
- :password => @password,
12
6
  :host => "example.com",
13
7
  :port => 80,
14
8
  :scheme => "https",
15
9
  }
16
10
 
17
- @instance = ::Hugs.new @valid_options
11
+ WebMock.reset!
12
+ @instance = Hugs.new @valid_options
18
13
  end
19
14
 
20
15
  describe "#response_for" do
21
16
  before do
22
- @http = mock(:request => mock(:body => :body))
23
- @request = ::Net::HTTP::Get
24
-
25
- ::Net::HTTP::Persistent.stubs :new => @http
17
+ @request = Net::HTTP::Get
26
18
  end
27
19
 
28
20
  it "generates a path" do
21
+ stub_request :get, "https://example.com:80/"
22
+
29
23
  @instance.send :response_for, @request, "/", {}
30
24
 
31
- @instance.instance_variable_get(:@request).path.must_equal "/"
25
+ assert_requested :get, "https://example.com:80/"
32
26
  end
33
27
 
34
28
  it "generates a path when a valid :query exists" do
29
+ stub_request(:get, "https://example.com:80/").with:query => {"foo" => "bar"}
30
+
35
31
  @instance.send :response_for, @request, "/", :query => "foo=bar"
36
32
 
37
- @instance.instance_variable_get(:@request).path.must_equal "/?foo=bar"
33
+ assert_requested :get, "https://example.com:80/", :query => {"foo" => "bar"}
38
34
  end
39
35
 
40
36
  it "generates a path when a nil :query exists" do
37
+ stub_request :get, "https://example.com:80/"
38
+
41
39
  @instance.send :response_for, @request, "/", :query => nil
42
40
 
43
- @instance.instance_variable_get(:@request).path.must_equal "/"
41
+ assert_requested :get, "https://example.com:80/"
44
42
  end
45
43
 
46
- it "sets the body as JSON when a valid :body exists" do
47
- @instance.send :response_for, @request, "/", :body => {:foo => :bar}
44
+ describe "body" do
45
+ before do
46
+ stub_request :get, "https://example.com:80/"
47
+ end
48
+
49
+ it "doesn't set the body when an invalid :body exists" do
50
+ @instance.send :response_for, @request, "/", :body => nil
48
51
 
49
- @instance.instance_variable_get(:@request).body.must_equal '{"foo":"bar"}'
50
- end
52
+ assert_requested :get, "https://example.com:80/", :body => nil
53
+ end
54
+
55
+ describe "json" do
56
+ before do
57
+ @instance = Hugs.new @valid_options.merge(:type => :json)
58
+ end
51
59
 
52
- it "doesn't set the body when an invalid :body exists" do
53
- @instance.send :response_for, @request, "/", :body => nil
60
+ it "sets the body when a valid :body exists" do
61
+ @instance.send :response_for, @request, "/", :body => {:foo => :bar}
54
62
 
55
- @instance.instance_variable_get(:@request).body.must_be_nil
63
+ assert_requested :get, "https://example.com:80/", :body => '{"foo":"bar"}'
64
+ end
65
+ end
66
+
67
+ describe "xml" do
68
+ before do
69
+ @instance = Hugs.new @valid_options.merge(:type => :xml)
70
+ end
71
+
72
+ it "sets the body when a valid :body exists" do
73
+ @instance.send :response_for, @request, "/", :body => "foo bar"
74
+
75
+ assert_requested :get, "https://example.com:80/", :body => "foo bar"
76
+ end
77
+ end
56
78
  end
57
79
 
58
80
  describe "headers" do
59
- it "has authentication" do
81
+ it "authenticates" do
82
+ stub_request :get, "https://user:credentials@example.com:80/"
83
+
84
+ @instance = Hugs.new @valid_options.merge(:user => "user", :password => "credentials")
85
+
60
86
  @instance.send :response_for, @request, "/", {}
61
87
 
62
- @instance.instance_variable_get(:@request).get_fields("authorization").join.
63
- must_match %r{Basic #{::Base64.encode64("#{@user}:#{@password}").delete("\r\n")}}
88
+ assert_requested :get, "https://user:credentials@example.com:80/"
64
89
  end
65
90
 
66
91
  [:user, :password].each do |option|
67
92
  it "doesn't authenticate when '#{option}' missing" do
93
+ stub_request :get, "https://example.com:80/"
94
+
68
95
  invalid_options = @valid_options.reject { |k,v| k == option }
69
- @instance = ::Hugs.new invalid_options
96
+ @instance = Hugs.new invalid_options
70
97
 
71
98
  @instance.send :response_for, @request, "/", {}
72
99
 
73
- @instance.instance_variable_get(:@request).get_fields("authorization").must_be_nil
100
+ assert_requested :get, "https://example.com:80/"
74
101
  end
75
102
  end
76
103
 
@@ -79,33 +106,67 @@ describe Hugs do
79
106
  { :xml => "application/xml" },
80
107
  ].each do |pair|
81
108
  pair.each_pair do |type, subtype|
82
- [::Net::HTTP::Post, ::Net::HTTP::Put].each do |clazz|
83
- it "has '#{subtype}' content-type and accept for '#{clazz}'" do
84
- @instance = ::Hugs.new @valid_options.merge(:type => type)
109
+ [:post, :put].each do |verb|
110
+ clazz = eval "Net::HTTP::#{verb.capitalize}"
111
+
112
+ it "has '#{subtype}' Content-Type and Accept for '#{clazz}'" do
113
+ stub_request verb, "https://example.com:80/"
114
+
115
+ @instance = Hugs.new @valid_options.merge(:type => type)
85
116
 
86
117
  @instance.send :response_for, clazz, "/", {}
87
118
 
88
- @instance.instance_variable_get(:@request).get_fields("content-type").join.
89
- must_match %r{#{subtype}}
90
- @instance.instance_variable_get(:@request).get_fields("accept").join.
91
- must_match %r{#{subtype}}
119
+ assert_requested verb, "https://example.com:80/", :headers => {
120
+ "Accept" => ["*/*", subtype], "Content-Type" => subtype }
92
121
  end
93
122
  end
94
123
 
95
- [::Net::HTTP::Get, ::Net::HTTP::Delete].each do |clazz|
96
- it "has '#{subtype}' accept for '#{clazz}'" do
97
- @instance = ::Hugs.new @valid_options.merge(:type => type)
124
+ [:get, :delete].each do |verb|
125
+ clazz = eval "Net::HTTP::#{verb.capitalize}"
126
+
127
+ it "has '#{subtype}' Accept for '#{clazz}'" do
128
+ stub_request verb, "https://example.com:80/"
129
+
130
+ @instance = Hugs.new @valid_options.merge(:type => type)
98
131
 
99
132
  @instance.send :response_for, clazz, "/", {}
100
133
 
101
- @instance.instance_variable_get(:@request).get_fields("accept").join.
102
- must_match %r{#{subtype}}
103
- @instance.instance_variable_get(:@request).get_fields("content-type").
104
- must_be_nil
134
+ assert_requested verb, "https://example.com:80/", :headers => {
135
+ "Accept" => ["*/*", subtype] }
105
136
  end
106
137
  end
107
138
  end
108
139
  end
109
140
  end
110
141
  end
142
+
143
+ describe "HTTP methods" do
144
+ describe "json" do
145
+ before do
146
+ @instance = Hugs.new @valid_options.merge(:type => :json)
147
+ end
148
+
149
+ it "objectifies the json and returns a hash" do
150
+ stub_request(:get, "https://example.com:80/").to_return :body => '{"foo":"bar"}'
151
+
152
+ response = @instance.get "/", :body => { :foo => :bar }
153
+
154
+ response.body.must_be_kind_of Hash
155
+ end
156
+ end
157
+
158
+ describe "xml" do
159
+ before do
160
+ @instance = Hugs.new @valid_options.merge(:type => :xml)
161
+ end
162
+
163
+ it "parses xml and returns a Nokogiri object" do
164
+ stub_request(:get, "https://example.com:80/").to_return :body => "<STORAGE></STORAGE>"
165
+
166
+ response = @instance.get "/", :body => { :foo => :bar }
167
+
168
+ response.body.must_be_kind_of Nokogiri::XML::Document
169
+ end
170
+ end
171
+ end
111
172
  end
metadata CHANGED
@@ -3,10 +3,10 @@ name: hugs
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 1
7
- - 3
8
- - 1
9
- version: 1.3.1
6
+ - 2
7
+ - 0
8
+ - 0
9
+ version: 2.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - retr0h
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-15 00:00:00 -08:00
17
+ date: 2010-12-16 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -106,17 +106,17 @@ dependencies:
106
106
  prerelease: false
107
107
  version_requirements: *id006
108
108
  - !ruby/object:Gem::Dependency
109
- name: mocha
109
+ name: webmock
110
110
  requirement: &id007 !ruby/object:Gem::Requirement
111
111
  none: false
112
112
  requirements:
113
- - - ~>
113
+ - - "="
114
114
  - !ruby/object:Gem::Version
115
115
  segments:
116
- - 0
117
- - 9
118
- - 10
119
- version: 0.9.10
116
+ - 1
117
+ - 6
118
+ - 1
119
+ version: 1.6.1
120
120
  type: :development
121
121
  prerelease: false
122
122
  version_requirements: *id007