pokemon_tcg_sdk 2.5.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +3 -0
  3. data/.gitignore +23 -24
  4. data/.travis.yml +15 -4
  5. data/CODE_OF_CONDUCT.md +48 -48
  6. data/Gemfile +4 -4
  7. data/LICENSE.txt +21 -21
  8. data/README.md +187 -155
  9. data/Rakefile +20 -10
  10. data/lib/pokemon_tcg_sdk.rb +38 -31
  11. data/lib/pokemon_tcg_sdk/ability.rb +14 -10
  12. data/lib/pokemon_tcg_sdk/ancient_trait.rb +13 -10
  13. data/lib/pokemon_tcg_sdk/attack.rb +16 -10
  14. data/lib/pokemon_tcg_sdk/card.rb +69 -42
  15. data/lib/pokemon_tcg_sdk/card_images.rb +13 -0
  16. data/lib/pokemon_tcg_sdk/configuration.rb +8 -8
  17. data/lib/pokemon_tcg_sdk/legalities.rb +14 -0
  18. data/lib/pokemon_tcg_sdk/query_builder.rb +63 -67
  19. data/lib/pokemon_tcg_sdk/rarity.rb +12 -0
  20. data/lib/pokemon_tcg_sdk/resistance.rb +14 -0
  21. data/lib/pokemon_tcg_sdk/rest_client.rb +43 -31
  22. data/lib/pokemon_tcg_sdk/set.rb +50 -40
  23. data/lib/pokemon_tcg_sdk/set_images.rb +13 -0
  24. data/lib/pokemon_tcg_sdk/subtype.rb +11 -11
  25. data/lib/pokemon_tcg_sdk/supertype.rb +11 -11
  26. data/lib/pokemon_tcg_sdk/tcgplayer.rb +44 -0
  27. data/lib/pokemon_tcg_sdk/type.rb +11 -11
  28. data/lib/pokemon_tcg_sdk/version.rb +3 -3
  29. data/lib/pokemon_tcg_sdk/weakness.rb +13 -0
  30. data/pokemon_tcg_sdk.gemspec +32 -34
  31. metadata +33 -61
  32. data/lib/pokemon_tcg_sdk/representers/ability_representer.rb +0 -11
  33. data/lib/pokemon_tcg_sdk/representers/ancient_trait_representer.rb +0 -10
  34. data/lib/pokemon_tcg_sdk/representers/attack_representer.rb +0 -13
  35. data/lib/pokemon_tcg_sdk/representers/card_representer.rb +0 -40
  36. data/lib/pokemon_tcg_sdk/representers/set_representer.rb +0 -17
  37. data/lib/pokemon_tcg_sdk/representers/type_value_representer.rb +0 -10
  38. data/lib/pokemon_tcg_sdk/type_value.rb +0 -10
