Sha256: f6ecfcbc069dbbba24db327e259e8e0ac6b9c168b336c5f29eb99f4380cba88b

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'net/http'
require 'json'

class Shoptet
  class Request
    def self.get url, headers
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      http.open_timeout = 5
      http.read_timeout = 5
      http.write_timeout = 5
      http.ssl_timeout = 5

      request = Net::HTTP::Get.new(url)
      headers.each do |key, value|
        request[key] = value
      end

      response = handle_net_timeouts { http.request(request) }
      parsed_body = JSON.parse(response.body)

      unless parsed_body
        message = "Status code: #{response.code}, url: #{url}"
        fail Shoptet::EmptyResponse.new(message)
      end

      parsed_body
    end

    def self.post url, body
      request = Net::HTTP::Post.new(url)
      request.set_form_data(body)

      response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
        handle_net_timeouts { http.request(request) }
      end

      JSON.parse(response.body)
    end

    private

    def self.handle_net_timeouts
      begin
        yield
      rescue Net::OpenTimeout
        yield
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoptet-0.0.32 lib/shoptet/request.rb