easy_http 0.1.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easy_http/response.rb +29 -11
- data/lib/easy_http/session.rb +25 -5
- data/lib/easy_http/version.rb +1 -1
- metadata +10 -31
data/lib/easy_http/response.rb
CHANGED
@@ -3,25 +3,38 @@
|
|
3
3
|
module EasyHTTP
|
4
4
|
class Response
|
5
5
|
|
6
|
-
|
6
|
+
# Requested URL
|
7
|
+
attr_reader :url
|
8
|
+
# Response status code
|
9
|
+
attr_reader :status
|
10
|
+
# Response status text
|
11
|
+
attr_reader :status_message
|
12
|
+
# Response body
|
13
|
+
attr_reader :body
|
14
|
+
# Parsed response headers
|
15
|
+
attr_reader :headers
|
16
|
+
# Response charset
|
17
|
+
attr_reader :charset
|
7
18
|
|
8
19
|
def initialize url, response, default_charset = nil
|
9
20
|
default_charset = "ASCII-8BIT" unless default_charset
|
10
21
|
@url = url
|
11
|
-
|
12
22
|
@status = response.code.to_i
|
13
23
|
@status_message = response.message
|
14
|
-
|
24
|
+
if response["content-type"]
|
25
|
+
@charset = determine_charset(response['content-type'], response.body) || default_charset
|
26
|
+
end
|
27
|
+
@charset = default_charset if @charset.nil?
|
15
28
|
@body = response.body
|
16
|
-
@body = @body.encode default_charset if String.method_defined? :encode
|
17
29
|
|
18
|
-
|
19
|
-
|
20
|
-
if response['Content-type'].nil?
|
21
|
-
@charset = default_charset
|
22
|
-
else
|
23
|
-
@charset = determine_charset(response['Content-type'], response.body) || default_charset
|
30
|
+
if response["content-type"] && response["content-type"][0, 5] == "text/"
|
31
|
+
convert_to_default_encoding! @body
|
24
32
|
end
|
33
|
+
|
34
|
+
@headers = {}
|
35
|
+
response.each_header { |k, v| @headers[k] = v }
|
36
|
+
|
37
|
+
self
|
25
38
|
end
|
26
39
|
|
27
40
|
def inspect
|
@@ -29,7 +42,6 @@ module EasyHTTP
|
|
29
42
|
end
|
30
43
|
|
31
44
|
private
|
32
|
-
|
33
45
|
def determine_charset(header_data, body)
|
34
46
|
header_data.match(charset_regex) || (body && body.match(charset_regex))
|
35
47
|
$1
|
@@ -39,5 +51,11 @@ module EasyHTTP
|
|
39
51
|
/(?:charset|encoding)="?([a-z0-9-]+)"?/i
|
40
52
|
end
|
41
53
|
|
54
|
+
def convert_to_default_encoding!(str)
|
55
|
+
if str.respond_to?(:encode) && Encoding.default_internal
|
56
|
+
str.force_encoding(charset).encode!(Encoding.default_internal)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
42
60
|
end
|
43
61
|
end
|
data/lib/easy_http/session.rb
CHANGED
@@ -26,6 +26,9 @@ module EasyHTTP
|
|
26
26
|
# Default request charset
|
27
27
|
attr_accessor :default_response_charset
|
28
28
|
|
29
|
+
# Control redirects
|
30
|
+
attr_accessor :max_redirs
|
31
|
+
|
29
32
|
def initialize url, options = {}
|
30
33
|
@cookies = nil # Initialize cookies store to nil
|
31
34
|
@default_headers = {
|
@@ -46,6 +49,8 @@ module EasyHTTP
|
|
46
49
|
|
47
50
|
@uri = URI(@base_url) # Parse URI Object
|
48
51
|
|
52
|
+
@max_redirs = options[:max_redirs] || 5
|
53
|
+
|
49
54
|
create_ua
|
50
55
|
end
|
51
56
|
|
@@ -83,7 +88,7 @@ module EasyHTTP
|
|
83
88
|
end
|
84
89
|
|
85
90
|
# Send an HTTP request
|
86
|
-
def send_request action, path, headers, request_options = {}
|
91
|
+
def send_request action, path, headers, request_options = {}, redir = 0
|
87
92
|
# Parse path to prevent errors
|
88
93
|
path = "/#{path}" unless path.match(/^"\/"/)
|
89
94
|
|
@@ -125,8 +130,23 @@ module EasyHTTP
|
|
125
130
|
unless response.nil?
|
126
131
|
# Store cookies if have enabled store it
|
127
132
|
handle_cookies response unless @cookies.nil?
|
128
|
-
|
129
|
-
|
133
|
+
|
134
|
+
# Generate and return response or follow redir
|
135
|
+
case response
|
136
|
+
when Net::HTTPSuccess
|
137
|
+
return Response.new "#{base_url}#{path}", response, @default_response_charset
|
138
|
+
when Net::HTTPRedirection
|
139
|
+
if redir == @max_redirs
|
140
|
+
return Response.new "#{base_url}#{path}", response, @default_response_charset
|
141
|
+
else
|
142
|
+
puts "redir (#{redir}) from #{path} to #{response['location']}"
|
143
|
+
return send_request action, URI.parse(response['location']).path, headers, request_options, redir + 1
|
144
|
+
|
145
|
+
end
|
146
|
+
else
|
147
|
+
return Response.new "#{base_url}#{path}", response, @default_response_charset
|
148
|
+
end
|
149
|
+
|
130
150
|
end
|
131
151
|
end
|
132
152
|
end
|
@@ -156,13 +176,13 @@ module EasyHTTP
|
|
156
176
|
# Store the cookies
|
157
177
|
parsed_cookies.each do |k,v|
|
158
178
|
cookie_parts = v.to_s.split(";")[0].split("=")
|
159
|
-
@cookies[cookie_parts[
|
179
|
+
@cookies[k] = URI.unescape(cookie_parts[1]) unless cookie_parts[1].nil?
|
160
180
|
end
|
161
181
|
end
|
162
182
|
end
|
163
183
|
|
164
184
|
def format_cookies
|
165
|
-
(@cookies.collect{ |k,v| "#{k}=#{
|
185
|
+
(@cookies.collect{ |k,v| "#{k}=#{v}"}).join("; ")
|
166
186
|
end
|
167
187
|
end
|
168
188
|
end
|
data/lib/easy_http/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Tomas J. Sahagun
|
@@ -15,42 +14,25 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2013-01-13 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
19
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
name: nokogiri
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 9
|
29
|
-
segments:
|
30
|
-
- 1
|
31
|
-
- 5
|
32
|
-
- 5
|
33
|
-
version: 1.5.5
|
34
|
-
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
20
|
- !ruby/object:Gem::Dependency
|
37
21
|
name: rake
|
38
22
|
prerelease: false
|
39
|
-
requirement: &
|
40
|
-
none: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
41
24
|
requirements:
|
42
25
|
- - ~>
|
43
26
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 63
|
45
27
|
segments:
|
46
28
|
- 0
|
47
29
|
- 9
|
48
30
|
- 2
|
49
31
|
version: 0.9.2
|
50
32
|
type: :development
|
51
|
-
version_requirements: *
|
33
|
+
version_requirements: *id001
|
52
34
|
description: |-
|
53
|
-
EasyHTTP 0.1.
|
35
|
+
EasyHTTP 0.1.5
|
54
36
|
2012, Tomas J. Sahagun <113.seattle@gmail.com>
|
55
37
|
email: 113.seattle@gmail.com
|
56
38
|
executables: []
|
@@ -64,6 +46,7 @@ files:
|
|
64
46
|
- lib/easy_http/response.rb
|
65
47
|
- lib/easy_http/session.rb
|
66
48
|
- lib/easy_http/version.rb
|
49
|
+
has_rdoc: true
|
67
50
|
homepage: https://github.com/seattle/EasyHTTP
|
68
51
|
licenses: []
|
69
52
|
|
@@ -73,27 +56,23 @@ rdoc_options: []
|
|
73
56
|
require_paths:
|
74
57
|
- lib
|
75
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
59
|
requirements:
|
78
60
|
- - ">="
|
79
61
|
- !ruby/object:Gem::Version
|
80
|
-
hash: 3
|
81
62
|
segments:
|
82
63
|
- 0
|
83
64
|
version: "0"
|
84
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
66
|
requirements:
|
87
67
|
- - ">="
|
88
68
|
- !ruby/object:Gem::Version
|
89
|
-
hash: 3
|
90
69
|
segments:
|
91
70
|
- 0
|
92
71
|
version: "0"
|
93
72
|
requirements: []
|
94
73
|
|
95
74
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.3.6
|
97
76
|
signing_key:
|
98
77
|
specification_version: 3
|
99
78
|
summary: Simple wrapper for Net:HTTP
|