Class: WebHookDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/webhook-dispatcher/core.rb,
lib/webhook-dispatcher/version.rb,
lib/webhook-dispatcher/request/base.rb,
lib/webhook-dispatcher/acl/deny_entry.rb,
lib/webhook-dispatcher/acl/entry_base.rb,
lib/webhook-dispatcher/acl/allow_entry.rb

Defined Under Namespace

Modules: Request Classes: Acl, Response

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WebHookDispatcher

Returns a new instance of WebHookDispatcher.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/webhook-dispatcher/core.rb', line 11

def initialize(options = {})
  options = options.dup
  @open_timeout = options.delete(:open_timeout) || self.class.open_timeout || self.class.default_open_timeout
  @read_timeout = options.delete(:read_timeout) || self.class.read_timeout || self.class.default_read_timeout
  @user_agent   = options.delete(:user_agent)   || self.class.user_agent   || self.class.default_user_agent
  @acl          = options.delete(:acl)          || self.class.acl          || self.class.default_acl
  raise(ArgumentError, "invalid parameter") unless options.empty?
end

Class Attribute Details

.aclObject

Returns the value of attribute acl.



26
27
28
# File 'lib/webhook-dispatcher/core.rb', line 26

def acl
  @acl
end

.open_timeoutObject

Returns the value of attribute open_timeout.



26
27
28
# File 'lib/webhook-dispatcher/core.rb', line 26

def open_timeout
  @open_timeout
end

.read_timeoutObject

Returns the value of attribute read_timeout.



26
27
28
# File 'lib/webhook-dispatcher/core.rb', line 26

def read_timeout
  @read_timeout
end

.user_agentObject

Returns the value of attribute user_agent.



26
27
28
# File 'lib/webhook-dispatcher/core.rb', line 26

def user_agent
  @user_agent
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



38
39
40
# File 'lib/webhook-dispatcher/core.rb', line 38

def acl
  @acl
end

#open_timeoutObject

Returns the value of attribute open_timeout.



38
39
40
# File 'lib/webhook-dispatcher/core.rb', line 38

def open_timeout
  @open_timeout
end

#read_timeoutObject

Returns the value of attribute read_timeout.



38
39
40
# File 'lib/webhook-dispatcher/core.rb', line 38

def read_timeout
  @read_timeout
end

#user_agentObject

Returns the value of attribute user_agent.



38
39
40
# File 'lib/webhook-dispatcher/core.rb', line 38

def user_agent
  @user_agent
end

Class Method Details

.acl_with(&block) ⇒ Object



33
34
35
# File 'lib/webhook-dispatcher/core.rb', line 33

def acl_with(&block)
  self.acl = Acl.with(&block)
end

Instance Method Details

#acl_with(&block) ⇒ Object



40
41
42
# File 'lib/webhook-dispatcher/core.rb', line 40

def acl_with(&block)
  self.acl = Acl.with(&block)
end

#get(uri) ⇒ Object



86
87
88
# File 'lib/webhook-dispatcher/core.rb', line 86

def get(uri)
  return self.request(Request::Get.new(uri))
end

#head(uri) ⇒ Object



90
91
92
# File 'lib/webhook-dispatcher/core.rb', line 90

def head(uri)
  return self.request(Request::Head.new(uri))
end

#post(uri, body = nil) ⇒ Object



94
95
96
# File 'lib/webhook-dispatcher/core.rb', line 94

def post(uri, body = nil)
  return self.request(Request::Post.new(uri, body))
end

#request(request) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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/webhook-dispatcher/core.rb', line 44

def request(request)
  http_conn = request.create_http_connector
  http_req  = request.create_http_request
  setup_http_connector(http_conn)
  setup_http_request(http_req)

  begin
    if @acl.deny?(http_conn.address, http_conn.port)
      return Response.new(
        :status    => :denied,
        :message   => "denied.")
    end

    http_res = http_conn.start { http_conn.request(http_req) }

    return Response.new(
      :status    => (http_res.kind_of?(Net::HTTPSuccess) ? :success : :failure),
      :http_code => http_res.code.to_i,
      :message   => http_res.message)
  rescue TimeoutError => e
    return Response.new(
      :status    => :timeout,
      :message   => "timeout.",
      :exception => e)
  rescue Errno::ECONNREFUSED => e
    return Response.new(
      :status    => :refused,
      :message   => "connection refused.",
      :exception => e)
  rescue Errno::ECONNRESET => e
    return Response.new(
      :status    => :reset,
      :message   => "connection reset by peer.",
      :exception => e)
  rescue => e
    return Response.new(
      :status    => :error,
      :message   => "#{e.class}: #{e.message}",
      :exception => e)
  end
end