Sha256: 026b04c2c60758cc8fd2a9d76ce97bd9bf7c0638e0d36dbcea23413cc7eb5cc4

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Alma
  class FineSet < ResultSet
    class ResponseError < Alma::StandardError
    end

    attr_reader :results, :raw_response
    def_delegators :results, :empty?

    def initialize(raw_response)
      @raw_response = raw_response
      @response = raw_response.parsed_response
      validate(raw_response)
      @results = (@response.fetch(key, []) || [])
        .map { |item| single_record_class.new(item) }
    end

    def loggable
      { uri: @raw_response&.request&.uri.to_s
      }.select { |k, v| !(v.nil? || v.empty?) }
    end

    def validate(response)
      if response.code != 200
        message = "Could not find fines."
        log = loggable.merge(response.parsed_response)
        raise ResponseError.new(message, log)
      end
    end

    def each(&block)
      @results.each(&block)
    end

    def success?
      raw_response.response.code.to_s == "200"
    end

    def key
      "fee"
    end

    def sum
      fetch("total_sum", 0)
    end

    alias :total_sum :sum

    def currency
      fetch("currency", nil)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alma-0.6.2 lib/alma/fine_set.rb
alma-0.6.1 lib/alma/fine_set.rb
alma-0.6.0 lib/alma/fine_set.rb
alma-0.5.1 lib/alma/fine_set.rb
alma-0.5.0 lib/alma/fine_set.rb
alma-0.4.2 lib/alma/fine_set.rb
alma-0.4.1 lib/alma/fine_set.rb
alma-0.4.0 lib/alma/fine_set.rb