-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QueryBuilder to match python sdk more closely
- Loading branch information
Showing
25 changed files
with
127 additions
and
3,130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ rdoc | |
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
test/fixtures | ||
tmp | ||
*.bundle | ||
*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.