Class: Clashinator::Base

Inherits:
Object
  • Object
show all
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

Methods included from Camelizable

to_camel_case

Methods included from Underscorable

#to_underscore

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.prepare_options(query_options = {})
  # new hash to store camelcased attributes, to make it work
  # with the official API
  new_query_options = {}
  query_options.each do |name, val|
    name = to_camel_case(name.to_s)
    val.gsub!('#', '%23') if val.class == String
    new_query_options[name.to_sym] = val
  end

  new_query_options
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