Class: OzonApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ozon_api/client.rb

Constant Summary collapse

InvalidConfigurationError =
Class.new(StandardError)
ApiError =
Class.new(StandardError)
CONFIGURATION_KEYS =
[:scheme, :host, :base_path, :login, :password, :out].freeze
SUCCESS_STATUS =
2

Instance Method Summary collapse

Constructor Details

#initialize(hsh = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
# File 'lib/ozon_api/client.rb', line 14

def initialize(hsh = {})
  @config = hsh.select do |key, _|
    CONFIGURATION_KEYS.include? key.to_sym
  end
end

Instance Method Details

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ozon_api/client.rb', line 20

def get(path, params = {})
  uri = URI("#{scheme}://#{host}/#{base_path}/#{path}/")
  uri.query = URI.encode_www_form(default_params.merge(params))
  response = Net::HTTP.get(uri)

  if out
    out << "Ozon get:\n"
    out << uri.to_s
    out << "\n"
    out << "Ozon response:\n"
    out << response
    out << "\n"
  end

  parse_response(response)
end

#post(path, params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ozon_api/client.rb', line 37

def post(path, params)
  query = URI.encode_www_form(default_params.merge(params))
  uri   = URI("#{scheme}://#{host}/#{base_path}/#{path}/?#{query}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri)
  response = http.request(request)

  if out
    out << "Ozon post:\n"
    out << uri.to_s
    out << "\n"
    out << "Ozon response:\n"
    out << response
    out << "\n"
  end

  parse_response(response.read_body)
end