Sha256: 9d6597b31c206cf51534ec67d232f7e0d636f67be4de41a74ae82f0acec65db4

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require "json"

require_relative "require_auth"
require_relative "simple_inspect"
require_relative "review"
require_relative "request"

module Itch
  # Fetch reviews
  class Reviews
    include SimpleInspect
    include RequireAuth
    include Request

    def initialize(agent, game_id)
      @agent = agent
      @game_id = game_id
    end

    def list
      all_reviews = []

      page_number = 1
      loop do
        page_reviews = parse_page_reviews(page_number)

        break if page_reviews.empty?

        all_reviews += page_reviews
        page_number += 1
        sleep 0.5
      end

      all_reviews
    end

    protected

    def parse_page_reviews(page_number)
      page = with_login do
        @agent.get(review_url(page_number))
      end

      raise Error, "Could not find game id #{@game_id} rewards" unless page.code == "200"

      page.css(".content_column .rating").map do |row|
        parse_row row
      end
    end

    # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
    def parse_row(row)
      id = row.at_css("button[data-lightbox_url]")["data-lightbox_url"].split("/").last
      stars = row.css(".star_picker .icon-star").length
      header = row.at_css(".row_header")
      date = DateTime.parse header.at_css("abbr")["title"].strip

      user_link = header.at_css(".user_link")
      user_name = user_link.search("./text()").text.strip
      user_id = user_link["href"].split("/").last

      text = row.css(".blurb p").map(&:text)

      Review.new(
        id: id,
        user_name: user_name,
        user_id: user_id,
        stars: stars,
        date: date,
        review: text
      )
    end
    # rubocop:enable Metrics/MethodLength, Metrics/AbcSize

    def review_url(page = 1)
      format(Itch::URL::REVIEWS, id: @game_id, page: page)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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