Sha256: ed9a5827e341599d4910d868af00d94bc15b2553e47cf358a4e8980ce82e9e98

Contents?: true

Size: 903 Bytes

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
itch_client-0.3.0 lib/itch/bundles.rb