Sha256: d475db5ebc926a4d2f72bb98f25b3b55d28a1a02dd45d2c6b5265d82a9b67d87

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module PensioAPI
  module Mixins
    module RequestDefaults
      def self.included(base)
        base.send(:include, HTTParty)
        base.send(:attr_reader, :response)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def set_base_uri
          self.base_uri PensioAPI::Credentials.base_uri unless self.base_uri
        end
      end

      HEADERS = {
        'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
      }

      def initialize(path, options={})
        self.class.set_base_uri

        raise Errors::NoCredentials unless credentials_supplied?

        @response = self.class.post(path, request_options(options))
      end

      private

      def request_options(options)
        {
          basic_auth: auth,
          headers: (options.delete(:headers) || {}).merge(HEADERS),
          body: options
        }
      end

      def auth
        {
          username: PensioAPI::Credentials.username,
          password: PensioAPI::Credentials.password
        }
      end

      def credentials_supplied?
        Credentials.base_uri && Credentials.username && Credentials.password
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pensio_api-0.3.0 lib/pensio_api/mixins/request_defaults.rb
pensio_api-0.2.3 lib/pensio_api/mixins/request_defaults.rb