Class: HttpSimple::Http
- Inherits:
-
Object
- Object
- HttpSimple::Http
- Defined in:
- lib/httpsimple.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#follow_redirects ⇒ Object
Returns the value of attribute follow_redirects.
-
#handlers ⇒ Object
Returns the value of attribute handlers.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#strict_ssl ⇒ Object
Returns the value of attribute strict_ssl.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #get(url, params = nil) ⇒ Object
- #handle(*status_codes, &handler) ⇒ Object
-
#initialize ⇒ Http
constructor
A new instance of Http.
- #post(url, body = nil) ⇒ Object
- #remove_handler(*status_codes) ⇒ Object
Constructor Details
#initialize ⇒ Http
Returns a new instance of Http.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/httpsimple.rb', line 38 def initialize @headers = {} @max_redirects = 3 @follow_redirects = true @strict_ssl = true @timeout = 90 # If debug is set to true ``set_debug_output`` # will be enabled and write to $stderr @debug = false # Response handlers @handlers = {} @set_cookie_headers = [] end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def debug @debug end |
#follow_redirects ⇒ Object
Returns the value of attribute follow_redirects.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def follow_redirects @follow_redirects end |
#handlers ⇒ Object
Returns the value of attribute handlers.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def handlers @handlers end |
#headers ⇒ Object
Returns the value of attribute headers.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def headers @headers end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def max_redirects @max_redirects end |
#strict_ssl ⇒ Object
Returns the value of attribute strict_ssl.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def strict_ssl @strict_ssl end |
#timeout ⇒ Object
Returns the value of attribute timeout.
34 35 36 |
# File 'lib/httpsimple.rb', line 34 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
36 37 38 |
# File 'lib/httpsimple.rb', line 36 def url @url end |
Instance Method Details
#get(url, params = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/httpsimple.rb', line 66 def get(url, params=nil) uri = URI(url) uri.query = URI.encode_www_form(params) unless params.nil? request = Net::HTTP::Get.new(uri.request_uri) if block_given? yield uri, request else fetch(uri, request, @max_redirects) end end |
#handle(*status_codes, &handler) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/httpsimple.rb', line 54 def handle(*status_codes, &handler) # Add response handle for http status code. ie 200, 302, 400 if block_given? status_codes.each { |code| @handlers[code.to_s.to_sym] = handler } end end |
#post(url, body = nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/httpsimple.rb', line 79 def post(url, body=nil) uri = URI(url) request = Net::HTTP::Post.new(uri.request_uri) case body when String request.body = body when Hash request.set_form_data(body) end if block_given? yield uri, request else fetch(uri, request, @max_redirects) end end |
#remove_handler(*status_codes) ⇒ Object
61 62 63 64 |
# File 'lib/httpsimple.rb', line 61 def remove_handler(*status_codes) # Remove response handler for http status code. status_codes.each { |code| @handlers.delete(code.to_s.to_sym) } end |