Sha256: 9b5d0725038ccaf1f7def4d8faac69bbb3e7c1aeefd741affb0ffc686a2fd230

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

require 'fortnox/api/class_methods'
require 'fortnox/api/environment_validation'
require 'fortnox/api/request_handling'
require 'httparty'

module Fortnox
  module API
    class Base

      include HTTParty
      extend Fortnox::API::ClassMethods
      include Fortnox::API::EnvironmentValidation
      include Fortnox::API::RequestHandling

      HTTParty::Parser::SupportedFormats[ "text/html" ] = :json

      DEFAULT_HEADERS = {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
      }.freeze

      HTTP_METHODS = [ :get, :put, :post, :delete ].freeze

      attr_accessor :headers

      def initialize
        self.class.base_uri( get_base_url )

        self.headers = DEFAULT_HEADERS.merge({
          'Client-Secret' => get_client_secret,
        })

        check_access_tokens!
      end

      HTTP_METHODS.each do |method|
        define_method method do |*args|
          self.headers['Access-Token'] = get_access_token
          execute do |remote|
            remote.send( method, *args )
          end
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fortnox-api-0.4.0 lib/fortnox/api/base.rb
fortnox-api-0.3.0 lib/fortnox/api/base.rb
fortnox-api-0.2.0 lib/fortnox/api/base.rb