Class: GoFigure::HttpFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/go_figure/http_fetcher.rb

Instance Method Summary collapse

Instance Method Details

#get(url, params = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/go_figure/http_fetcher.rb', line 7

def get(url, params = {})
  url = URI.parse(url)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.scheme == 'https'

  res = http.start do |http|
    req = Net::HTTP::Get.new(url.path)
    http.request(req)
  end

  case res
  when Net::HTTPSuccess
    return res
  end
  res.error!
end

#post(url, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/go_figure/http_fetcher.rb', line 25

def post(url, params = {})
  url = URI.parse(url)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.scheme == 'https'

  res = http.start do |http|
    req = Net::HTTP::Post.new(url.path)
    req.set_form_data(params) if params.any?
    http.request(req)
  end

  case res
  when Net::HTTPSuccess
    return res
  end
  res.error!
end