Sha256: 815465c824caabef6a99db4c86bfd7ebddae990237e4aaee9d6247f683c5287a

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require "net/http"
require "uri"
require "json"

module Klarna
  class Client
    def initialize(configuration)
      @configuration = configuration
    end


    private

    def do_request(type, service, &block)
      uri = URI.parse(@configuration.endpoint)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      request = case type
        when :post
          Net::HTTP::Post.new(service)
        when :delete
          Net::HTTP::Delete.new(service)
        when :get
          Net::HTTP::Get.new(service)
        else
          raise "Wrong request type <#{type}>. Allowed [:post, :delete, :get]"
      end


      request.content_type = "application/json"
      request.basic_auth(@configuration.api_key, @configuration.api_secret)
      if @configuration.user_agent
        request["User-Agent"] = "Ruby Klarna #{Klarna::VERSION} (#{@configuration.user_agent.to_s} Ruby/#{RUBY_VERSION})"
      end

      yield(request) if block_given?

      http.set_debug_output(@configuration.debugger) if @configuration.debugger
      response = http.request(request)
      http.set_debug_output(nil) if @configuration.debugger

      Klarna::Response.new(response)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
klarna_client-0.9.1 lib/klarna/client.rb
klarna_client-0.9.0 lib/klarna/client.rb