Class: Todo::Support::Http
- Inherits:
-
Object
- Object
- Todo::Support::Http
- Defined in:
- lib/todo/support/http.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(url, opts) ⇒ Http
constructor
A new instance of Http.
- #post(data) ⇒ Object
Constructor Details
#initialize(url, opts) ⇒ Http
Returns a new instance of Http.
9 10 11 12 |
# File 'lib/todo/support/http.rb', line 9 def initialize(url, opts) @uri = URI(url).tap { |url| url.query = URI.encode_www_form(opts[:params] || {}) } @headers = opts[:headers] || {} end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/todo/support/http.rb', line 7 def headers @headers end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/todo/support/http.rb', line 7 def uri @uri end |
Instance Method Details
#get ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/todo/support/http.rb', line 14 def get client = Net::HTTP.new(url.host, url.port) client.use_ssl = true response = client.get(uri, headers) raise "Failed to get #{url.to_s}" unless response.code.to_s[0] == '2' response end |
#post(data) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/todo/support/http.rb', line 22 def post(data) request = Net::HTTP::Post.new(url, headers) request.form_data = data response = http.request(request) raise "Failed to post to #{url.to_s}: #{response.body}" unless response.code.to_s[0] == '2' response end |