Sha256: ff08c86af6bc37358ae812cbfadc41a39dddcd3425a48f4318d5f1435587f04b

Contents?: true

Size: 903 Bytes

Versions: 4

Compression:

Stored size: 903 Bytes

Contents

# frozen_string_literal: true

require "bigdecimal"

require_relative "simple_inspect"
require_relative "bundle"
require_relative "request"
require_relative "require_auth"

module Itch
  # Fetch bundles
  class Bundles
    include RequireAuth
    include SimpleInspect
    include Request

    def initialize(agent)
      @agent = agent
    end

    def list
      page = with_login do
        @agent.get(bundles_url)
      end

      page.css(".bundle_list table > tr").map do |row|
        parse_row(row)
      end
    end

    def parse_row(row)
      id = row.at_xpath("td[2]/a/@href").value.match(%r{^/b/(\d+)/})[1]
      vals = row.css("td").map(&:text)
      price = BigDecimal(vals[5].gsub(/[^\d.-]/, ""))
      earnings = BigDecimal(vals[6].gsub(/[\D-]/, ""))

      Bundle.new(id, vals[1], vals[4].to_i, price, earnings)
    end

    def bundles_url
      Itch::URL::BUNDLES
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
itch_client-0.4.3 lib/itch/bundles.rb
itch_client-0.4.2 lib/itch/bundles.rb
itch_client-0.4.1 lib/itch/bundles.rb
itch_client-0.4.0 lib/itch/bundles.rb