easy_http 0.1.2
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/lib/easy_http/response.rb +43 -0
- data/lib/easy_http/session.rb +168 -0
- data/lib/easy_http/version.rb +23 -0
- data/lib/easy_http.rb +19 -0
- metadata +101 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module EasyHTTP
|
4
|
+
class Response
|
5
|
+
|
6
|
+
attr_reader :url, :status, :status_message, :body, :headers, :charset
|
7
|
+
|
8
|
+
def initialize url, response, default_charset = nil
|
9
|
+
default_charset = "ASCII-8BIT" unless default_charset
|
10
|
+
@url = url
|
11
|
+
|
12
|
+
@status = response.code.to_i
|
13
|
+
@status_message = response.message
|
14
|
+
|
15
|
+
@body = response.body
|
16
|
+
@body = @body.encode default_charset if String.method_defined? :encode
|
17
|
+
|
18
|
+
@headers = {}
|
19
|
+
response.each_header { |k, v| @headers[k] = v}
|
20
|
+
if response['Content-type'].nil?
|
21
|
+
@charset = default_charset
|
22
|
+
else
|
23
|
+
@charset = determine_charset(response['Content-type'], response.body) || default_charset
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
"#<EasyHTTP::Response @status_message='#{@status_message}'>"
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def determine_charset(header_data, body)
|
34
|
+
header_data.match(charset_regex) || (body && body.match(charset_regex))
|
35
|
+
$1
|
36
|
+
end
|
37
|
+
|
38
|
+
def charset_regex
|
39
|
+
/(?:charset|encoding)="?([a-z0-9-]+)"?/i
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module EasyHTTP
|
4
|
+
class Session
|
5
|
+
# Net:HTTP object
|
6
|
+
attr_accessor :http
|
7
|
+
|
8
|
+
# Base URL
|
9
|
+
attr_accessor :base_url
|
10
|
+
|
11
|
+
# Default headers
|
12
|
+
attr_accessor :default_headers
|
13
|
+
|
14
|
+
# URI Objet for requests
|
15
|
+
attr_accessor :uri
|
16
|
+
|
17
|
+
# Options sent to the constructor
|
18
|
+
attr_accessor :options
|
19
|
+
|
20
|
+
# Session Cookies
|
21
|
+
attr_accessor :cookies
|
22
|
+
|
23
|
+
# HTTP Auth credentials
|
24
|
+
attr_accessor :username, :password
|
25
|
+
|
26
|
+
# Default request charset
|
27
|
+
attr_accessor :default_response_charset
|
28
|
+
|
29
|
+
def initialize url, options = {}
|
30
|
+
@cookies = nil # Initialize cookies store to nil
|
31
|
+
@default_headers = {
|
32
|
+
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11'
|
33
|
+
} # Initialize default headers
|
34
|
+
@options = options # Store Options
|
35
|
+
|
36
|
+
# Store base URL
|
37
|
+
@base_url = url.match(/^http/) ? url : "#{@options[:ssl] ? 'https' : 'http'}://#{url}"
|
38
|
+
|
39
|
+
unless options[:port].nil?
|
40
|
+
@base_url = "#{@base_url}:#{options[:port]}" unless @base_url.match(/":#{options[:port]}"^/)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Store credentials
|
44
|
+
@username = options[:username] unless options[:username].nil?
|
45
|
+
@password = options[:password] unless options[:password].nil?
|
46
|
+
|
47
|
+
@uri = URI(@base_url) # Parse URI Object
|
48
|
+
|
49
|
+
create_ua
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_ua
|
53
|
+
# Create the Session Object
|
54
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
55
|
+
|
56
|
+
# Set passed read timeout
|
57
|
+
@http.read_timeout = options[:read_timeout] ||= 1000
|
58
|
+
|
59
|
+
# Enable debug output
|
60
|
+
@http.set_debug_output options[:debug] unless options[:debug].nil?
|
61
|
+
|
62
|
+
# Enable SSL if necessary
|
63
|
+
@http.use_ssl = true if @options[:ssl]
|
64
|
+
|
65
|
+
# Allow work with insecure servers
|
66
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @options[:insecure]
|
67
|
+
end
|
68
|
+
|
69
|
+
# Initialize store cookies for this session
|
70
|
+
def session_cookies
|
71
|
+
@cookies = {}
|
72
|
+
end
|
73
|
+
|
74
|
+
# Method for GET request
|
75
|
+
def get path, query = nil, headers = {}
|
76
|
+
send_request :get, path, headers, :query => query
|
77
|
+
end
|
78
|
+
|
79
|
+
# Method for POST request
|
80
|
+
def post path, data, headers = {}
|
81
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded' if headers['Content-Type'].nil?
|
82
|
+
send_request :post, path, headers, :data => data
|
83
|
+
end
|
84
|
+
|
85
|
+
# Send an HTTP request
|
86
|
+
def send_request action, path, headers, request_options = {}
|
87
|
+
# Parse path to prevent errors
|
88
|
+
path = "/#{path}" unless path.match(/^"\/"/)
|
89
|
+
|
90
|
+
# Create the request
|
91
|
+
case action
|
92
|
+
when :get
|
93
|
+
# Create a GET method
|
94
|
+
request = Net::HTTP::Get.new(path)
|
95
|
+
when :post
|
96
|
+
# Create a POST method
|
97
|
+
request = Net::HTTP::Post.new(path)
|
98
|
+
|
99
|
+
# Set data to send
|
100
|
+
if request_options[:data].is_a?(Hash)
|
101
|
+
request.set_form_data request_options[:data]
|
102
|
+
elsif request_options[:data].is_a?(String)
|
103
|
+
request.body = request_options[:data]
|
104
|
+
end
|
105
|
+
|
106
|
+
else
|
107
|
+
# Raises exception?¿
|
108
|
+
end
|
109
|
+
|
110
|
+
unless request.nil?
|
111
|
+
# Enable auth if user parameter is set on constructor
|
112
|
+
request.basic_auth(@username, @password) unless @username.nil?
|
113
|
+
|
114
|
+
# Set rdefault and equest headers
|
115
|
+
@default_headers.each { |k,v| request[k] = v}
|
116
|
+
headers.each { |k,v| request[k] = v}
|
117
|
+
|
118
|
+
# If we have activated store cookie, we define it for the request
|
119
|
+
unless @cookies.nil?
|
120
|
+
request['Cookie'] = request['Cookie'].nil? ? format_cookies : [request['Cookie'], format_cookies].join("; ")
|
121
|
+
end
|
122
|
+
# make request
|
123
|
+
response = http.request(request)
|
124
|
+
|
125
|
+
unless response.nil?
|
126
|
+
# Store cookies if have enabled store it
|
127
|
+
handle_cookies response unless @cookies.nil?
|
128
|
+
# Generate and return response
|
129
|
+
return Response.new "#{base_url}#{path}", response, @default_response_charset
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def marshal_dump
|
135
|
+
[ @base_url, @default_headers, @uri, @options, @cookies , @username, @password, @default_response_charset ]
|
136
|
+
end
|
137
|
+
|
138
|
+
def marshal_load data
|
139
|
+
@base_url, @default_headers, @uri, @options, @cookies , @username, @password, @default_response_charset = data
|
140
|
+
create_ua
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
def handle_cookies response
|
145
|
+
# CGI don't understand this parameters
|
146
|
+
bad_keys = ['path', 'Path', 'expires', 'Expires']
|
147
|
+
|
148
|
+
# If at the request has sent a cookie we store it
|
149
|
+
unless response['set-cookie'].nil?
|
150
|
+
# Get cookies in the header
|
151
|
+
parsed_cookies = CGI::Cookie::parse response['set-cookie']
|
152
|
+
|
153
|
+
# Remove unnecessary values
|
154
|
+
bad_keys.each { |key| parsed_cookies.delete(key) }
|
155
|
+
|
156
|
+
# Store the cookies
|
157
|
+
parsed_cookies.each do |k,v|
|
158
|
+
cookie_parts = v.to_s.split(";")[0].split("=")
|
159
|
+
@cookies[cookie_parts[0]] = cookie_parts[1]
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def format_cookies
|
165
|
+
(@cookies.collect{ |k,v| "#{k}=#{URI.unescape(v)}"}).join("; ")
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module EasyHTTP
|
4
|
+
|
5
|
+
module Version
|
6
|
+
MAJOR = 0
|
7
|
+
MINOR = 1
|
8
|
+
TINY = 2
|
9
|
+
end
|
10
|
+
|
11
|
+
PROGRAM_NAME = "EasyHTTP"
|
12
|
+
PROGRAM_NAME_LOW = PROGRAM_NAME.downcase
|
13
|
+
PROGRAM_DESC = "Simple wrapper for Net:HTTP"
|
14
|
+
VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].join('.')
|
15
|
+
AUTHOR = "Tomas J. Sahagun"
|
16
|
+
AUTHOR_EMAIL = "113.seattle@gmail.com"
|
17
|
+
DESCRIPTION = "#{PROGRAM_NAME} #{VERSION}\n2012, #{AUTHOR} <#{AUTHOR_EMAIL}>"
|
18
|
+
|
19
|
+
module GemVersion
|
20
|
+
VERSION = ::EasyHTTP::VERSION
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/lib/easy_http.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Include external gems
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
require 'cgi'
|
7
|
+
|
8
|
+
# Include internal modules
|
9
|
+
require 'easy_http/version.rb'
|
10
|
+
require 'easy_http/session.rb'
|
11
|
+
require 'easy_http/response.rb'
|
12
|
+
|
13
|
+
module EasyHTTP
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tomas J. Sahagun
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-12-28 00:00:00 Z
|
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
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 63
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 9
|
48
|
+
- 2
|
49
|
+
version: 0.9.2
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
description: |-
|
53
|
+
EasyHTTP 0.1.2
|
54
|
+
2012, Tomas J. Sahagun <113.seattle@gmail.com>
|
55
|
+
email: 113.seattle@gmail.com
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files: []
|
61
|
+
|
62
|
+
files:
|
63
|
+
- lib/easy_http.rb
|
64
|
+
- lib/easy_http/response.rb
|
65
|
+
- lib/easy_http/session.rb
|
66
|
+
- lib/easy_http/version.rb
|
67
|
+
homepage: https://github.com/seattle/EasyHTTP
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.8.24
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Simple wrapper for Net:HTTP
|
100
|
+
test_files: []
|
101
|
+
|