data/Rakefile CHANGED
@@ -1,11 +1,21 @@
1
- require "bundler/gem_tasks"
2
- require 'rake/testtask'
3
- require_relative 'lib/pokemon_tcg_sdk'
4
-
5
- task :default => :test
6
-
7
- Rake::TestTask.new do |t|
8
- t.libs << "test"
9
- t.test_files = FileList['test/*_test.rb']
10
- t.verbose = true
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ require_relative 'lib/pokemon_tcg_sdk'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "lib"
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/*_test.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ Pokemon.configure do |config|
15
+ config.api_key = ENV['POKEMON_API_KEY']
16
+ end
17
+
18
+ task :card do
19
+ card = Pokemon::Card.find('xy1-1')
20
+ puts card.tcgplayer.updated_at
11
21
  end
@@ -1,32 +1,39 @@
1
- require "pokemon_tcg_sdk/version"
2
- require "pokemon_tcg_sdk/card"
3
- require "pokemon_tcg_sdk/configuration"
4
- require "pokemon_tcg_sdk/query_builder"
5
- require "pokemon_tcg_sdk/rest_client"
6
- require "pokemon_tcg_sdk/ability"
7
- require "pokemon_tcg_sdk/attack"
8
- require "pokemon_tcg_sdk/type_value"
9
- require "pokemon_tcg_sdk/type"
10
- require "pokemon_tcg_sdk/subtype"
11
- require "pokemon_tcg_sdk/supertype"
12
- require "pokemon_tcg_sdk/set"
13
-
14
- module Pokemon
15
- API_URL = 'https://api.pokemontcg.io'
16
-
17
- class << self
18
- attr_writer :configuration
19
- end
20
-
21
- def self.configuration
22
- @configuration ||= Configuration.new
23
- end
24
-
25
- def self.configure
26
- yield(configuration)
27
- end
28
-
29
- def self.reset
30
- @configuration = Configuration.new
31
- end
1
+ require "pokemon_tcg_sdk/version"
2
+ require "pokemon_tcg_sdk/card"
3
+ require "pokemon_tcg_sdk/configuration"
4
+ require "pokemon_tcg_sdk/query_builder"
5
+ require "pokemon_tcg_sdk/rest_client"
6
+ require "pokemon_tcg_sdk/ability"
7
+ require "pokemon_tcg_sdk/attack"
8
+ require "pokemon_tcg_sdk/type"
9
+ require "pokemon_tcg_sdk/subtype"
10
+ require "pokemon_tcg_sdk/supertype"
11
+ require "pokemon_tcg_sdk/set"
12
+ require "pokemon_tcg_sdk/weakness"
13
+ require "pokemon_tcg_sdk/resistance"
14
+ require "pokemon_tcg_sdk/legalities"
15
+ require "pokemon_tcg_sdk/card_images"
16
+ require "pokemon_tcg_sdk/set_images"
17
+ require "pokemon_tcg_sdk/tcgplayer"
18
+ require "pokemon_tcg_sdk/ancient_trait"
19
+ require "pokemon_tcg_sdk/rarity"
20
+
21
+ module Pokemon
22
+ API_URL = 'https://api.pokemontcg.io'
23
+
24
+ class << self
25
+ attr_writer :configuration
26
+ end
27
+
28
+ def self.configuration
29
+ @configuration ||= Configuration.new
30
+ end
31
+
32
+ def self.configure
33
+ yield(configuration)
34
+ end
35
+
36
+ def self.reset
37
+ @configuration = Configuration.new
38
+ end
32
39
  end
@@ -1,10 +1,14 @@
1
- require_relative 'representers/ability_representer'
2
-
3
- module Pokemon
4
- class Ability
5
- include Roar::JSON
6
- include AbilityRepresenter
7
-
8
- attr_accessor :name, :text, :type
9
- end
10
- end
1
+ module Pokemon
2
+ class Ability
3
+ attr_accessor :name, :text, :type
4
+
5
+ def self.from_json(json)
6
+ ability = Ability.new
7
+ ability.name = json['name']
8
+ ability.text = json['text']
9
+ ability.type = json['type']
10
+
11
+ ability
12
+ end
13
+ end
14
+ end
@@ -1,10 +1,13 @@
1
- require_relative 'representers/ancient_trait_representer'
2
-
3
- module Pokemon
4
- class AncientTrait
5
- include Roar::JSON
6
- include AncientTraitRepresenter
7
-
8
- attr_accessor :name, :text
9
- end
10
- end
1
+ module Pokemon
2
+ class AncientTrait
3
+ attr_accessor :name, :text
4
+
5
+ def self.from_json(json)
6
+ trait = AncientTrait.new
7
+ trait.name = json['name']
8
+ trait.text = json['text']
9
+
10
+ trait
11
+ end
12
+ end
13
+ end
@@ -1,10 +1,16 @@
1
- require_relative 'representers/attack_representer'
2
-
3
- module Pokemon
4
- class Attack
5
- include Roar::JSON
6
- include AttackRepresenter
7
-
8
- attr_accessor :cost, :name, :text, :damage, :converted_energy_cost
9
- end
10
- end
1
+ module Pokemon
2
+ class Attack
3
+ attr_accessor :cost, :name, :text, :damage, :converted_energy_cost
4
+
5
+ def self.from_json(json)
6
+ attack = Attack.new
7
+ attack.cost = json['cost']
8
+ attack.name = json['name']
9
+ attack.text = json['text']
10
+ attack.damage = json['damage']
11
+ attack.converted_energy_cost = json['convertedEnergyCost']
12
+
13
+ attack
14
+ end
15
+ end
16
+ end
@@ -1,43 +1,70 @@
1
- require_relative 'representers/card_representer'
2
-
3
- module Pokemon
4
- class Card
5
- include Roar::JSON
6
- include CardRepresenter
7
-
8
- attr_accessor :id, :name, :image_url, :image_url_hi_res, :subtype, :supertype, :ability,
9
- :hp, :number, :artist, :rarity, :series, :set, :set_code,
10
- :retreat_cost, :text, :types, :attacks, :weaknesses, :resistances,
11
- :national_pokedex_number, :ancient_trait, :evolves_from
12
-
13
- # Get the resource string
14
- #
15
- # @return [String] The API resource string
16
- def self.Resource
17
- "cards"
18
- end
19
-
20
- # Find a single card by the card id
21
- #
22
- # @param id [String] the card id
23
- # @return [Card] the Card object response
24
- def self.find(id)
25
- QueryBuilder.new(Card).find(id)
26
- end
27
-
28
- # Get all cards from a query by paging through data
29
- #
30
- # @return [Array<Card>] Array of Card objects
31
- def self.all
32
- QueryBuilder.new(Card).all
33
- end
34
-
35
- # Adds a parameter to the hash of query parameters
36
- #
37
- # @param args [Hash] the query parameter
38
- # @return [QueryBuilder] the QueryBuilder
39
- def self.where(args)
40
- QueryBuilder.new(Card).where(args)
41
- end
42
- end
1
+ require 'erb'
2
+
3
+ module Pokemon
4
+ class Card
5
+ attr_accessor :id, :name, :supertype, :subtypes, :level, :hp, :types, :evolves_from, :evolves_to, :rules, :ancient_trait,
6
+ :abilities, :attacks, :weaknesses, :resistances, :retreat_cost, :converted_retreat_cost, :set, :number, :artist, :rarity, :national_pokedex_numbers,
7
+ :legalities, :tcgplayer, :images, :flavor_text
8
+
9
+ def self.from_json(json)
10
+ card = Card.new
11
+ card.id = json['id']
12
+ card.name = json['name']
13
+ card.supertype = json['supertype']
14
+ card.subtypes = json['subtypes']
15
+ card.level = json['level']
16
+ card.hp = json['hp']
17
+ card.types = json['types']
18
+ card.evolves_from = json['evolvesFrom']
19
+ card.evolves_to = json['evolvesTo']
20
+ card.rules = json['rules']
21
+ card.ancient_trait = AncientTrait.from_json(json['ancientTrait']) if !json['ancientTrait'].nil?
22
+ card.abilities = json['abilities'].map {|ability_json| Ability.from_json(ability_json)} if !json['abilities'].nil?
23
+ card.attacks = json['attacks'].map {|attack_json| Attack.from_json(attack_json)} if !json['attacks'].nil?
24
+ card.weaknesses = json['weaknesses'].map {|weakness_json| Weakness.from_json(weakness_json)} if !json['weaknesses'].nil?
25
+ card.resistances = json['resistances'].map {|resistance_json| Resistance.from_json(resistance_json)} if !json['resistances'].nil?
26
+ card.retreat_cost = json['retreatCost']
27
+ card.converted_retreat_cost = json['convertedRetreatCost']
28
+ card.set = Set.from_json(json['set']) if !json['set'].nil?
29
+ card.number = json['number']
30
+ card.artist = json['artist']
31
+ card.rarity = json['rarity']
32
+ card.flavor_text = json['flavorText']
33
+ card.national_pokedex_numbers = json['nationalPokedexNumbers']
34
+ card.legalities = Legalities.from_json(json['legalities']) if !json['legalities'].nil?
35
+ card.tcgplayer = Tcgplayer.from_json(json['tcgplayer']) if !json['tcgplayer'].nil?
36
+ card.images = CardImages.from_json(json['images']) if !json['images'].nil?
37
+ card
38
+ end
39
+
40
+ # Get the resource string
41
+ #
42
+ # @return [String] The API resource string
43
+ def self.Resource
44
+ "cards"
45
+ end
46
+
47
+ # Find a single card by the card id
48
+ #
49
+ # @param id [String] the card id
50
+ # @return [Card] the Card object response
51
+ def self.find(id)
52
+ QueryBuilder.new(Card).find(ERB::Util.url_encode(id))
53
+ end
54
+
55
+ # Get all cards from a query by paging through data
56
+ #
57
+ # @return [Array<Card>] Array of Card objects
58
+ def self.all
59
+ QueryBuilder.new(Card).all
60
+ end
61
+
62
+ # Adds a parameter to the hash of query parameters
63
+ #
64
+ # @param args [Hash] the query parameter
65
+ # @return [Array<Card>] Array of Card objects
66
+ def self.where(args)
67
+ QueryBuilder.new(Card).where(args)
68
+ end
69
+ end
43
70
  end
@@ -0,0 +1,13 @@
1
+ module Pokemon
2
+ class CardImages
3
+ attr_accessor :small, :large
4
+
5
+ def self.from_json(json)
6
+ images = CardImages.new
7
+ images.small = json['small']
8
+ images.large = json['large']
9
+
10
+ images
11
+ end
12
+ end
13
+ end
@@ -1,9 +1,9 @@
1
- module Pokemon
2
- class Configuration
3
- attr_accessor :api_version
4
-
5
- def initialize
6
- @api_version = 1
7
- end
8
- end
1
+ module Pokemon
2
+ # Placeholder for future configuration options
3
+ class Configuration
4
+ attr_accessor :api_key
5
+
6
+ def initialize
7
+ end
8
+ end
9
9
  end
@@ -0,0 +1,14 @@
1
+ module Pokemon
2
+ class Legalities
3
+ attr_accessor :expanded, :standard, :unlimited
4
+
5
+ def self.from_json(json)
6
+ legality = Legalities.new
7
+ legality.expanded = json['expanded']
8
+ legality.standard = json['standard']
9
+ legality.unlimited = json['unlimited']
10
+
11
+ legality
12
+ end
13
+ end
14
+ end
@@ -1,68 +1,64 @@
1
- require_relative 'rest_client'
2
-
3
- module Pokemon
4
- class QueryBuilder
5
- include RestClient
6
- attr_accessor :type, :query
7
-
8
- def initialize(type)
9
- @type = type
10
- @query = {}
11
- end
12
-
13
- # Adds a parameter to the hash of query parameters
14
- #
15
- # @param args [Hash] the query parameter
16
- # @return [QueryBuilder] the QueryBuilder
17
- def where(args)
18
- @query.merge!(args)
19
- self
20
- end
21
-
22
- # Find a single resource by the resource id
23
- #
24
- # @param id [String] the resource id
25
- # @return [Object] the Type object response
26
- def find(id)
27
- response = RestClient.get("#{@type.Resource}/#{id}")
28
- singular_resource = @type.Resource[0...-1]
29
- if response.body[singular_resource].nil?
30
- raise ArgumentError, 'Resource not found'
31
- end
32
-
33
- type.new.from_json(response.body[singular_resource].to_json)
34
- end
35
-
36
- # Get all resources from a query by paging through data
37
- #
38
- # @return [Array<Object>] Array of resources
39
- def all
40
- list = []
41
- page = 1
42
- fetch_all = true
43
-
44
- if @query.has_key?(:page)
45
- page = @query[:page]
46
- fetch_all = false
47
- end
48
-
49
- while true
50
- response = RestClient.get(@type.Resource, @query)
51
- data = response.body[@type.Resource]
52
- if !data.empty?
53
- data.each {|item| list << @type.new.from_json(item.to_json)}
54
-
55
- if !fetch_all
56
- break
57
- else
58
- where(page: page += 1)
59
- end
60
- else
61
- break
62
- end
63
- end
64
-
65
- return list
66
- end
67
- end
1
+ require_relative 'rest_client'
2
+ require 'json'
3
+
4
+ module Pokemon
5
+ class QueryBuilder
6
+ include RestClient
7
+ attr_accessor :type, :query
8
+
9
+ def initialize(type)
10
+ @type = type
11
+ @query = {}
12
+ end
13
+
14
+ # Adds a parameter to the hash of query parameters
15
+ #
16
+ # @param args [Hash] the query parameter
17
+ # @return [QueryBuilder] the QueryBuilder
18
+ def where(args)
19
+ @query.merge!(args)
20
+ self.all
21
+ end
22
+
23
+ # Find a single resource by the resource id
24
+ #
25
+ # @param id [String] the resource id
26
+ # @return [Object] the Type object response
27
+ def find(id)
28
+ response = RestClient.get("#{@type.Resource}/#{id}")
29
+ @type.from_json response['data']
30
+ end
31
+
32
+ # Get all resources from a query by paging through data
33
+ #
34
+ # @return [Array<Object>] Array of resources
35
+ def all
36
+ list = []
37
+ page = 1
38
+ fetch_all = true
39
+
40
+ if @query.has_key?(:page)
41
+ page = @query[:page]
42
+ fetch_all = false
43
+ end
44
+
45
+ while true
46
+ response = RestClient.get(@type.Resource, @query)
47
+ data = response['data']
48
+ if !data.nil? && data.any?
49
+ data.each {|item| list << @type.from_json(item)}
50
+
51
+ if !fetch_all
52
+ break
53
+ else
54
+ where(page: page += 1)
55
+ end
56
+ else
57
+ break
58
+ end
59
+ end
60
+
61
+ return list
62
+ end
63
+ end
68
64
  end