Sha256: 1c9c63363d523292639fae13430d6386e92281bf03c22695f3568ef232a60c88

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

require 'uri'
require 'json'
require 'rakuten_web_service/response'
require 'rakuten_web_service/error'

module RakutenWebService
  class Client
    attr_reader :url

    def initialize(resource_class)
      @resource_class = resource_class
      @url = URI.parse(@resource_class.endpoint)
    end

    def get(params)
      params = RakutenWebService.configuration.generate_parameters(params)
      response = request(url.path, params)
      body = JSON.parse(response.body)

      if Net::HTTPSuccess === response
        return RakutenWebService::Response.new(@resource_class, body)
      else
        raise RakutenWebService::Error.repository[response.code.to_i], body['error_description']
      end
    end

    private
    def request(path, params)
      http = Net::HTTP.new(@url.host, @url.port)
      http.use_ssl = true
      path = "#{path}?#{params.map { |k, v| "#{k}=#{URI.encode(v.to_s)}" }.join('&')}"
      header = {
        'User-Agent' => "RakutenWebService SDK for Ruby v#{RWS::VERSION}(ruby-#{RUBY_VERSION} [#{RUBY_PLATFORM}])"
      }
      http.get(path, header)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rakuten_web_service-1.4.0 lib/rakuten_web_service/client.rb
rakuten_web_service-1.3.0 lib/rakuten_web_service/client.rb
rakuten_web_service-1.2.0 lib/rakuten_web_service/client.rb
rakuten_web_service-1.1.1 lib/rakuten_web_service/client.rb
rakuten_web_service-1.1.0 lib/rakuten_web_service/client.rb