Class: Hooksler::Router

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

Defined Under Namespace

Classes: RouteNotFound

Constant Summary collapse

VALID_TYPES =
[:input, :output].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Class Method Details

.config(&block) ⇒ Object



32
33
34
35
36
# File 'lib/hooksler/router.rb', line 32

def self.config(&block)
  @instance ||= self.new
  @instance.instance_exec &block if block
  @instance
end

.host_name(host = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/hooksler/router.rb', line 55

def self.host_name(host=nil)
  if defined? @host_name
    @host_name
  else
    return ENV['HOST_NAME'] if host.nil?
    @host_name = host
  end
end

.inboundsObject



38
39
40
# File 'lib/hooksler/router.rb', line 38

def self.inbounds
  @channels[:input]
end

.infoObject



46
47
48
49
50
51
52
53
# File 'lib/hooksler/router.rb', line 46

def self.info
  info = {}
  @instance.routes.each do |from, to_list|
    path = host_name + @instance.endpoints.path(from)
    info[path] = {from => to_list.map(&:name) }
  end
  info
end

.outboundsObject



42
43
44
# File 'lib/hooksler/router.rb', line 42

def self.outbounds
  @channels[:output]
end

.register(type, name, klass) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hooksler/router.rb', line 15

def self.register(type, name, klass)
  fail "Unknown type #{type} allow #{VALID_TYPES.join(', ')}" unless VALID_TYPES.include? type

  case type
    when :input
      fail 'Instance must be extended by Hooksler::Inbound' unless klass.is_a? Hooksler::Channel::Input
    when :output
      fail 'Instance must be extended by Hooksler::Outbound' unless klass.is_a? Hooksler::Channel::Output
  end

  @channels[type][name.to_sym] = klass
end

.resolve_path(*args) ⇒ Object



28
29
30
# File 'lib/hooksler/router.rb', line 28

def self.resolve_path(*args)
  @instance.resolve_path(*args) if @instance
end

Instance Method Details

#endpoints(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/hooksler/router.rb', line 68

def endpoints(&block)
  fail 'secret code not set' unless defined? @secret_code

  unless @endpoints
    @endpoints = Hooksler::Endpoints.new(@secret_code)
    @endpoints.instance_exec &block if block
  end
  @endpoints
end

#host_name(host = nil) ⇒ Object



64
65
66
# File 'lib/hooksler/router.rb', line 64

def host_name(host=nil)
  self.class.host_name(host)
end

#resolve_path(path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hooksler/router.rb', line 114

def resolve_path(path)
  return if !@endpoints || path.to_s.empty?

  type, _name, key = path.split('/').select { |s| !(s.nil? || s.empty?) }

  return unless type && _name && key

  fail "unknown type #{type}" unless self.class.inbounds.key? type.to_sym
  from_instance, _type, name = @endpoints.resolve :input, key

  fail RouteNotFound.new "route for #{name} not found" unless @routes.key? name.to_sym

  [from_instance, @routes[name.to_sym]]
rescue KeyError
  nil
end

#route(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hooksler/router.rb', line 88

def route(params)
  fail 'route must be a Hash' unless params.is_a? Hash

  @routes ||= {}

  from, to = params.first

  params.delete from

  [*from].each do |i|
    fail 'from must be string or symbol' unless i.is_a?(String) || i.is_a?(Symbol)

    @routes[i.to_sym] ||= []
    @routes[i.to_sym] += [*to].map do |it|

      fail 'to must be string or symbol' unless it.is_a?(String) || it.is_a?(Symbol)
      output = @endpoints.resolve :output, it

      fail 'unknown out endpoint' unless output

      Hooksler::Route.new it, output, params
    end
  end
  @routes
end

#secret_code(code) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/hooksler/router.rb', line 78

def secret_code(code)
  if defined? @secret_code
    @secret_code
  else
    return nil if code.nil?
    @secret_code = code
  end
end