Class: SellsyV2::Request

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

Overview

Your code goes hereā€¦

Instance Method Summary collapse

Constructor Details

#initialize(verb:, path:, options:) ⇒ Request

Returns a new instance of Request.



15
16
17
18
# File 'lib/sellsy_v2.rb', line 15

def initialize(verb:, path:, options:)
  params = '?' + options.map{|k,v| v.class == Array ? "#{k}[]=#{v.join(',')}" : "#{k}=#{v}"}.join('&')
  @url = URI("https://api.sellsy.com/v2#{path}#{params}")
end

Instance Method Details

#callObject



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

def call
  https = Net::HTTP.new(@url.host, @url.port)
  https.use_ssl = true

  request = Net::HTTP::Get.new(@url) # change to verb
  request["Authorization"] = ENV["SELLSY_TOKEN"]

  response = https.request(request)

  if response.is_a?(Net::HTTPOK)
    OpenStruct.new(success?: true, data: JSON.parse(response.read_body))
  else
    OpenStruct.new(success?: false, data: response)
  end
end