httpadapter 0.1.1

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.
@@ -0,0 +1,146 @@
1
+ # Copyright (C) 2010 Google Inc.
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 'spec_helper'
16
+
17
+ require 'httpadapter/adapters/typhoeus'
18
+
19
+ describe HTTPAdapter::TyphoeusResponseAdapter do
20
+ it 'should raise an error for converting from an invalid tuple' do
21
+ (lambda do
22
+ HTTPAdapter.specialize_response(
23
+ [200, [], [42]], HTTPAdapter::TyphoeusResponseAdapter
24
+ )
25
+ end).should raise_error(TypeError)
26
+ end
27
+
28
+ it 'should raise an error for converting from an invalid response' do
29
+ (lambda do
30
+ HTTPAdapter.adapt_response(
31
+ 42, HTTPAdapter::TyphoeusResponseAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ describe HTTPAdapter::TyphoeusResponseAdapter,
38
+ 'converting from Typhoeus::Response' do
39
+ before do
40
+ @response = Typhoeus::Response.new(
41
+ :code => 200,
42
+ :headers => <<-HEADERS,
43
+ Content-Type: application/json; charset=utf-8
44
+ Content-Length: 27
45
+ HEADERS
46
+ :body => '{"three":3,"two":2,"one":1}'
47
+ )
48
+ @result = HTTPAdapter.adapt_response(
49
+ @response, HTTPAdapter::TyphoeusResponseAdapter
50
+ )
51
+ @status, @headers, @body = @result
52
+ end
53
+
54
+ it 'should convert the HTTP status properly' do
55
+ @status.should == 200
56
+ end
57
+
58
+ it 'should convert the headers properly' do
59
+ content_type = nil
60
+ @headers.each do |header, value|
61
+ header.should be_kind_of(String)
62
+ value.should be_kind_of(String)
63
+ content_type = value if header == 'Content-Type'
64
+ end
65
+ content_type.should == 'application/json; charset=utf-8'
66
+ end
67
+
68
+ it 'should convert the body properly' do
69
+ merged_body = ""
70
+ @body.each do |chunk|
71
+ merged_body += chunk
72
+ chunk.should be_kind_of(String)
73
+ end
74
+ merged_body.should == '{"three":3,"two":2,"one":1}'
75
+ end
76
+ end
77
+
78
+ describe HTTPAdapter::TyphoeusResponseAdapter,
79
+ 'converting from a 200 tuple' do
80
+ before do
81
+ @tuple = [
82
+ 200,
83
+ [
84
+ ['Content-Type', 'application/json; charset=utf-8'],
85
+ ['Content-Length', '27']
86
+ ],
87
+ ['{"three":3,"two":2,"one":1}']
88
+ ]
89
+ @response = HTTPAdapter.specialize_response(
90
+ @tuple, HTTPAdapter::TyphoeusResponseAdapter
91
+ )
92
+ end
93
+
94
+ it 'should convert the HTTP status properly' do
95
+ @response.code.to_i.should == 200
96
+ end
97
+
98
+ it 'should convert the headers properly' do
99
+ content_type = nil
100
+ @response.headers_hash.each do |header, value|
101
+ header.should be_kind_of(String)
102
+ value.should be_kind_of(String)
103
+ content_type = value if header == 'Content-Type'
104
+ end
105
+ content_type.should == 'application/json; charset=utf-8'
106
+ end
107
+
108
+ it 'should convert the body properly' do
109
+ @response.body.should == '{"three":3,"two":2,"one":1}'
110
+ end
111
+ end
112
+
113
+ describe HTTPAdapter::TyphoeusResponseAdapter,
114
+ 'converting from a 200 tuple' do
115
+ before do
116
+ @tuple = [
117
+ 500,
118
+ [
119
+ ['Content-Type', 'text/html'],
120
+ ['Content-Length', '28']
121
+ ],
122
+ ['<html><body>', '42', '</body></html>']
123
+ ]
124
+ @response = HTTPAdapter.specialize_response(
125
+ @tuple, HTTPAdapter::TyphoeusResponseAdapter
126
+ )
127
+ end
128
+
129
+ it 'should convert the HTTP status properly' do
130
+ @response.code.to_i.should == 500
131
+ end
132
+
133
+ it 'should convert the headers properly' do
134
+ content_type = nil
135
+ @response.headers_hash.each do |header, value|
136
+ header.should be_kind_of(String)
137
+ value.should be_kind_of(String)
138
+ content_type = value if header == 'Content-Type'
139
+ end
140
+ content_type.should == 'text/html'
141
+ end
142
+
143
+ it 'should convert the body properly' do
144
+ @response.body.should == '<html><body>42</body></html>'
145
+ end
146
+ end
@@ -0,0 +1,224 @@
1
+ # Copyright (C) 2010 Google Inc.
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 'spec_helper'
16
+
17
+ require 'httpadapter'
18
+
19
+ class StubAdapter
20
+ def to_ary
21
+ return ['GET', '/', [], [""]]
22
+ end
23
+
24
+ def self.from_ary(array)
25
+ return Object.new
26
+ end
27
+ end
28
+
29
+ class BogusAdapter
30
+ def initialize(request)
31
+ end
32
+ end
33
+
34
+ describe HTTPAdapter, 'when attempting to specialize a request' do
35
+ it 'should raise an error for converting from an invalid tuple' do
36
+ (lambda do
37
+ HTTPAdapter.specialize_request(42, StubAdapter)
38
+ end).should raise_error(TypeError)
39
+ end
40
+
41
+ it 'should raise an error for converting from an invalid tuple' do
42
+ (lambda do
43
+ HTTPAdapter.specialize_request([42], StubAdapter)
44
+ end).should raise_error(TypeError)
45
+ end
46
+
47
+ it 'should raise an error for converting from an invalid tuple' do
48
+ (lambda do
49
+ HTTPAdapter.specialize_request(
50
+ [42, 42, 42, 42], StubAdapter
51
+ )
52
+ end).should raise_error(TypeError)
53
+ end
54
+
55
+ it 'should raise an error for converting from an invalid tuple' do
56
+ (lambda do
57
+ HTTPAdapter.specialize_request(
58
+ ['GET', 42, [], ['']], StubAdapter
59
+ )
60
+ end).should raise_error(TypeError)
61
+ end
62
+
63
+ it 'should raise an error for converting from an invalid tuple' do
64
+ (lambda do
65
+ HTTPAdapter.specialize_request(
66
+ ['GET', '/', 42, ['']], StubAdapter
67
+ )
68
+ end).should raise_error(TypeError)
69
+ end
70
+
71
+ it 'should raise an error for converting from an invalid tuple' do
72
+ (lambda do
73
+ HTTPAdapter.specialize_request(
74
+ ['GET', '/', [42], ['']], StubAdapter
75
+ )
76
+ end).should raise_error(TypeError)
77
+ end
78
+
79
+ it 'should raise an error for converting from an invalid tuple' do
80
+ (lambda do
81
+ HTTPAdapter.specialize_request(
82
+ ['GET', '/', [[42, 'value']], ['']], StubAdapter
83
+ )
84
+ end).should raise_error(TypeError)
85
+ end
86
+
87
+ it 'should raise an error for converting from an invalid tuple' do
88
+ (lambda do
89
+ HTTPAdapter.specialize_request(
90
+ ['GET', '/', [['X', 42]], ['']], StubAdapter
91
+ )
92
+ end).should raise_error(TypeError)
93
+ end
94
+
95
+ it 'should raise an error for converting from an invalid tuple' do
96
+ (lambda do
97
+ HTTPAdapter.specialize_request(
98
+ ['GET', '/', [], 42], StubAdapter
99
+ )
100
+ end).should raise_error(TypeError)
101
+ end
102
+
103
+ it 'should raise an error for converting from an invalid tuple' do
104
+ (lambda do
105
+ HTTPAdapter.specialize_request(
106
+ ['GET', '/', [], ''], StubAdapter
107
+ )
108
+ end).should raise_error(TypeError)
109
+ end
110
+
111
+ it 'should raise an error for converting from an invalid adapter' do
112
+ (lambda do
113
+ HTTPAdapter.specialize_request(
114
+ ['GET', '/', [], ['']], Object
115
+ )
116
+ end).should raise_error(TypeError)
117
+ end
118
+ end
119
+
120
+ describe HTTPAdapter, 'when attempting to adapt a request' do
121
+ it 'should raise an error for converting from an invalid adapter' do
122
+ (lambda do
123
+ HTTPAdapter.adapt_request(
124
+ Object.new, BogusAdapter
125
+ )
126
+ end).should raise_error(TypeError)
127
+ end
128
+ end
129
+
130
+ describe HTTPAdapter, 'when attempting to specialize a response' do
131
+ it 'should raise an error for converting from an invalid tuple' do
132
+ (lambda do
133
+ HTTPAdapter.specialize_response(42, StubAdapter)
134
+ end).should raise_error(TypeError)
135
+ end
136
+
137
+ it 'should raise an error for converting from an invalid tuple' do
138
+ (lambda do
139
+ HTTPAdapter.specialize_response([42], StubAdapter)
140
+ end).should raise_error(TypeError)
141
+ end
142
+
143
+ it 'should raise an error for converting from an invalid tuple' do
144
+ (lambda do
145
+ HTTPAdapter.specialize_response(
146
+ [42, 42, 42], StubAdapter
147
+ )
148
+ end).should raise_error(TypeError)
149
+ end
150
+
151
+ it 'should raise an error for converting from an invalid tuple' do
152
+ (lambda do
153
+ HTTPAdapter.specialize_response(
154
+ [Object.new, [], ['']], StubAdapter
155
+ )
156
+ end).should raise_error(TypeError)
157
+ end
158
+
159
+ it 'should raise an error for converting from an invalid tuple' do
160
+ (lambda do
161
+ HTTPAdapter.specialize_response(
162
+ ['', 42, ['']], StubAdapter
163
+ )
164
+ end).should raise_error(TypeError)
165
+ end
166
+
167
+ it 'should raise an error for converting from an invalid tuple' do
168
+ (lambda do
169
+ HTTPAdapter.specialize_response(
170
+ [200, [42], ['']], StubAdapter
171
+ )
172
+ end).should raise_error(TypeError)
173
+ end
174
+
175
+ it 'should raise an error for converting from an invalid tuple' do
176
+ (lambda do
177
+ HTTPAdapter.specialize_response(
178
+ [200, [[42, 'value']], ['']], StubAdapter
179
+ )
180
+ end).should raise_error(TypeError)
181
+ end
182
+
183
+ it 'should raise an error for converting from an invalid tuple' do
184
+ (lambda do
185
+ HTTPAdapter.specialize_response(
186
+ [200, [['X', 42]], ['']], StubAdapter
187
+ )
188
+ end).should raise_error(TypeError)
189
+ end
190
+
191
+ it 'should raise an error for converting from an invalid tuple' do
192
+ (lambda do
193
+ HTTPAdapter.specialize_response(
194
+ [200, [], 42], StubAdapter
195
+ )
196
+ end).should raise_error(TypeError)
197
+ end
198
+
199
+ it 'should raise an error for converting from an invalid tuple' do
200
+ (lambda do
201
+ HTTPAdapter.specialize_response(
202
+ [200, [], ''], StubAdapter
203
+ )
204
+ end).should raise_error(TypeError)
205
+ end
206
+
207
+ it 'should raise an error for converting from an invalid adapter' do
208
+ (lambda do
209
+ HTTPAdapter.specialize_response(
210
+ [200, [], ['']], Object
211
+ )
212
+ end).should raise_error(TypeError)
213
+ end
214
+ end
215
+
216
+ describe HTTPAdapter, 'when attempting to adapt a response' do
217
+ it 'should raise an error for converting from an invalid adapter' do
218
+ (lambda do
219
+ HTTPAdapter.adapt_response(
220
+ Object.new, BogusAdapter
221
+ )
222
+ end).should raise_error(TypeError)
223
+ end
224
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,7 @@
1
+ spec_dir = File.expand_path(File.dirname(__FILE__))
2
+ lib_dir = File.expand_path(File.join(spec_dir, "../lib"))
3
+
4
+ $:.unshift(lib_dir)
5
+ $:.uniq!
6
+
7
+ require "httpadapter"
@@ -0,0 +1,2 @@
1
+ desc "Remove all build products"
2
+ task "clobber"
data/tasks/gem.rake ADDED
@@ -0,0 +1,73 @@
1
+ require "rake/gempackagetask"
2
+
3
+ namespace :gem do
4
+ GEM_SPEC = Gem::Specification.new do |s|
5
+ unless s.respond_to?(:add_development_dependency)
6
+ puts "The gem spec requires a newer version of RubyGems."
7
+ exit(1)
8
+ end
9
+
10
+ s.name = PKG_NAME
11
+ s.version = PKG_VERSION
12
+ s.author = PKG_AUTHOR
13
+ s.email = PKG_AUTHOR_EMAIL
14
+ s.homepage = PKG_HOMEPAGE
15
+ s.summary = PKG_SUMMARY
16
+ s.description = PKG_DESCRIPTION
17
+ s.rubyforge_project = RUBY_FORGE_PROJECT
18
+
19
+ s.files = PKG_FILES.to_a
20
+
21
+ s.has_rdoc = true
22
+ s.extra_rdoc_files = %w( README )
23
+ s.rdoc_options.concat ["--main", "README"]
24
+
25
+ s.add_runtime_dependency("addressable", "~> 2.2.0")
26
+
27
+ s.add_development_dependency("rake", "~> 0.8.3")
28
+ s.add_development_dependency("rspec", "~> 1.1.11")
29
+ s.add_development_dependency("launchy", "~> 0.3.2")
30
+ s.add_development_dependency("diff-lcs", "~> 1.1.2")
31
+
32
+ s.add_development_dependency("typhoeus", "~> 0.1.31")
33
+ s.add_development_dependency("rack", "~> 1.2.1")
34
+
35
+ s.require_path = "lib"
36
+ end
37
+
38
+ Rake::GemPackageTask.new(GEM_SPEC) do |p|
39
+ p.gem_spec = GEM_SPEC
40
+ p.need_tar = true
41
+ p.need_zip = true
42
+ end
43
+
44
+ desc "Show information about the gem"
45
+ task :debug do
46
+ puts GEM_SPEC.to_ruby
47
+ end
48
+
49
+ desc "Install the gem"
50
+ task :install => ["clobber", "gem:package"] do
51
+ sh "#{SUDO} gem install --local pkg/#{GEM_SPEC.full_name}"
52
+ end
53
+
54
+ desc "Uninstall the gem"
55
+ task :uninstall do
56
+ installed_list = Gem.source_index.find_name(PKG_NAME)
57
+ if installed_list &&
58
+ (installed_list.collect { |s| s.version.to_s}.include?(PKG_VERSION))
59
+ sh(
60
+ "#{SUDO} gem uninstall --version '#{PKG_VERSION}' " +
61
+ "--ignore-dependencies --executables #{PKG_NAME}"
62
+ )
63
+ end
64
+ end
65
+
66
+ desc "Reinstall the gem"
67
+ task :reinstall => [:uninstall, :install]
68
+ end
69
+
70
+ desc "Alias to gem:package"
71
+ task "gem" => "gem:package"
72
+
73
+ task "clobber" => ["gem:clobber_package"]
data/tasks/git.rake ADDED
@@ -0,0 +1,40 @@
1
+ namespace :git do
2
+ namespace :tag do
3
+ desc "List tags from the Git repository"
4
+ task :list do
5
+ tags = `git tag -l`
6
+ tags.gsub!("\r", "")
7
+ tags = tags.split("\n").sort {|a, b| b <=> a }
8
+ puts tags.join("\n")
9
+ end
10
+
11
+ desc "Create a new tag in the Git repository"
12
+ task :create do
13
+ changelog = File.open("CHANGELOG", "r") { |file| file.read }
14
+ puts "-" * 80
15
+ puts changelog
16
+ puts "-" * 80
17
+ puts
18
+
19
+ v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
20
+ abort "Versions don't match #{v} vs #{PKG_VERSION}" if v != PKG_VERSION
21
+
22
+ tag = "#{PKG_NAME}-#{PKG_VERSION}"
23
+ msg = "Release #{PKG_NAME}-#{PKG_VERSION}"
24
+
25
+ existing_tags = `git tag -l #{PKG_NAME}-*`.split("\n")
26
+ if existing_tags.include?(tag)
27
+ warn("Tag already exists, deleting...")
28
+ unless system "git tag -d #{tag}"
29
+ abort "Tag deletion failed."
30
+ end
31
+ end
32
+ puts "Creating git tag '#{tag}'..."
33
+ unless system "git tag -a -m \"#{msg}\" #{tag}"
34
+ abort "Tag creation failed."
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ task "gem:release" => "git:tag:create"
@@ -0,0 +1,22 @@
1
+ namespace :metrics do
2
+ task :lines do
3
+ lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
4
+ for file_name in FileList["lib/**/*.rb"]
5
+ f = File.open(file_name)
6
+ while line = f.gets
7
+ lines += 1
8
+ next if line =~ /^\s*$/
9
+ next if line =~ /^\s*#/
10
+ codelines += 1
11
+ end
12
+ puts "L: #{sprintf("%4d", lines)}, " +
13
+ "LOC #{sprintf("%4d", codelines)} | #{file_name}"
14
+ total_lines += lines
15
+ total_codelines += codelines
16
+
17
+ lines, codelines = 0, 0
18
+ end
19
+
20
+ puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
21
+ end
22
+ end
data/tasks/rdoc.rake ADDED
@@ -0,0 +1,26 @@
1
+ require "rake/rdoctask"
2
+
3
+ namespace :doc do
4
+ desc "Generate RDoc documentation"
5
+ Rake::RDocTask.new do |rdoc|
6
+ rdoc.rdoc_dir = "doc"
7
+ rdoc.title = "#{PKG_NAME}-#{PKG_VERSION} Documentation"
8
+ rdoc.options << "--line-numbers" << "--inline-source" <<
9
+ "--accessor" << "cattr_accessor=object" << "--charset" << "utf-8"
10
+ rdoc.template = "#{ENV["template"]}.rb" if ENV["template"]
11
+ rdoc.rdoc_files.include("README", "CHANGELOG", "LICENSE")
12
+ rdoc.rdoc_files.include("lib/**/*.rb")
13
+ end
14
+
15
+ desc "Generate ri locally for testing"
16
+ task :ri do
17
+ sh "rdoc --ri -o ri ."
18
+ end
19
+
20
+ desc "Remove ri products"
21
+ task :clobber_ri do
22
+ rm_r "ri" rescue nil
23
+ end
24
+ end
25
+
26
+ task "clobber" => ["doc:clobber_rdoc", "doc:clobber_ri"]
@@ -0,0 +1,103 @@
1
+ namespace :gem do
2
+ desc 'Package and upload to RubyForge'
3
+ task :release => ["gem:package"] do |t|
4
+ require 'rubyforge'
5
+
6
+ v = ENV['VERSION'] or abort 'Must supply VERSION=x.y.z'
7
+ abort "Versions don't match #{v} vs #{PROJ.version}" if v != PKG_VERSION
8
+ pkg = "pkg/#{GEM_SPEC.full_name}"
9
+
10
+ rf = RubyForge.new
11
+ rf.configure
12
+ puts 'Logging in...'
13
+ rf.login
14
+
15
+ c = rf.userconfig
16
+ changelog = File.open("CHANGELOG") { |file| file.read }
17
+ c['release_changes'] = changelog
18
+ c['preformatted'] = true
19
+
20
+ files = ["#{pkg}.tgz", "#{pkg}.zip", "#{pkg}.gem"]
21
+
22
+ puts "Releasing #{PKG_NAME} v. #{PKG_VERSION}"
23
+ rf.add_release RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, *files
24
+ end
25
+ end
26
+
27
+ namespace :doc do
28
+ desc "Publish RDoc to RubyForge"
29
+ task :release => ["doc"] do
30
+ require "rake/contrib/sshpublisher"
31
+ require "yaml"
32
+
33
+ config = YAML.load(
34
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
35
+ )
36
+ host = "#{config['username']}@rubyforge.org"
37
+ remote_dir = RUBY_FORGE_PATH + "/api"
38
+ local_dir = "doc"
39
+ if !File.exist?("website/api")
40
+ system("mkdir website/api")
41
+ end
42
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
43
+ end
44
+ end
45
+
46
+ namespace :spec do
47
+ desc "Publish specdoc to RubyForge"
48
+ task :release => ["spec:specdoc"] do
49
+ require "rake/contrib/sshpublisher"
50
+ require "yaml"
51
+
52
+ config = YAML.load(
53
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
54
+ )
55
+ host = "#{config['username']}@rubyforge.org"
56
+ remote_dir = RUBY_FORGE_PATH + "/specdoc"
57
+ local_dir = "specdoc"
58
+ if !File.exist?("website/specdoc")
59
+ system("mkdir website/specdoc")
60
+ end
61
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
62
+ end
63
+
64
+ namespace :rcov do
65
+ desc "Publish coverage report to RubyForge"
66
+ task :release => ["spec:rcov"] do
67
+ require "rake/contrib/sshpublisher"
68
+ require "yaml"
69
+
70
+ config = YAML.load(
71
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
72
+ )
73
+ host = "#{config['username']}@rubyforge.org"
74
+ remote_dir = RUBY_FORGE_PATH + "/coverage"
75
+ local_dir = "coverage"
76
+ if !File.exist?("website/coverage")
77
+ system("mkdir website/coverage")
78
+ end
79
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
80
+ end
81
+ end
82
+ end
83
+
84
+ namespace :website do
85
+ desc "Publish website to RubyForge"
86
+ task :init do
87
+ require "rake/contrib/sshpublisher"
88
+ require "yaml"
89
+
90
+ config = YAML.load(
91
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
92
+ )
93
+ host = "#{config['username']}@rubyforge.org"
94
+ remote_dir = RUBY_FORGE_PATH
95
+ local_dir = "website"
96
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
97
+ end
98
+
99
+ desc "Publish website to RubyForge"
100
+ task :release => [
101
+ "website:init", "doc:release", "spec:release", "spec:rcov:release"
102
+ ]
103
+ end