Class: NintendoEshop::Game

Inherits:
APIRequest show all
Defined in:
lib/nintendo_eshop/game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, object_id: nil) ⇒ Game

Returns a new instance of Game.



16
17
18
19
# File 'lib/nintendo_eshop/game.rb', line 16

def initialize(id: nil, object_id: nil)
  self.id = id
  self.object_id = object_id
end

Instance Attribute Details

#artObject

Returns the value of attribute art.



3
4
5
# File 'lib/nintendo_eshop/game.rb', line 3

def art
  @art
end

#categoriesObject

Returns the value of attribute categories.



4
5
6
# File 'lib/nintendo_eshop/game.rb', line 4

def categories
  @categories
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/nintendo_eshop/game.rb', line 5

def description
  @description
end

#esrbObject

Returns the value of attribute esrb.



6
7
8
# File 'lib/nintendo_eshop/game.rb', line 6

def esrb
  @esrb
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/nintendo_eshop/game.rb', line 7

def id
  @id
end

#msrpObject

Returns the value of attribute msrp.



8
9
10
# File 'lib/nintendo_eshop/game.rb', line 8

def msrp
  @msrp
end

#object_idObject

Returns the value of attribute object_id.



9
10
11
# File 'lib/nintendo_eshop/game.rb', line 9

def object_id
  @object_id
end

#platformObject

Returns the value of attribute platform.



10
11
12
# File 'lib/nintendo_eshop/game.rb', line 10

def platform
  @platform
end

#release_dateObject

Returns the value of attribute release_date.



11
12
13
# File 'lib/nintendo_eshop/game.rb', line 11

def release_date
  @release_date
end

#sale_priceObject

Returns the value of attribute sale_price.



12
13
14
# File 'lib/nintendo_eshop/game.rb', line 12

def sale_price
  @sale_price
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/nintendo_eshop/game.rb', line 13

def title
  @title
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/nintendo_eshop/game.rb', line 14

def url
  @url
end

Class Method Details

.retrieve_by(id_type = {}) ⇒ Object



48
49
50
51
52
# File 'lib/nintendo_eshop/game.rb', line 48

def retrieve_by(id_type = {})
  validate_argument(id_type)
  instance = new(id_type)
  instance.refresh
end

Instance Method Details

#current_priceObject



35
36
37
# File 'lib/nintendo_eshop/game.rb', line 35

def current_price
  sale_price || msrp
end

#refreshObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nintendo_eshop/game.rb', line 21

def refresh
  response = request(:post, to_json: body)
  result = if response.dig(:hits)&.any?
             response.dig(:hits, 0)
           elsif response.dig(:results)&.any?
             response.dig(:results, 0)
           else
             raise InvalidRequestError, "ID not found"
           end

  refresh_object(result)
  self
end

#release_date_prettyObject



39
40
41
# File 'lib/nintendo_eshop/game.rb', line 39

def release_date_pretty
  release_date.strftime("%b %d, %Y")
end

#sale_percentObject



43
44
45
# File 'lib/nintendo_eshop/game.rb', line 43

def sale_percent
  ((1 - (sale_price / msrp)).round(2) * 100).to_i.to_s + "%" if sale_price
end