Class: PlayStation::Store

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/consumers/play_station/store.rb

Overview

This class uses PlayStation store json api to retrive list of games

Constant Summary collapse

URL =
'https://store.playstation.com'.freeze
PER_PAGE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore


14
15
16
17
18
19
20
21
22
# File 'app/consumers/play_station/store.rb', line 14

def initialize
  @api = Faraday.new(url: URL) do |faraday|
    faraday.use :http_cache, store: Rails.cache
    faraday.adapter Faraday.default_adapter
    faraday.response :json
  end
  @page  = 0
  @games = []
end

Instance Attribute Details

#gamesObject (readonly)

Return list of fetched games


12
13
14
# File 'app/consumers/play_station/store.rb', line 12

def games
  @games
end

#pageObject (readonly)

Current page


10
11
12
# File 'app/consumers/play_station/store.rb', line 10

def page
  @page
end

Instance Method Details

#eachObject

Fetch all games from playstation store and iterate over it


34
35
36
37
# File 'app/consumers/play_station/store.rb', line 34

def each(*)
  @page = 0
  @games.each { |game| yield(game) } while self.next
end

#nextObject

Fetch next page with games


26
27
28
29
30
31
# File 'app/consumers/play_station/store.rb', line 26

def next
  response = @api.get("/chihiro-api/viewfinder/PL/en/19/STORE-MSF75508-PS4CAT?game_content_type=games&platform=ps4&size=#{PER_PAGE}&gkb=1&geoCountry=PL&start=#{offset}&sort=name&direction=as")
  @games   = response.body['links'].map { |raw_json_game| PlayStation::Game.new(raw_json_game) }
  @page += 1
  !@games.blank?
end