class Gwitch::Region::Americas

Constants

CLIENT
FACETS

Trick for API limitation. Nintendo limit each facet can only get 1000 results. So use facets as much as possible to get results. Maybe incompletely got.

INDEX
PLATFORM

Public Class Methods

games() click to toggle source
# File lib/gwitch/region/americas.rb, line 34
def games
  games = []

  # No facets
  games += fetch

  # Each facet
  filters.each do |filter|
    games += fetch(filter)
  end

  parse(games.uniq)
end

Private Class Methods

fetch(filter = nil) click to toggle source
# File lib/gwitch/region/americas.rb, line 50
def fetch(filter = nil)
  hits = []

  # Empty string means all
  query = ''

  # histPerPage max can be 500
  search_params = {
    hitsPerPage: 500
  }

  search_params.merge!(facetFilters: filter) if filter

  page = 0

  loop do
    response = INDEX.search(query, search_params.merge(page: page))
    total_pages = response['nbPages']
    hits += response['hits']
    page += 1

    break unless page < total_pages
  end

  hits
end
filters() click to toggle source
# File lib/gwitch/region/americas.rb, line 137
def filters
  filters = []
  
  FACETS.each do |facet, options|
    options.each do |option|
      filters << [PLATFORM, "#{facet}:#{option}"]
    end
  end

  filters
end
parse(raw) click to toggle source
# File lib/gwitch/region/americas.rb, line 77
def parse(raw)
  host = 'https://www.nintendo.com'
  microsite_host = 'https://assets.nintendo.com/image/upload/f_auto,q_auto,w_960,h_540'
  legacy_host = 'https://assets.nintendo.com/video/upload/f_auto,q_auto,w_960,h_540'
  asset_host = 'https://assets.nintendo.com/image/upload/f_auto,q_auto,w_960,h_540/Legacy%20Videos/posters/'

  raw.map do |game|
    image_urls = []
    image_urls << (host + game['boxArt']) if game['boxArt']

    gallery_path = game['gallery']
    if gallery_path.nil?
      nil
    # Begin with '/'
    elsif gallery_path[0] == '/'
      # If prefix is 'content'
      if gallery_path[1] == 'c'
        image_urls << host + gallery_path
      # Prefix is 'Microsites'
      # Just one game
      elsif  gallery_path[1] == 'M'
        url = microsite_host + gallery_path
        last_slash = url.rindex('/')
        image_urls << url.insert(last_slash + 1, 'posters/')
      # Prefix is 'Nintendo'
      elsif gallery_path[1] == 'N'
        # TODO Nintendo* url have many situations
        # https://assets.nintendo.com/image/upload/f_auto,q_auto,w_960,h_540/Nintendo Switch/Games/Third Party/Overcooked 2/Video/posters/Overcooked_2_Gourmet_Edition_Trailer
        # /Nintendo Switch/Games/Third Party/Overcooked 2/Video/Overcooked_2_Gourmet_Edition_Trailer

        # https://assets.nintendo.com/video/upload/f_auto,q_auto,w_960,h_540/Nintendo Switch/Games/NES and Super NES/Video/NES_Super_NES_May_2020_Game_Updates_Nintendo_Switch_Online
        # /Nintendo Switch/Games/NES and Super NES/Video/NES_Super_NES_May_2020_Game_Updates_Nintendo_Switch_Online
      # Prefix is 'Legacy Videos'
      elsif gallery_path[1] == 'L'
        image_urls << legacy_host + gallery_path
      else
        raise GalleryParsedError
      end
    # Just id
    else
      image_urls << asset_host + gallery_path
    end

    url = game['url'] ? host + game['url'] : nil

    {
      nsuid: game['nsuid'],
      title: game['title'],
      description: game['description'],
      categories: game['categories'],
      maker: game['publishers']&.join(', '),
      player: game['players'],
      region: 'Americas',
      images: image_urls,
      url: url,
      release_at: game['releaseDateMask']
    }
  end
end