Class: Clashinator::Base
- Inherits:
-
Object
- Object
- Clashinator::Base
- Extended by:
- Camelizable
- Includes:
- Underscorable
- Defined in:
- lib/clashinator/base.rb
Overview
This is the base class for the other entities
Direct Known Subclasses
Clan, ClanRanking, League, Location, Player, PlayerRanking, Warlog
Constant Summary collapse
- CLASS_MAP =
{ member_list: 'Player', achievements: 'Achievement', troops: 'Troop', heroes: 'Hero', spells: 'Spell' }.freeze
- OBJECT_MAP =
{ opponent: 'Clan', league: 'League', location: 'Location', badge_urls: 'BadgeUrl', icon_urls: 'BadgeUrl', clan: 'Clan' }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attrs) ⇒ Base
constructor
A new instance of Base.
- #verify_array_of_classes(lower_camel_cased, val) ⇒ Object
- #verify_hash_that_are_objects(lower_camel_cased, val) ⇒ Object
Methods included from Camelizable
Methods included from Underscorable
Constructor Details
#initialize(attrs) ⇒ Base
Returns a new instance of Base.
20 21 22 23 24 25 26 27 28 |
# File 'lib/clashinator/base.rb', line 20 def initialize(attrs) attrs.each do |name, val| lower_camel_cased = to_underscore(name) (class << self; self; end).send(:attr_reader, lower_camel_cased.to_sym) val = verify_hash_that_are_objects(lower_camel_cased.to_sym, val) val = verify_array_of_classes(lower_camel_cased.to_sym, val) instance_variable_set "@#{lower_camel_cased}", val end end |
Class Method Details
.prepare_options(query_options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clashinator/base.rb', line 67 def self.( = {}) # new hash to store camelcased attributes, to make it work # with the official API = {} .each do |name, val| name = to_camel_case(name.to_s) val.gsub!('#', '%23') if val.class == String [name.to_sym] = val end end |
Instance Method Details
#verify_array_of_classes(lower_camel_cased, val) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/clashinator/base.rb', line 42 def verify_array_of_classes(lower_camel_cased, val) key_found = CLASS_MAP.key?(lower_camel_cased) val = get_array_resource(lower_camel_cased, val) if key_found val end |
#verify_hash_that_are_objects(lower_camel_cased, val) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/clashinator/base.rb', line 30 def verify_hash_that_are_objects(lower_camel_cased, val) if OBJECT_MAP.key? lower_camel_cased class_name = 'Clashinator::' \ "#{OBJECT_MAP[lower_camel_cased]}" val = Object .const_get(class_name) .new(val) end val end |