class Pokemon::QueryBuilder

Attributes

query[RW]
type[RW]

Public Class Methods

new(type) click to toggle source
# File lib/pokemon_tcg_sdk/query_builder.rb, line 9
def initialize(type)
  @type = type
  @query = {}
end

Public Instance Methods

all() click to toggle source

Get all resources from a query by paging through data

@return [Array<Object>] Array of resources

# File lib/pokemon_tcg_sdk/query_builder.rb, line 35
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['data'] 
    if !data.nil? && data.any?
      data.each {|item| list << @type.from_json(item)}
      
      if !fetch_all
        break
      else
        @query.merge!(page: page += 1)
      end
    else
      break
    end
  end

  return list
end
find(id) click to toggle source

Find a single resource by the resource id

@param id [String] the resource id @return [Object] the Type object response

# File lib/pokemon_tcg_sdk/query_builder.rb, line 27
def find(id)
  response = RestClient.get("#{@type.Resource}/#{id}")
  @type.from_json response['data']
end
where(args) click to toggle source

Adds a parameter to the hash of query parameters

@param args [Hash] the query parameter @return [QueryBuilder] the QueryBuilder

# File lib/pokemon_tcg_sdk/query_builder.rb, line 18
def where(args)
  @query.merge!(args)
  self.all
end