Sha256: 04efede445b87f29d5c552ede29e1614bd23b7efee77f6abb61c80fc5c40a699

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module NintendoEshop
  class GamesList < APIRequest
    include Enumerable

    attr_accessor :title
    attr_accessor :games

    RESOURCE_PATH = "/1/indexes/noa_aem_game_en_us/query".freeze

    def initialize(title)
      self.title = title
    end

    def refresh
      response = request(:post, to_json: body)
      result = response.dig(:hits)
      self.games = refresh_list_objects(result)
      self
    end

    def each
      games.each do |game|
        yield(game)
      end
    end

    def self.by_title(title)
      instance = new(title)
      instance.refresh
    end

  private

    def body
      {
        "query": title.to_s,
        "restrictSearchableAttributes": [
          "title"
        ]
      }
    end

    def refresh_list_objects(objects)
      objects.map do |object|
        game = Game.new(object.dig(:nsuid))
        game.art = object.dig(:boxArt)
        game.msrp = object.dig(:msrp)
        game.platform = object.dig(:platform)
        game.sale_price = object.dig(:salePrice)
        game.title = object.dig(:title)
        game.url = object.dig(:url)
        game
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nintendo_eshop-0.1.0.alpha4 lib/nintendo_eshop/games_list.rb