Module: Mistilteinn::HttpUtil

Defined in:
lib/mistilteinn/http_util.rb

Defined Under Namespace

Classes: HttpError

Class Method Summary collapse

Class Method Details

.get_json(url, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/mistilteinn/http_util.rb', line 14

def get_json(url, params={})
  url = url.to_s + '?' + params.map{|key,value| "#{key}=#{value}" }.join("&")
  begin
    open(url) do |io|
      JSON.parse(io.read)
    end
  rescue => e
    raise HttpError.new("#{e.message} (#{url})")
  end
end

.post_json(url, headers, data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mistilteinn/http_util.rb', line 25

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

  http.start do
    ret = http.post(url.path,
                    data.to_json,
                    headers.merge("Content-Type" => "application/json"))
    case ret
    when Net::HTTPSuccess
    else
      raise HttpError.new("#{ret} (#{url})")
    end
  end
end