Sha256: a69fcd71fb69db38c28d158b982f6983def1df8ee5a575b0d372f7171335430b

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module ADPDownloader
  class Statement
    def initialize(json)
      @data = json
    end

    abstract_methods = %w(id date)

    abstract_methods.each do |method_name|
      define_method method_name do
        raise "You need to define `#{method_name}` for #{self.class}"
      end
    end

    def merge(details)
      top_level_attr, contents = details.first
      { top_level_attr => @data.merge(contents) }
    end

    def filename
      [date, id, file_suffix].compact.join("-")
    end

    def file_suffix
      nil
    end

    def pdf_uri
      path = _("statementImageUri.href")
      path.gsub(%r{^/l2}, "") # remove first characters, since it's incorrect o.O
    end

    def year
      date.split("-").first
    end

    def pdf
      File.join(year, "#{filename}.pdf")
    end

    def json
      File.join(year, "#{filename}.json")
    end

    def _(string)
      get_info(@data, string.split("."))
    end

    private
    def get_info(data, attributes)
      attr = attributes.first
      info = data[attr]
      if attributes.size == 1
        info
      else
        next_data = info.nil? ? {} : info
        get_info(next_data, attributes.drop(1))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adp-downloader-0.2.2 lib/adp-downloader/statement/statement.rb
adp-downloader-0.2.1 lib/adp-downloader/statement/statement.rb