Class: Requesta
- Inherits:
-
Object
- Object
- Requesta
- Defined in:
- lib/requesta.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#password ⇒ Object
writeonly
Sets the attribute password.
-
#post ⇒ Object
Returns the value of attribute post.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#username ⇒ Object
writeonly
Sets the attribute username.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #clear_header(key) ⇒ Object
- #clear_headers ⇒ Object
-
#initialize ⇒ Requesta
constructor
A new instance of Requesta.
- #request(uri) ⇒ Object
- #set_header(key, value) ⇒ Object
-
#set_headers(headers) ⇒ Object
Request Headers —————–.
Constructor Details
#initialize ⇒ Requesta
Returns a new instance of Requesta.
19 20 21 22 23 24 |
# File 'lib/requesta.rb', line 19 def initialize @ssl = false @post = false @verbose = true @headers = {} end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
15 16 17 |
# File 'lib/requesta.rb', line 15 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
17 18 19 |
# File 'lib/requesta.rb', line 17 def headers @headers end |
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
16 17 18 |
# File 'lib/requesta.rb', line 16 def password=(value) @password = value end |
#post ⇒ Object
Returns the value of attribute post.
15 16 17 |
# File 'lib/requesta.rb', line 15 def post @post end |
#ssl ⇒ Object
Returns the value of attribute ssl.
15 16 17 |
# File 'lib/requesta.rb', line 15 def ssl @ssl end |
#username=(value) ⇒ Object (writeonly)
Sets the attribute username
16 17 18 |
# File 'lib/requesta.rb', line 16 def username=(value) @username = value end |
#verbose ⇒ Object
Returns the value of attribute verbose.
15 16 17 |
# File 'lib/requesta.rb', line 15 def verbose @verbose end |
Instance Method Details
#clear_header(key) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/requesta.rb', line 84 def clear_header(key) unless key.is_a?(String) || key.is_a?(Symbol) raise RequestaError::RequestHeaderError, "clear_header(): 'key' must be of type Symbol or String" end @headers.delete(key) end |
#clear_headers ⇒ Object
80 81 82 |
# File 'lib/requesta.rb', line 80 def clear_headers @headers = {} end |
#request(uri) ⇒ Object
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 |
# File 'lib/requesta.rb', line 26 def request(uri) unless uri.is_a?(URI) raise RequestaError::RequestError, "request(): 'uri' parameter must be of type 'URI'" end puts "Requesting '#{uri.to_s}'" if @verbose http = Net::HTTP.new(uri.host, uri.port) if @ssl || uri_scheme_https?(uri) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = @post ? Net::HTTP::Post.new(uri) : Net::HTTP::Get.new(uri) if @username.present? && @password.present? request.basic_auth(@username, @password) end if @headers.present? @headers.each{ |key, value| request.add_field(key, value) } end if @body.present? request.body = @body end http.request(request) end |
#set_header(key, value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/requesta.rb', line 68 def set_header(key, value) unless key.is_a?(String) || key.is_a?(Symbol) raise RequestaError::RequestHeaderError, "set_header(): 'key' parameter must be of type Symbol or String" end unless value.is_a?(String) || value.is_a?(Symbol) raise RequestaError::RequestHeaderError, "set_header(): 'value' parameter must be of type Symbol or String" end @headers[key] = value end |
#set_headers(headers) ⇒ Object
Request Headers
60 61 62 63 64 65 66 |
# File 'lib/requesta.rb', line 60 def set_headers(headers) unless headers.is_a?(Hash) raise RequestaError::RequestHeaderError, "set_headers(): 'headers' parameter must be of type 'Hash'" end headers.each{ |key,value| set_header(key, value) } end |