Skip to content

Commit

Permalink
Add QueryBuilder to match python sdk more closely
Browse files Browse the repository at this point in the history
  • Loading branch information
adback03 committed Mar 29, 2016
1 parent c8a9243 commit 584e5d1
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 3,130 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rdoc
spec/reports
test/tmp
test/version_tmp
test/fixtures
tmp
*.bundle
*.so
Expand Down
1 change: 1 addition & 0 deletions lib/mtg_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "mtg_sdk/supertype"
require "mtg_sdk/changelog"
require "mtg_sdk/rest_client"
require "mtg_sdk/query_builder"

module MTG
API_URL = 'https://api.magicthegathering.io'
Expand Down
55 changes: 17 additions & 38 deletions lib/mtg_sdk/card.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,44 @@
require_relative 'representers/card_representer'
require_relative 'resource'

module MTG
class Card < Resource
class Card
include Roar::JSON
include CardRepresenter
include RestClient


attr_accessor :name, :layout, :names, :mana_cost, :cmc, :colors, :type, :supertypes, :subtypes, :types,
:rarity, :text, :flavor, :artist, :number, :power, :toughness, :loyalty, :multiverse_id, :variations,
:watermark, :border, :timeshifted, :hand, :life, :reserved, :release_date, :starter,
:rulings, :foreign_names, :printings, :original_text, :original_type, :legalities,
:source, :image_url, :set, :id


# Get the resource string
#
# @return [String] The API resource string
def self.Resource
"cards"
end

# Find a single card by the card multiverse id
#
# @param id [Integer] the multiverse id
# @return [Card] the Card object response
def self.find(id)
response = RestClient.get("cards/#{id}")

if response.body['card'].nil?
raise ArgumentError, 'Card not found'
end

new.from_json(response.body['card'].to_json)
QueryBuilder.new(Card).find(id)
end

# Get all cards from a query by paging through data
#
# @return [Array<Card>] Array of Card objects
def self.all
cards = []
page = 0

while true
where(page: page += 1)
response = RestClient.get('cards', query[:parameters])
data = response.body['cards']
data.empty? ? break : data.each {|card| cards << new.from_json(card.to_json)}
end

@query = nil
cards
QueryBuilder.new(Card).all
end

# Execute a query and convert the response
# into a list of Card objects
# Adds a parameter to the hash of query parameters
#
# @return [Array<Card>] Array of Card objects
def self.get
cards = []
response = RestClient.get('cards', query[:parameters])
data = response.body['cards']

data.each do |card|
cards << new.from_json(card.to_json)
end

@query = nil
cards
# @param args [Hash] the query parameter
# @return [QueryBuilder] the QueryBuilder
def self.where(args)
QueryBuilder.new(Card).where(args)
end
end
end
22 changes: 9 additions & 13 deletions lib/mtg_sdk/changelog.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
require_relative 'representers/changelog_representer'
require_relative 'resource'

module MTG
class Changelog < Resource
class Changelog
include Roar::JSON
include RestClient
include ChangelogRepresenter

attr_accessor :version, :details, :release_date

# Get the resource string
#
# @return [string] The API resource string
def self.Resource
"changelogs"
end

# Get all changelogs
#
# @return [Array<Changelog>] Array of Changelog objects
def self.all
changelogs = []
response = RestClient.get('changelogs')
data = response.body['changelogs']

data.each do |changelog|
changelogs << new.extend(ChangelogRepresenter).from_json(changelog.to_json)
end

@query = nil
changelogs
QueryBuilder.new(Changelog).all
end
end
end
66 changes: 66 additions & 0 deletions lib/mtg_sdk/query_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module MTG
class QueryBuilder
include RestClient
attr_accessor :type, :query

def initialize(type)
@type = type
@query = {}
end

# Adds a parameter to the hash of query parameters
#
# @param args [Hash] the query parameter
# @return [QueryBuilder] the QueryBuilder
def where(args)
@query.merge!(args)
self
end

# Find a single resource by the resource id
#
# @param id [String] the resource id
# @return [Object] the Type object response
def find(id)
response = RestClient.get("#{@type.Resource}/#{id}")
singular_resource = @type.Resource[0...-1]
if response.body[singular_resource].nil?
raise ArgumentError, 'Resource not found'
end

type.new.from_json(response.body[singular_resource].to_json)
end

