Class: Todo::Support::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/todo/support/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/todo/support/http.rb', line 7

def headers
  @headers
end

#uriObject (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

#getObject



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