Class: Hearthstone::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/hearthstone/api.rb

Constant Summary collapse

OAUTH_URL =
"https://us.battle.net/oauth/token"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



13
14
15
16
17
18
19
20
21
22
# File 'lib/hearthstone/api.rb', line 13

def initialize(options={})
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @region = options[:region] || "us"
  @locale = options[:locale] || "en_US"
  @access_token = get_token
  @is_valid = true
rescue Hearthstone::InvalidClientError => e
  @is_valid = false
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def client_secret
  @client_secret
end

#is_validObject (readonly)

Returns the value of attribute is_valid.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def is_valid
  @is_valid
end

#localeObject (readonly)

Returns the value of attribute locale.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def locale
  @locale
end

#regionObject (readonly)

Returns the value of attribute region.



9
10
11
# File 'lib/hearthstone/api.rb', line 9

def region
  @region
end

Instance Method Details

#deck(deckcode, options = {}) ⇒ Object

Finds a deck by its deck code. Allow Options

- none


71
72
73
# File 'lib/hearthstone/api.rb', line 71

def deck(deckcode, options={})
  request(:get, "/hearthstone/deck/#{deckcode}", localized_options(options))
end

#endpointObject



24
25
26
27
28
29
# File 'lib/hearthstone/api.rb', line 24

def endpoint
  @endpoint ||= Faraday::Connection.new(url: "https://#{region}.api.blizzard.com") do |builder|
    builder.use Faraday::Request::UrlEncoded
    builder.use Faraday::Adapter::NetHttp
  end
end

#fetch(slug, options = {}) ⇒ Object

Returns the card with an ID or slug that matches the one you specify. Allow Options

- none


64
65
66
# File 'lib/hearthstone/api.rb', line 64

def fetch(slug, options={})
  request(:get, "/hearthstone/cards/#{slug}", localized_options(options))
end

#fetch_metadata(type, options = {}) ⇒ Object

Returns information about just one type of metadata.



82
83
84
# File 'lib/hearthstone/api.rb', line 82

def (type, options={})
  request(:get, "/hearthstone/metadata/#{type}", localized_options(options))
end

#metadata(options = {}) ⇒ Object

Returns information about the categorization of cards. Metadata includes the card set, set group (for example, Standard or Year of the Dragon), rarity, class, card type, minion type, and keywords.



77
78
79
# File 'lib/hearthstone/api.rb', line 77

def (options={})
  request(:get, "/hearthstone/metadata", localized_options(options))
end

#search(options = {}) ⇒ Object

Returns an up-to-date list of all cards matching the search criteria. Allow Options

- set[string]          : The slug of the set the card belongs to. If you do not supply a value, cards from all sets will be returned.
- class[string]        : The slug of the card's class.
- manaCost[numbers]    : The mana cost required to play the card. You can include multiple values in a comma-separated list of numeric values.
- attack[numbers]      : The attack power of the minion or weapon. You can include multiple values in a comma-separated list of numeric values.
- health[numbers]      : The health of a minion. You can include multiple values in a comma-separated list of numeric values.
- collectible[numbers] : Whether a card is collectible. A value of 1 indicates that collectible cards should be returned; 0 indicates uncollectible cards.
                         To return all cards, use a value of '0,1'.
- rarity[string]       : The rarity of a card. This value must match the rarity slugs found in metadata.
- type[string]         : The type of card (for example, minion, spell, and so on). This value must match the type slugs found in metadata.
- minionType[string]   : The type of minion card (for example, beast, murloc, dragon, and so on).
                         This value must match the minion type slugs found in metadata.
- keyword[string]      : A required keyword on the card (for example, battlecry, deathrattle, and so on).
                         This value must match the keyword slugs found in metadata.
- textFilter[string]   : A text string used to filter cards. You must include a locale along with the textFilter parameter.
- page[number]         : A page number.
- pageSize[number]     : The number of results to choose per page.
                         A value will be selected automatically if you do not supply a pageSize or if the pageSize is higher than the maximum allowed.
- sort[string]         : The field used to sort the results. Valid values include manaCost, attack, health, and name.
                         Results are sorted by manaCost by default. Cards will also be sorted by class automatically in most cases.
- order[string]        : The order in which to sort the results. Valid values are asc or desc. The default value is asc.


57
58
59
# File 'lib/hearthstone/api.rb', line 57

def search(options={})
  request(:get, "/hearthstone/cards", localized_options(options))
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hearthstone/api.rb', line 31

def valid?
  is_valid
end