class HearthstoneCardApi::Data

Attributes

data[R]
format[R]
objs[R]

Public Class Methods

new(api_response) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 5
def initialize(api_response)
  @objs = []
  @data = api_response
  @format = HearthstoneCardApi.data_format
end

Public Instance Methods

formatted() click to toggle source
# File lib/hearthstone_card_api/data.rb, line 11
def formatted
  return self.send("return_#{format}", data)
end

Private Instance Methods

data_to_objects(hash) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 42
def data_to_objects(hash)
  hash.each do |a|
    b = Hash[a.map{|(k,v)| [underscore(k).to_sym, v]}] 
    obj = Struct.new(*b.keys).new(*b.values)
    objs.push(obj)
  end
  return objs
end
return_hash(data) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 37
def return_hash(data)
  hashes = data.body
  return hashes
end
return_json(data) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 32
def return_json(data)
  json = data.raw_body
  return json
end
return_objects(d) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 16
def return_objects(d)
  data = d.body
  if data.is_a? Hash
    if data.key?("error") or data.key?("patch")
      return data
    else
      data.each_pair do |key,value|
        data_to_objects(value)
      end
    end
  elsif data.is_a? Array
    data_to_objects(data)
  end
  return objs
end
underscore(string) click to toggle source
# File lib/hearthstone_card_api/data.rb, line 51
def underscore(string)
  string.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end