hypetext 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hypetext.rb +138 -0
- metadata +46 -0
data/lib/hypetext.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
class HypeText
|
6
|
+
|
7
|
+
attr_accessor :ssl_verify, :rootCA, :timeout, :ssl_verify
|
8
|
+
attr_reader :last_call, :full_call
|
9
|
+
|
10
|
+
def initialize(params={})
|
11
|
+
# set required settings
|
12
|
+
@rootCA = params[:rootCA] ||= nil
|
13
|
+
@timeout = params[:timeout] ||= 0
|
14
|
+
@ssl_verify = true
|
15
|
+
unless params[:ssl_verify].nil?
|
16
|
+
@ssl_verify = params[:ssl_verify]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_by_url(url, p={})
|
21
|
+
# Simple method for getting data by url
|
22
|
+
# url is required, p = {:params=>{}, :headers={}, :timeout=0}
|
23
|
+
# returns hash = {"response"=>NET::HTTP:RESPONSE, "url"=>string, "body"=>NET::HTTP:RESPONSE.body}
|
24
|
+
|
25
|
+
# Set required settings
|
26
|
+
params = p[:params] ||= {}
|
27
|
+
headers = p[:headers] ||= {}
|
28
|
+
timeout = p[:timeout] ||= @timeout
|
29
|
+
|
30
|
+
# parse out uri stuff
|
31
|
+
u = URI.parse(url)
|
32
|
+
path = u.path
|
33
|
+
unless params.empty?
|
34
|
+
# Add query params if they exist
|
35
|
+
a = []
|
36
|
+
params.each do |k,v|
|
37
|
+
a << "#{k}=#{v}"
|
38
|
+
end
|
39
|
+
path = "#{path}?#{a.join('&')}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# Setup Http
|
43
|
+
http = Net::HTTP.new(u.host, u.port)
|
44
|
+
if url.match(/^https/)
|
45
|
+
# If its an ssl url.. time setup up the ssl
|
46
|
+
http = Net::HTTP.new(u.host, 443)
|
47
|
+
http.use_ssl = true
|
48
|
+
http.ca_file = @rootCA
|
49
|
+
if @ssl_verify
|
50
|
+
# set verification
|
51
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
52
|
+
else
|
53
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
54
|
+
end
|
55
|
+
http.verify_depth = 5 # Protect against stack looping...
|
56
|
+
end
|
57
|
+
begin
|
58
|
+
Timeout::timeout(timeout) do
|
59
|
+
# Request the url
|
60
|
+
resp, data = http.get(path, headers)
|
61
|
+
@full_call = "#{u.host}:#{u.port}#{path}"
|
62
|
+
@last_call = @full_call
|
63
|
+
if resp.code.match /^301|^302/
|
64
|
+
a = self.get_by_url(resp.header['location'])
|
65
|
+
end
|
66
|
+
if a
|
67
|
+
return {:response=>a[:response], :url=>a[:url], :body=>a[:body]}
|
68
|
+
end
|
69
|
+
return {:response=>resp, :url=>@full_call, :body=>resp.body}
|
70
|
+
end
|
71
|
+
rescue Timeout::Error
|
72
|
+
# If the call times out.. Catch it and throw an HttpClientException for better handling up the stack
|
73
|
+
raise HypeTextException::TimeoutError.new("Get #{@full_call} Timed Out")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def post_by_url(url, params, p={})
|
78
|
+
# Simple method for posting data by url
|
79
|
+
# url is required, params is a hash of post params and headers is a hash of headers
|
80
|
+
# returns hash = {"response"=>NET::HTTP:RESPONSE, "url"=>string, "body"=>NET::HTTP:RESPONSE.body}
|
81
|
+
headers = p[:headers] ||= {}
|
82
|
+
timeout = p[:timeout] ||= @timeout
|
83
|
+
if params.empty?
|
84
|
+
raise HypeTextExcetion::MissingPostParams.new("HypeText.post_by_url(url, params) requires parameters.")
|
85
|
+
end
|
86
|
+
|
87
|
+
# Uri parsing
|
88
|
+
u = URI.parse(url)
|
89
|
+
path = u.path
|
90
|
+
|
91
|
+
# Construct params
|
92
|
+
a = []
|
93
|
+
params.each do |key, value|
|
94
|
+
a << "#{key}=#{value}"
|
95
|
+
end
|
96
|
+
post_params = a.join("&")
|
97
|
+
|
98
|
+
# Setup Http
|
99
|
+
if url.match(/^https/)
|
100
|
+
# If its an ssl url.. time setup up the ssl
|
101
|
+
http = Net::HTTP.new(u.host, u.port)
|
102
|
+
http.use_ssl = true
|
103
|
+
http.ca_file = @rootCA
|
104
|
+
if @ssl_verify
|
105
|
+
# set verification
|
106
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
107
|
+
else
|
108
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
109
|
+
end
|
110
|
+
http.verify_depth = 5 # Protect against stack looping...
|
111
|
+
else
|
112
|
+
http = Net::HTTP.new(u.host, u.port)
|
113
|
+
end
|
114
|
+
begin
|
115
|
+
Timeout::timeout(timeout) do
|
116
|
+
resp, data = http.post(path, post_params)
|
117
|
+
@full_call = "#{u.host}:#{u.port}#{path}"
|
118
|
+
@last_call = @full_call
|
119
|
+
return {:response=>resp, :url=>@full_call, :body=>resp.body, :params=>params}
|
120
|
+
end
|
121
|
+
rescue Timeout::Error
|
122
|
+
# If the call times out.. Catch it and throw an HttpClientException for better handling up the stack
|
123
|
+
raise HypeTextException::TimeoutError.new("Post to #{@full_call} Timed Out")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
class HypeTextException < Exception
|
131
|
+
|
132
|
+
class MissingPostParams < HypeTextException
|
133
|
+
end
|
134
|
+
|
135
|
+
class TimeoutError < HypeTextException
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hypetext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Heijmans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A gem that provides a simple and dirty HTTP client that handles get,
|
15
|
+
post, and automatic SSL
|
16
|
+
email: parabuzzle@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/hypetext.rb
|
22
|
+
homepage: https://github.com/parabuzzle/hypetext
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Simple dirty HTTP client library
|
46
|
+
test_files: []
|