Class: Hooksler::Endpoints

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

Instance Method Summary collapse

Constructor Details

#initialize(secret_code) ⇒ Endpoints

Returns a new instance of Endpoints.



6
7
8
9
# File 'lib/hooksler/endpoints.rb', line 6

def initialize(secret_code)
  @secret_code = secret_code
  @instances = {input: {}, output: {}}
end

Instance Method Details

#encode_name(name) ⇒ Object



52
53
54
# File 'lib/hooksler/endpoints.rb', line 52

def encode_name(name)
  Digest::SHA1.hexdigest "#{name}::#{@secret_code}"
end

#input(name, params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hooksler/endpoints.rb', line 11

def input(name, params = {})
  fail 'endpoint type must be set' unless params.key? :type

  type = params.delete(:type)
  klass = Hooksler::Router.inbounds[type.to_sym]
  fail "unknown type #{type}" unless klass

  instance = klass.build(params)

  @instances[:input][encode_name(name)] = [instance, type, name.to_s]

  instance.route_defined(path(name)) if instance

  instance
end

#output(name, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hooksler/endpoints.rb', line 27

def output(name, params = {})
  fail 'endpoint type must be set' unless params.key? :type

  type = params.delete(:type)

  klass = Hooksler::Router.outbounds[type.to_sym]
  fail "unknown output type #{type}" unless klass
  instance = klass.build(params)

  @instances[:output][name.to_s] = instance
  instance
end

#path(name) ⇒ Object



40
41
42
43
44
# File 'lib/hooksler/endpoints.rb', line 40

def path(name)
  _k, (_instance, type, in_name) = @instances[:input].detect { |_k, (_instance, type, in_name)| name.to_s == in_name }

  "/#{type}/#{in_name}/#{encode_name(in_name)}" if _k
end

#resolve(type, key) ⇒ Object



46
47
48
49
50
# File 'lib/hooksler/endpoints.rb', line 46

def resolve(type, key)
  fail 'unknown type #{type}, allowed :in, :out' unless [:input, :output].include?(type)

  @instances.fetch(type).fetch(key.to_s)
end