Class: Gamifier::DSL::Network

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gamifier/dsl/network.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Network

Returns a new instance of Network.



13
14
15
16
17
# File 'lib/gamifier/dsl/network.rb', line 13

def initialize(*args)
  @source         = ::Gamifier::Network.new(*args)
  @sites          = []
  @api_keys       = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



81
82
83
# File 'lib/gamifier/dsl/network.rb', line 81

def method_missing(*args, &block)
  source.send(*args, &block)
end

Instance Attribute Details

#api_keysObject (readonly)

Returns the value of attribute api_keys.



11
12
13
# File 'lib/gamifier/dsl/network.rb', line 11

def api_keys
  @api_keys
end

#sitesObject (readonly)

Returns the value of attribute sites.



10
11
12
# File 'lib/gamifier/dsl/network.rb', line 10

def sites
  @sites
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/gamifier/dsl/network.rb', line 9

def source
  @source
end

Instance Method Details

#api_key(&block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/gamifier/dsl/network.rb', line 35

def api_key(&block)
  new_key = DSL::ApiKey.new
  new_key.source.engine = enterprise_engine
  @api_keys.push(new_key)

  DSL.eval_with_context(new_key, &block)
end

#engineObject



19
20
21
# File 'lib/gamifier/dsl/network.rb', line 19

def engine
  @engine ||= Gamifier::Engine.new(:key => get_key('private') || Gamifier.config[:key])
end

#enterprise_engineObject

Raises:

  • (ArgumentError)


23
24
25
26
# File 'lib/gamifier/dsl/network.rb', line 23

def enterprise_engine
  raise ArgumentError, "You must specify an enterprise key" unless Gamifier.config[:enterprise_key]
  @enterprise_engine ||= Gamifier::Engine.new(:key => Gamifier.config[:enterprise_key])
end

#get_key(access = 'private') ⇒ Object



71
72
73
74
75
# File 'lib/gamifier/dsl/network.rb', line 71

def get_key(access = 'private')
  key = self.api_keys.select {|k| k.access == access}.first
  return key.key if key
  nil
end

#save!Object



77
78
79
# File 'lib/gamifier/dsl/network.rb', line 77

def save!
  save_sites!
end

#save_api_keys!Object



53
54
55
56
57
58
59
# File 'lib/gamifier/dsl/network.rb', line 53

def save_api_keys!
  api_keys.each do |key|
    key.engine = enterprise_engine
    key.network_id = self.id
    key.save
  end
end

#save_network!Object



43
44
45
46
47
48
49
50
51
# File 'lib/gamifier/dsl/network.rb', line 43

def save_network!
  begin
    if self.save
      save_api_keys!
    end
  rescue
    Gamifier.logger.debug "Can't save #{self}"
  end
end

#save_sites!Object



61
62
63
64
65
66
67
68
69
# File 'lib/gamifier/dsl/network.rb', line 61

def save_sites!
  sites.each do |site|
    if gsite = engine.sites.find(site.url)
      site._id = gsite._id
    end
    site.engine = engine
    site.save
  end
end

#site(name, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/gamifier/dsl/network.rb', line 28

def site(name, &block)
  new_site = DSL::Site.new(:name => name)
  @sites.push(new_site)

  DSL.eval_with_context(new_site, &block)
end