Sha256: 4f9ad084f65a05576b56dad2608f19a4c09506beda15edda13141df565dad365

Contents?: true

Size: 1.7 KB

Versions: 11

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'cgi'

module Fortnox
  module API
    module Repository
      module Loaders
        def all
          response_hash = get(self.class::URI)
          instantiate_collection_response(response_hash)
        end

        def only(filter)
          response_hash = get("#{self.class::URI}?filter=#{filter}")
          instantiate_collection_response(response_hash)
        end

        def search(hash)
          attribute, value = hash.first
          uri_encoded_value = CGI.escape(value)
          uri = "#{self.class::URI}?#{attribute}=#{uri_encoded_value}"
          response_hash = get(uri)
          instantiate_collection_response(response_hash)
        end

        def find(id_or_hash)
          return find_all_by(id_or_hash) if id_or_hash.is_a? Hash

          find_one_by(id_or_hash)
        end

        def find_one_by(id)
          response_hash = get("#{self.class::URI}#{id}")
          instantiate(@mapper.wrapped_json_hash_to_entity_hash(response_hash))
        end

        def find_all_by(hash)
          response_hash = get("#{self.class::URI}?#{to_query(hash)}")
          instantiate_collection_response(response_hash)
        end

        def to_query(hash)
          hash.collect do |key, value|
            escape(key, value)
          end.sort * '&'
        end

        def escape(key, value)
          "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
        end

        private

        def instantiate_collection_response(response_hash)
          entities_hash = @mapper.wrapped_json_collection_to_entities_hash(response_hash)
          entities_hash.map do |entity_hash|
            instantiate(entity_hash)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fortnox-api-0.8.2 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.8.1 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.9.0 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.8.0 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.7.2 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.7.1 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.7.0 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.6.3 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.6.2 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.6.1 lib/fortnox/api/repositories/base/loaders.rb
fortnox-api-0.6.0 lib/fortnox/api/repositories/base/loaders.rb