# Get all resources from a query by paging through data
#
# @return [Array<Object>] Array of resources
def all
list = []
page = 1
fetch_all = true

if @query.has_key?(:page)
page = @query[:page]
fetch_all = false
end

while true
response = RestClient.get(@type.Resource, @query)
data = response.body[@type.Resource]
if !data.empty?
data.each {|item| list << @type.new.from_json(item.to_json)}

if !fetch_all
break
else
where(page: page += 1)
end
else
break
end
end

return list
end
end
end
23 changes: 0 additions & 23 deletions lib/mtg_sdk/resource.rb

This file was deleted.

41 changes: 12 additions & 29 deletions lib/mtg_sdk/set.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative 'representers/set_representer'
require_relative 'card'
require_relative 'resource'
require_relative 'rest_client'

module MTG
class Set < Resource
class Set
include Roar::JSON
include SetRepresenter
include RestClient
Expand All @@ -12,29 +12,30 @@ class Set < Resource
:release_date, :gatherer_code, :magic_cards_info_code, :booster,
:old_code, :block, :online_only

# Get the resource string
#
# @return [string] The API resource string
def self.Resource
"sets"
end

# Find a single set by the set code
#
# @param code [String] the Set code
# @return [Set] a Set object
def self.find(code)
response = RestClient.get("sets/#{code}")

if response.body['set'].nil?
raise ArgumentError, 'Set not found'
end

new.from_json(response.body['set'].to_json)
QueryBuilder.new(Set).find(code)
end

# Get all sets from a query
#
# @return [Array<Set>] Array of Set objects
def self.all
get
QueryBuilder.new(Set).all
end

# Generate a booster pack for a specific set
#

# @param code [String] the Set code
# @return [Array<Card>] Array of Card objects
def self.generate_booster(code)
Expand All @@ -46,25 +47,7 @@ def self.generate_booster(code)
cards << Card.new.from_json(card.to_json)
end

@query = nil
cards
end

# Execute a query and convert the response
# into a list of Set objects
#
# @return [Array<Set>] Array of Set objects
def self.get
sets = []
response = RestClient.get('sets', query[:parameters])
data = response.body['sets']

data.each do |set|
sets << new.from_json(set.to_json)
end

@query = nil
sets
end
end
end
4 changes: 1 addition & 3 deletions lib/mtg_sdk/subtype.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require_relative 'resource'

module MTG
class Subtype < Resource
class Subtype
include RestClient

# Get all subtypes
Expand Down
4 changes: 1 addition & 3 deletions lib/mtg_sdk/supertype.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require_relative 'resource'

module MTG
class Supertype < Resource
class Supertype
include RestClient

# Get all supertypes
Expand Down
4 changes: 1 addition & 3 deletions lib/mtg_sdk/type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require_relative 'resource'

module MTG
class Type < Resource
class Type
include RestClient

# Get all types
Expand Down
2 changes: 1 addition & 1 deletion lib/mtg_sdk/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MTG
VERSION = "1.1.1"
VERSION = "2.0.0"
end
25 changes: 9 additions & 16 deletions test/card_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,27 @@ def test_find_with_invalid_id_throws_exception
end
end

def test_where_and_get_returns_cards
def test_where_with_page_size_and_page_returns_cards
VCR.use_cassette('query_cards_pageSize') do
cards = MTG::Card.where(pageSize: 10).get
cards = MTG::Card.where(pageSize: 10).where(page: 1).all

# make sure we got a ton of cards
# make sure we got only 10 cards back
assert cards.length == 10
assert cards.kind_of?(Array)
assert cards.first.kind_of?(MTG::Card)
end
end

def test_where_appends_to_query
VCR.use_cassette('query_zurgo') do
name = 'zurgo'
query = MTG::Card.where(name: name).query
parameters = query[:parameters]
MTG::Card.get
assert_equal name, parameters[:name]
end
end


def test_all_returns_cards
VCR.use_cassette('all_filtered') do
cards = MTG::Card.where(supertypes: 'legendary')
.where(subtypes: 'elf,warrior')
.all

puts cards.length

first_card = cards[0]
assert first_card.supertypes.include? 'Legendary'
assert first_card.subtypes.include? 'Elf'
assert first_card.subtypes.include? 'Warrior'
end
end
end
Loading

0 comments on commit 584e5d1

Please sign in to comment.