Class: Opbeat::ErrorMessage::HTTP

Inherits:
Struct
  • Object
show all
Defined in:
lib/opbeat/error_message/http.rb

Constant Summary collapse

HTTP_ENV_KEY =
/^HTTP_/.freeze
UNDERSCORE =
"_".freeze
DASH =
"-".freeze
QUESTION =
"?".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#to_h

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies

Returns:

  • (Object)

    the current value of cookies



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def cookies
  @cookies
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def data
  @data
end

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def env
  @env
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def headers
  @headers
end

#http_hostObject

Returns the value of attribute http_host

Returns:

  • (Object)

    the current value of http_host



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def http_host
  @http_host
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def method
  @method
end

#query_stringObject

Returns the value of attribute query_string

Returns:

  • (Object)

    the current value of query_string



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def query_string
  @query_string
end

#remote_hostObject

Returns the value of attribute remote_host

Returns:

  • (Object)

    the current value of remote_host



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def remote_host
  @remote_host
end

#secureObject

Returns the value of attribute secure

Returns:

  • (Object)

    the current value of secure



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def secure
  @secure
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent

Returns:

  • (Object)

    the current value of user_agent



3
4
5
# File 'lib/opbeat/error_message/http.rb', line 3

def user_agent
  @user_agent
end

Class Method Details

.from_rack_env(env, opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opbeat/error_message/http.rb', line 12

def self.from_rack_env env, opts = {}
  if defined?(ActionDispatch::Request) && env.is_a?(ActionDispatch::Request)
    req = env
  else
    req = Rack::Request.new env
  end

  http = new(
    req.url.split(QUESTION).first,               # url
    req.request_method,                          # method
    nil,                                         # data
    req.query_string,                            # query string
    env['HTTP_COOKIE'],                          # cookies
    {},                                          # headers
    req.ip,                                      # remote host
    req.host_with_port,                          # http host
    req.user_agent,                              # user agent
    req.scheme == 'https'.freeze ? true : false, # secure
    {}                                           # env
  )

  # In Rails < 5 ActionDispatch::Request inherits from Hash
  headers = env.respond_to?(:headers) ? env.headers : env

  headers.each do |k, v|
    next unless k.upcase == k # lower case stuff isn't relevant

    if k.match(HTTP_ENV_KEY)
      header = k.gsub(HTTP_ENV_KEY, '')
        .split(UNDERSCORE).map(&:capitalize).join(DASH)
      http.headers[header] = v.to_s
    else
      http.env[k] = v.to_s
    end
  end

  if req.form_data?
    http.data = req.POST
  elsif req.body
    http.data = req.body.read
    req.body.rewind
  end

  if filter = opts[:filter]
    http.apply_filter filter
  end

  http
end

Instance Method Details

#apply_filter(filter) ⇒ Object



62
63
64
65
66
# File 'lib/opbeat/error_message/http.rb', line 62

def apply_filter filter
  self.data = filter.apply data
  self.query_string = filter.apply query_string
  self.cookies = filter.apply cookies
end