http 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of http might be problematic. Click here for more details.
- data/.gitignore +1 -0
- data/.travis.yml +5 -0
- data/README.md +12 -0
- data/lib/http.rb +2 -0
- data/lib/http/chainable.rb +3 -1
- data/lib/http/client.rb +14 -14
- data/lib/http/response.rb +46 -0
- data/lib/http/version.rb +1 -1
- data/spec/http_spec.rb +8 -0
- metadata +48 -51
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -72,3 +72,15 @@ our URLs because the existing mechanisms make it too hard. It should be easy:
|
|
72
72
|
|
73
73
|
This adds the appropriate Accept header for retrieving a JSON response for the
|
74
74
|
given resource.
|
75
|
+
|
76
|
+
Contributing to Http
|
77
|
+
--------------------
|
78
|
+
|
79
|
+
* Fork Http on github
|
80
|
+
* Make your changes and send me a pull request
|
81
|
+
* If I like them I'll merge them and give you commit access to my repository
|
82
|
+
|
83
|
+
Copyright
|
84
|
+
---------
|
85
|
+
|
86
|
+
Copyright (c) 2011 Tony Arcieri, Carl Lerche. See LICENSE.txt for further details.
|
data/lib/http.rb
CHANGED
data/lib/http/chainable.rb
CHANGED
@@ -2,7 +2,7 @@ module Http
|
|
2
2
|
module Chainable
|
3
3
|
# Request a get sans response body
|
4
4
|
def head(uri, options = {})
|
5
|
-
request :head, uri, options
|
5
|
+
request :head, uri, {:response => :object}.merge(options)
|
6
6
|
end
|
7
7
|
|
8
8
|
# Get a resource
|
@@ -47,6 +47,8 @@ module Http
|
|
47
47
|
|
48
48
|
# Make an HTTP request with the given verb
|
49
49
|
def request(verb, uri, options = {})
|
50
|
+
options[:response] ||= :parsed_body
|
51
|
+
|
50
52
|
if options[:headers]
|
51
53
|
headers = default_headers.merge options[:headers]
|
52
54
|
else
|
data/lib/http/client.rb
CHANGED
@@ -10,7 +10,7 @@ module Http
|
|
10
10
|
@uri = URI(uri)
|
11
11
|
end
|
12
12
|
|
13
|
-
@options = {:response => :
|
13
|
+
@options = {:response => :object}.merge(options)
|
14
14
|
end
|
15
15
|
|
16
16
|
# Request a get sans response body
|
@@ -77,24 +77,24 @@ module Http
|
|
77
77
|
request = request_class.new(@uri.request_uri, headers)
|
78
78
|
request.set_form_data(options[:form]) if options[:form]
|
79
79
|
|
80
|
-
|
80
|
+
net_http_response = http.request(request)
|
81
|
+
|
82
|
+
response = Http::Response.new
|
83
|
+
net_http_response.each_header do |header, value|
|
84
|
+
response[header] = value
|
85
|
+
end
|
86
|
+
response.status = Integer(net_http_response.code) # WTF again Net::HTTP
|
87
|
+
response.body = net_http_response.body
|
81
88
|
|
82
89
|
case options[:response]
|
90
|
+
when :object
|
91
|
+
response
|
83
92
|
when :parsed_body
|
84
|
-
|
85
|
-
|
93
|
+
response.parse_body
|
94
|
+
when :body
|
86
95
|
response.body
|
96
|
+
else raise ArgumentError, "invalid response type: #{options[:response]}"
|
87
97
|
end
|
88
98
|
end
|
89
|
-
|
90
|
-
# Parse the response body according to its content type
|
91
|
-
def parse_response(response)
|
92
|
-
if response['content-type']
|
93
|
-
mime_type = MimeType[response['content-type'].split(/;\s*/).first]
|
94
|
-
return mime_type.parse(response.body) if mime_type
|
95
|
-
end
|
96
|
-
|
97
|
-
response.body
|
98
|
-
end
|
99
99
|
end
|
100
100
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Http
|
2
|
+
class Response
|
3
|
+
attr_accessor :status
|
4
|
+
attr_accessor :headers
|
5
|
+
attr_accessor :body
|
6
|
+
|
7
|
+
# Status aliases! TIMTOWTDI!!! (Want to be idiomatic? Just use status :)
|
8
|
+
alias_method :code, :status
|
9
|
+
alias_method :code=, :status=
|
10
|
+
|
11
|
+
alias_method :status_code, :status
|
12
|
+
alias_method :status_code=, :status
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@headers = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set a header value
|
19
|
+
def []=(header, value)
|
20
|
+
key = header.to_s.downcase
|
21
|
+
|
22
|
+
# Check if the header has already been set and group
|
23
|
+
old_value = @headers[key]
|
24
|
+
if old_value
|
25
|
+
@headers[key] = [old_value].flatten << key
|
26
|
+
else
|
27
|
+
@headers[key] = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get a header value
|
32
|
+
def [](header)
|
33
|
+
@headers[header.to_s.downcase]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Parse the response body according to its content type
|
37
|
+
def parse_body
|
38
|
+
if @headers['content-type']
|
39
|
+
mime_type = MimeType[@headers['content-type'].split(/;\s*/).first]
|
40
|
+
return mime_type.parse(@body) if mime_type
|
41
|
+
end
|
42
|
+
|
43
|
+
@body
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/http/version.rb
CHANGED
data/spec/http_spec.rb
CHANGED
@@ -27,4 +27,12 @@ describe Http do
|
|
27
27
|
response.should match(/HTML5/)
|
28
28
|
end
|
29
29
|
end
|
30
|
+
|
31
|
+
context "head requests" do
|
32
|
+
it "should be easy" do
|
33
|
+
response = Http.head "http://www.google.com"
|
34
|
+
response.status.should == 200
|
35
|
+
response['content-type'].should match(/html/)
|
36
|
+
end
|
37
|
+
end
|
30
38
|
end
|
metadata
CHANGED
@@ -1,63 +1,61 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: http
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Tony Arcieri
|
9
9
|
- Carl Lerche
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2011-11-30 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
17
16
|
name: rake
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &70233139252940 !ruby/object:Gem::Requirement
|
20
18
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
25
23
|
type: :development
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rspec
|
29
24
|
prerelease: false
|
30
|
-
|
25
|
+
version_requirements: *70233139252940
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &70233139252380 !ruby/object:Gem::Requirement
|
31
29
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
35
33
|
version: 2.6.0
|
36
34
|
type: :development
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: json
|
40
35
|
prerelease: false
|
41
|
-
|
36
|
+
version_requirements: *70233139252380
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: json
|
39
|
+
requirement: &70233139251960 !ruby/object:Gem::Requirement
|
42
40
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version:
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
47
45
|
type: :development
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70233139251960
|
48
|
+
description: HTTP so awesome it will lure Catherine Zeta Jones into your unicorn petting
|
49
|
+
zoo
|
50
|
+
email:
|
51
51
|
- tony.arcieri@gmail.com
|
52
52
|
executables: []
|
53
|
-
|
54
53
|
extensions: []
|
55
|
-
|
56
54
|
extra_rdoc_files: []
|
57
|
-
|
58
|
-
files:
|
55
|
+
files:
|
59
56
|
- .gitignore
|
60
57
|
- .rspec
|
58
|
+
- .travis.yml
|
61
59
|
- CHANGES.md
|
62
60
|
- Gemfile
|
63
61
|
- LICENSE.txt
|
@@ -70,6 +68,7 @@ files:
|
|
70
68
|
- lib/http/mime_type.rb
|
71
69
|
- lib/http/mime_types/json.rb
|
72
70
|
- lib/http/parameters.rb
|
71
|
+
- lib/http/response.rb
|
73
72
|
- lib/http/version.rb
|
74
73
|
- parser/common.rl
|
75
74
|
- parser/http.rl
|
@@ -78,31 +77,29 @@ files:
|
|
78
77
|
- spec/spec_helper.rb
|
79
78
|
homepage: https://github.com/tarcieri/http
|
80
79
|
licenses: []
|
81
|
-
|
82
80
|
post_install_message:
|
83
81
|
rdoc_options: []
|
84
|
-
|
85
|
-
require_paths:
|
82
|
+
require_paths:
|
86
83
|
- lib
|
87
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
85
|
none: false
|
89
|
-
requirements:
|
90
|
-
- -
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version:
|
93
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
91
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
version:
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
99
96
|
requirements: []
|
100
|
-
|
101
97
|
rubyforge_project:
|
102
98
|
rubygems_version: 1.8.10
|
103
99
|
signing_key:
|
104
100
|
specification_version: 3
|
105
101
|
summary: Made entirely of Ruby (and Ragel and C and Java)
|
106
|
-
test_files:
|
102
|
+
test_files:
|
107
103
|
- spec/http_spec.rb
|
108
104
|
- spec/spec_helper.rb
|
105
|
+
has_rdoc:
|