Class: HypeText
- Inherits:
-
Object
- Object
- HypeText
- Defined in:
- lib/hypetext.rb
Instance Attribute Summary collapse
-
#full_call ⇒ Object
readonly
Returns the value of attribute full_call.
-
#last_call ⇒ Object
readonly
Returns the value of attribute last_call.
-
#rootCA ⇒ Object
Returns the value of attribute rootCA.
-
#ssl_verify ⇒ Object
Returns the value of attribute ssl_verify.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #get_by_url(url, p = {}) ⇒ Object
-
#initialize(params = {}) ⇒ HypeText
constructor
A new instance of HypeText.
- #post_by_url(url, params, p = {}) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ HypeText
Returns a new instance of HypeText.
10 11 12 13 14 15 16 17 18 |
# File 'lib/hypetext.rb', line 10 def initialize(params={}) # set required settings @rootCA = params[:rootCA] ||= nil @timeout = params[:timeout] ||= 0 @ssl_verify = true unless params[:ssl_verify].nil? @ssl_verify = params[:ssl_verify] end end |
Instance Attribute Details
#full_call ⇒ Object (readonly)
Returns the value of attribute full_call.
8 9 10 |
# File 'lib/hypetext.rb', line 8 def full_call @full_call end |
#last_call ⇒ Object (readonly)
Returns the value of attribute last_call.
8 9 10 |
# File 'lib/hypetext.rb', line 8 def last_call @last_call end |
#rootCA ⇒ Object
Returns the value of attribute rootCA.
7 8 9 |
# File 'lib/hypetext.rb', line 7 def rootCA @rootCA end |
#ssl_verify ⇒ Object
Returns the value of attribute ssl_verify.
7 8 9 |
# File 'lib/hypetext.rb', line 7 def ssl_verify @ssl_verify end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/hypetext.rb', line 7 def timeout @timeout end |
Instance Method Details
#get_by_url(url, p = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hypetext.rb', line 20 def get_by_url(url, p={}) # Simple method for getting data by url # url is required, p = {:params=>{}, :headers={}, :timeout=0} # returns hash = {"response"=>NET::HTTP:RESPONSE, "url"=>string, "body"=>NET::HTTP:RESPONSE.body} # Set required settings params = p[:params] ||= {} headers = p[:headers] ||= {} timeout = p[:timeout] ||= @timeout # parse out uri stuff u = URI.parse(url) path = u.path unless params.empty? # Add query params if they exist a = [] params.each do |k,v| a << "#{k}=#{v}" end path = "#{path}?#{a.join('&')}" end # Setup Http http = Net::HTTP.new(u.host, u.port) if url.match(/^https/) # If its an ssl url.. time setup up the ssl http = Net::HTTP.new(u.host, 443) http.use_ssl = true http.ca_file = @rootCA if @ssl_verify # set verification http.verify_mode = OpenSSL::SSL::VERIFY_PEER else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.verify_depth = 5 # Protect against stack looping... end begin Timeout::timeout(timeout) do # Request the url resp, data = http.get(path, headers) @full_call = "#{u.host}:#{u.port}#{path}" @last_call = @full_call if resp.code.match /^301|^302/ a = self.get_by_url(resp.header['location']) end if a return {:response=>a[:response], :url=>a[:url], :body=>a[:body]} end return {:response=>resp, :url=>@full_call, :body=>resp.body} end rescue Timeout::Error # If the call times out.. Catch it and throw an HttpClientException for better handling up the stack raise HypeTextException::TimeoutError.new("Get #{@full_call} Timed Out") end end |
#post_by_url(url, params, p = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/hypetext.rb', line 77 def post_by_url(url, params, p={}) # Simple method for posting data by url # url is required, params is a hash of post params and headers is a hash of headers # returns hash = {"response"=>NET::HTTP:RESPONSE, "url"=>string, "body"=>NET::HTTP:RESPONSE.body} headers = p[:headers] ||= {} timeout = p[:timeout] ||= @timeout if params.empty? raise HypeTextExcetion::MissingPostParams.new("HypeText.post_by_url(url, params) requires parameters.") end # Uri parsing u = URI.parse(url) path = u.path # Construct params a = [] params.each do |key, value| a << "#{key}=#{value}" end post_params = a.join("&") # Setup Http if url.match(/^https/) # If its an ssl url.. time setup up the ssl http = Net::HTTP.new(u.host, u.port) http.use_ssl = true http.ca_file = @rootCA if @ssl_verify # set verification http.verify_mode = OpenSSL::SSL::VERIFY_PEER else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.verify_depth = 5 # Protect against stack looping... else http = Net::HTTP.new(u.host, u.port) end begin Timeout::timeout(timeout) do resp, data = http.post(path, post_params) @full_call = "#{u.host}:#{u.port}#{path}" @last_call = @full_call return {:response=>resp, :url=>@full_call, :body=>resp.body, :params=>params} end rescue Timeout::Error # If the call times out.. Catch it and throw an HttpClientException for better handling up the stack raise HypeTextException::TimeoutError.new("Post to #{@full_call} Timed Out") end end |