Class: Kynetx::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/kns_endpoint.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kns_endpoint.rb', line 59

def initialize(opts={})
  @environment = opts[:environment] if opts[:environment]
  @ruleset = opts[:ruleset] if opts[:ruleset]
  @use_session = opts[:use_session] if opts[:use_session]
  @query_timeout = opts[:query_timeout] if opts[:query_timeout]
  @logging = opts[:logging] if opts[:logging]
  @headers = opts[:headers] if opts[:headers]
  
  @events = self.class.events
  @directives = self.class.directives
  @ruleset ||= self.class.c_ruleset 
  @domain = self.class.c_domain 
  @environment ||= self.class.c_environment || :production
  @use_session ||= self.class.c_use_session || true
  
  # instance level settings
  @query_timeout ||= 120
  @headers ||= {}
  @logging ||= false
  @log = []
  @params = {}
  raise "No events defined" unless @events 
  raise "No directives defined" unless @directives
  raise "Undefined ruleset." unless @ruleset
  raise "Undefined domain." unless @domain
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

allow calling events directly



95
96
97
98
99
100
101
# File 'lib/kns_endpoint.rb', line 95

def method_missing(meth, *args)
  if @events.keys.include? meth.to_sym
    return run_event(meth.to_sym, args.first)
  else
    super
  end
end

Class Attribute Details

.c_domainObject (readonly)

Returns the value of attribute c_domain.



14
15
16
# File 'lib/kns_endpoint.rb', line 14

def c_domain
  @c_domain
end

.c_environmentObject (readonly)

Returns the value of attribute c_environment.



14
15
16
# File 'lib/kns_endpoint.rb', line 14

def c_environment
  @c_environment
end

.c_rulesetObject (readonly)

Returns the value of attribute c_ruleset.



14
15
16
# File 'lib/kns_endpoint.rb', line 14

def c_ruleset
  @c_ruleset
end

.c_use_sessionObject (readonly)

Returns the value of attribute c_use_session.



14
15
16
# File 'lib/kns_endpoint.rb', line 14

def c_use_session
  @c_use_session
end

.directivesObject (readonly)

Returns the value of attribute directives.



13
14
15
# File 'lib/kns_endpoint.rb', line 13

def directives
  @directives
end

.eventsObject (readonly)

Returns the value of attribute events.



13
14
15
# File 'lib/kns_endpoint.rb', line 13

def events
  @events
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def environment
  @environment
end

#headersObject

Returns the value of attribute headers.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def headers
  @headers
end

#logObject (readonly)

Returns the value of attribute log.



57
58
59
# File 'lib/kns_endpoint.rb', line 57

def log
  @log
end

#loggingObject

Returns the value of attribute logging.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def logging
  @logging
end

#query_timeoutObject

Returns the value of attribute query_timeout.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def query_timeout
  @query_timeout
end

#rulesetObject

Returns the value of attribute ruleset.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def ruleset
  @ruleset
end

#sessionObject

Returns the value of attribute session.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def session
  @session
end

#use_sessionObject

Returns the value of attribute use_session.



56
57
58
# File 'lib/kns_endpoint.rb', line 56

def use_session
  @use_session
end

Class Method Details

.directive(d, &block) ⇒ Object



22
23
24
25
# File 'lib/kns_endpoint.rb', line 22

def directive(d, &block)
  @directives ||= {}
  @directives[d] = block_given? ? block : lambda { |p| p }
end

.domain(d) ⇒ Object



30
# File 'lib/kns_endpoint.rb', line 30

def domain(d); @c_domain = d end

.environment(e) ⇒ Object



28
# File 'lib/kns_endpoint.rb', line 28

def environment(e); @c_environment = e end

.event(e, params = {}, &block) ⇒ Object



16
17
18
19
20
# File 'lib/kns_endpoint.rb', line 16

def event(e, params={}, &block)
  @events ||= {}
  @events[e] = { :params => params }
  @events[e][:block] = block_given? ? block : lambda { |p| p }
end

.method_missing(meth, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kns_endpoint.rb', line 37

def method_missing(meth, *args)
  if @events.include? meth.to_sym 
    ruleset = @ruleset
    if args.first.class == Symbol
      ruleset = args.first 
      params = args.length > 1 ? args.last : {}
    else
      params = args.first
    end
    e = self.new({:ruleset => ruleset})
    e.signal(meth.to_sym, params)
  else
    super
  end
end

.ruleset(r) ⇒ Object



29
# File 'lib/kns_endpoint.rb', line 29

def ruleset(r); @c_ruleset = r end

.signal(e, params) ⇒ Object



32
33
34
35
# File 'lib/kns_endpoint.rb', line 32

def signal(e, params)
  tmp_endpoint = self.new
  tmp_endpoint.signal(e, params)
end

.use_session(u) ⇒ Object



27
# File 'lib/kns_endpoint.rb', line 27

def use_session(u); @c_use_session = u end

Instance Method Details

#paramsObject



90
91
92
# File 'lib/kns_endpoint.rb', line 90

def params
  @params ||= {}
end

#signal(e, params = {}) ⇒ Object



86
87
88
# File 'lib/kns_endpoint.rb', line 86

def signal(e, params={})
  run_event(e, params)
end