Module: AsanaExceptionNotifier::Request::Core

Includes:
ApplicationHelper
Included in:
Client
Defined in:
lib/asana_exception_notifier/request/core.rb

Overview

module that is used for formatting numbers using metrics

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

add_files_to_zip, compress_files, create_archive, create_upload_file_part, default_template_path, ensure_eventmachine_running, escape, execute_with_rescue, extract_body, file_upload_request_options, force_utf8_encoding, get_extension_and_name_from_file, get_hash_rows, get_multi_request_values, get_response_from_request, hash_to_html_attributes, link_helper, log_bactrace, log_exception, logger, max_length, mount_table, mount_table_for_hash, multi_request_manager, multipart_file_upload_details, parse_fieldset_value, permitted_options, register_em_error_handler, rescue_interrupt, root, run_em_reactor, set_fieldset_key, setup_em_options, show_hash_content, split_archive, tempfile_details, template_dir, template_path_exist

Instance Attribute Details

#base_urlString

Returns THe base_url of the API.

Returns:

  • (String)

    THe base_url of the API



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asana_exception_notifier/request/core.rb', line 12

module Core
  include AsanaExceptionNotifier::ApplicationHelper

  # Returns the connection options used for connecting to API's
  #
  # @return [Hash] Returns the connection options used for connecting to API's
  def em_connection_options
    {
      connect_timeout: 1200, # default connection setup timeout
      inactivity_timeout: 120, # default connection inactivity (post-setup) timeout
      ssl: {
        verify_peer: false
      },
      head: {
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      }
    }
  end

  # Returns the request options used for connecting to API's
  #
  # @return [Hash] Returns the request options used for connecting to API's
  def em_request_options(params = {})
    {
      redirects: 5, # follow 3XX redirects up to depth 5
      keepalive: true, # enable keep-alive (don't send Connection:close header)
      head: (params[:head] || {}).merge(
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      ),
      body: (params[:body] || {})
    }
  end

  # instantiates an eventmachine http request object that will be used to make the htpp request
  # @see EventMachine::HttpRequest#initialize
  #
  # @param [String] url The URL that will be used in the HTTP request
  # @return [EventMachine::HttpRequest] Returns an http request object
  def em_request(url, options)
    uri = Addressable::URI.parse(url)
    conn_options = em_connection_options.merge(ssl: { sni_hostname: uri.host })
    em_request = EventMachine::HttpRequest.new(url, conn_options)
    em_request.send(options.fetch(:http_method, 'get'), em_request_options)
  end

  # Method that fetch the data from a URL and registers the error and success callback to the HTTP object
  # @see #em_request
  # @see #register_error_callback
  # @see #register_success_callback
  #
  # @param [url] url The URL that is used to fetch data from
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def fetch_data(options = {}, &block)
    options = options.symbolize_keys
    if options[:multi_request] && multi_manager.present?
      multi_fetch_data(options, &block)
    else
      register_error_callback(@http)
      register_success_callback(@http, options, &block)
    end
  end

  def multi_fetch_data(options = {}, &block)
    multi_manager.add options[:request_name], @http
    return unless options[:request_final]
    register_error_callback(multi_manager)
    register_success_callback(multi_manager, options, &block)
  end

  # Method that is used to register a success callback to a http object
  # @see #callback_before_success
  # @see #dispatch_http_response
  #
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for registering the success callback
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def register_success_callback(http, options)
    http.callback do
      res = callback_before_success(get_response_from_request(http, options))
      callback = options.fetch('callback', nil)
      block_given? ? yield(res) : callback.call(res)
    end
  end

  # Callback that is used before returning the response the the instance
  #
  # @param [String] response The response that will be dispatched to the instance class that made the request
  # @return [String] Returns the response
  def callback_before_success(response)
    response
  end

  # This method is used to reqister a error callback to a HTTP request object
  # @see #callback_error
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for reqisteringt the error callback
  # @return [void]
  def register_error_callback(http)
    http.errback { |error| callback_error(error) }
  end

  def get_error_from_request(http, options)
    http_response = http.respond_to?(:response) ? http.response : http.responses[:errback]
    options[:multi_request].present? && http_response.is_a?(Hash) ? http_response.values.map(&:response) : http_response
  end

  # Method that is used to react when an error happens in a HTTP request
  # and prints out an error message
  #
  # @param [Object] error The error that was raised by the HTTP request
  # @return [void]
  def callback_error(error)
    log_exception(error)
  end
end

#hostnameString

Returns THe hostname from where the badges are fetched from.

Returns:

  • (String)

    THe hostname from where the badges are fetched from



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asana_exception_notifier/request/core.rb', line 12

module Core
  include AsanaExceptionNotifier::ApplicationHelper

  # Returns the connection options used for connecting to API's
  #
  # @return [Hash] Returns the connection options used for connecting to API's
  def em_connection_options
    {
      connect_timeout: 1200, # default connection setup timeout
      inactivity_timeout: 120, # default connection inactivity (post-setup) timeout
      ssl: {
        verify_peer: false
      },
      head: {
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      }
    }
  end

  # Returns the request options used for connecting to API's
  #
  # @return [Hash] Returns the request options used for connecting to API's
  def em_request_options(params = {})
    {
      redirects: 5, # follow 3XX redirects up to depth 5
      keepalive: true, # enable keep-alive (don't send Connection:close header)
      head: (params[:head] || {}).merge(
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      ),
      body: (params[:body] || {})
    }
  end

  # instantiates an eventmachine http request object that will be used to make the htpp request
  # @see EventMachine::HttpRequest#initialize
  #
  # @param [String] url The URL that will be used in the HTTP request
  # @return [EventMachine::HttpRequest] Returns an http request object
  def em_request(url, options)
    uri = Addressable::URI.parse(url)
    conn_options = em_connection_options.merge(ssl: { sni_hostname: uri.host })
    em_request = EventMachine::HttpRequest.new(url, conn_options)
    em_request.send(options.fetch(:http_method, 'get'), em_request_options)
  end

  # Method that fetch the data from a URL and registers the error and success callback to the HTTP object
  # @see #em_request
  # @see #register_error_callback
  # @see #register_success_callback
  #
  # @param [url] url The URL that is used to fetch data from
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def fetch_data(options = {}, &block)
    options = options.symbolize_keys
    if options[:multi_request] && multi_manager.present?
      multi_fetch_data(options, &block)
    else
      register_error_callback(@http)
      register_success_callback(@http, options, &block)
    end
  end

  def multi_fetch_data(options = {}, &block)
    multi_manager.add options[:request_name], @http
    return unless options[:request_final]
    register_error_callback(multi_manager)
    register_success_callback(multi_manager, options, &block)
  end

  # Method that is used to register a success callback to a http object
  # @see #callback_before_success
  # @see #dispatch_http_response
  #
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for registering the success callback
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def register_success_callback(http, options)
    http.callback do
      res = callback_before_success(get_response_from_request(http, options))
      callback = options.fetch('callback', nil)
      block_given? ? yield(res) : callback.call(res)
    end
  end

  # Callback that is used before returning the response the the instance
  #
  # @param [String] response The response that will be dispatched to the instance class that made the request
  # @return [String] Returns the response
  def callback_before_success(response)
    response
  end

  # This method is used to reqister a error callback to a HTTP request object
  # @see #callback_error
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for reqisteringt the error callback
  # @return [void]
  def register_error_callback(http)
    http.errback { |error| callback_error(error) }
  end

  def get_error_from_request(http, options)
    http_response = http.respond_to?(:response) ? http.response : http.responses[:errback]
    options[:multi_request].present? && http_response.is_a?(Hash) ? http_response.values.map(&:response) : http_response
  end

  # Method that is used to react when an error happens in a HTTP request
  # and prints out an error message
  #
  # @param [Object] error The error that was raised by the HTTP request
  # @return [void]
  def callback_error(error)
    log_exception(error)
  end
end

#paramsHash

Returns THe params received from URL.

Returns:

  • (Hash)

    THe params received from URL



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asana_exception_notifier/request/core.rb', line 12

module Core
  include AsanaExceptionNotifier::ApplicationHelper

  # Returns the connection options used for connecting to API's
  #
  # @return [Hash] Returns the connection options used for connecting to API's
  def em_connection_options
    {
      connect_timeout: 1200, # default connection setup timeout
      inactivity_timeout: 120, # default connection inactivity (post-setup) timeout
      ssl: {
        verify_peer: false
      },
      head: {
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      }
    }
  end

  # Returns the request options used for connecting to API's
  #
  # @return [Hash] Returns the request options used for connecting to API's
  def em_request_options(params = {})
    {
      redirects: 5, # follow 3XX redirects up to depth 5
      keepalive: true, # enable keep-alive (don't send Connection:close header)
      head: (params[:head] || {}).merge(
        'ACCEPT' => '*/*',
        'Connection' => 'keep-alive'
      ),
      body: (params[:body] || {})
    }
  end

  # instantiates an eventmachine http request object that will be used to make the htpp request
  # @see EventMachine::HttpRequest#initialize
  #
  # @param [String] url The URL that will be used in the HTTP request
  # @return [EventMachine::HttpRequest] Returns an http request object
  def em_request(url, options)
    uri = Addressable::URI.parse(url)
    conn_options = em_connection_options.merge(ssl: { sni_hostname: uri.host })
    em_request = EventMachine::HttpRequest.new(url, conn_options)
    em_request.send(options.fetch(:http_method, 'get'), em_request_options)
  end

  # Method that fetch the data from a URL and registers the error and success callback to the HTTP object
  # @see #em_request
  # @see #register_error_callback
  # @see #register_success_callback
  #
  # @param [url] url The URL that is used to fetch data from
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def fetch_data(options = {}, &block)
    options = options.symbolize_keys
    if options[:multi_request] && multi_manager.present?
      multi_fetch_data(options, &block)
    else
      register_error_callback(@http)
      register_success_callback(@http, options, &block)
    end
  end

  def multi_fetch_data(options = {}, &block)
    multi_manager.add options[:request_name], @http
    return unless options[:request_final]
    register_error_callback(multi_manager)
    register_success_callback(multi_manager, options, &block)
  end

  # Method that is used to register a success callback to a http object
  # @see #callback_before_success
  # @see #dispatch_http_response
  #
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for registering the success callback
  # @param [Lambda] callback The callback that will be called if the response is blank
  # @param [Proc] block If the response is not blank, the block will receive the response
  # @return [void]
  def register_success_callback(http, options)
    http.callback do
      res = callback_before_success(get_response_from_request(http, options))
      callback = options.fetch('callback', nil)
      block_given? ? yield(res) : callback.call(res)
    end
  end

  # Callback that is used before returning the response the the instance
  #
  # @param [String] response The response that will be dispatched to the instance class that made the request
  # @return [String] Returns the response
  def callback_before_success(response)
    response
  end

  # This method is used to reqister a error callback to a HTTP request object
  # @see #callback_error
  # @param [EventMachine::HttpRequest] http The HTTP object that will be used for reqisteringt the error callback
  # @return [void]
  def register_error_callback(http)
    http.errback { |error| callback_error(error) }
  end

  def get_error_from_request(http, options)
    http_response = http.respond_to?(:response) ? http.response : http.responses[:errback]
    options[:multi_request].present? && http_response.is_a?(Hash) ? http_response.values.map(&:response) : http_response
  end

  # Method that is used to react when an error happens in a HTTP request
  # and prints out an error message
  #
  # @param [Object] error The error that was raised by the HTTP request
  # @return [void]
  def callback_error(error)
    log_exception(error)
  end
end

Instance Method Details

#callback_before_success(response) ⇒ String

Callback that is used before returning the response the the instance

Parameters:

  • response (String)

    The response that will be dispatched to the instance class that made the request

Returns:

  • (String)

    Returns the response



105
106
107
# File 'lib/asana_exception_notifier/request/core.rb', line 105

def callback_before_success(response)
  response
end

#callback_error(error) ⇒ void

This method returns an undefined value.

Method that is used to react when an error happens in a HTTP request and prints out an error message

Parameters:

  • error (Object)

    The error that was raised by the HTTP request



127
128
129
# File 'lib/asana_exception_notifier/request/core.rb', line 127

def callback_error(error)
  log_exception(error)
end

#em_connection_optionsHash

Returns the connection options used for connecting to API’s

Returns:

  • (Hash)

    Returns the connection options used for connecting to API’s



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/asana_exception_notifier/request/core.rb', line 18

def em_connection_options
  {
    connect_timeout: 1200, # default connection setup timeout
    inactivity_timeout: 120, # default connection inactivity (post-setup) timeout
    ssl: {
      verify_peer: false
    },
    head: {
      'ACCEPT' => '*/*',
      'Connection' => 'keep-alive'
    }
  }
end

#em_request(url, options) ⇒ EventMachine::HttpRequest

instantiates an eventmachine http request object that will be used to make the htpp request

Parameters:

  • url (String)

    The URL that will be used in the HTTP request

Returns:

  • (EventMachine::HttpRequest)

    Returns an http request object

See Also:

  • EventMachine::HttpRequest#initialize


52
53
54
55
56
57
# File 'lib/asana_exception_notifier/request/core.rb', line 52

def em_request(url, options)
  uri = Addressable::URI.parse(url)
  conn_options = em_connection_options.merge(ssl: { sni_hostname: uri.host })
  em_request = EventMachine::HttpRequest.new(url, conn_options)
  em_request.send(options.fetch(:http_method, 'get'), em_request_options)
end

#em_request_options(params = {}) ⇒ Hash

Returns the request options used for connecting to API’s

Returns:

  • (Hash)

    Returns the request options used for connecting to API’s



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asana_exception_notifier/request/core.rb', line 35

def em_request_options(params = {})
  {
    redirects: 5, # follow 3XX redirects up to depth 5
    keepalive: true, # enable keep-alive (don't send Connection:close header)
    head: (params[:head] || {}).merge(
      'ACCEPT' => '*/*',
      'Connection' => 'keep-alive'
    ),
    body: (params[:body] || {})
  }
end

#fetch_data(options = {}, &block) ⇒ void

This method returns an undefined value.

Method that fetch the data from a URL and registers the error and success callback to the HTTP object

Parameters:

  • url (url)

    The URL that is used to fetch data from

  • callback (Lambda)

    The callback that will be called if the response is blank

  • block (Proc)

    If the response is not blank, the block will receive the response

See Also:



68
69
70
71
72
73
74
75
76
# File 'lib/asana_exception_notifier/request/core.rb', line 68

def fetch_data(options = {}, &block)
  options = options.symbolize_keys
  if options[:multi_request] && multi_manager.present?
    multi_fetch_data(options, &block)
  else
    register_error_callback(@http)
    register_success_callback(@http, options, &block)
  end
end

#get_error_from_request(http, options) ⇒ Object



117
118
119
120
# File 'lib/asana_exception_notifier/request/core.rb', line 117

def get_error_from_request(http, options)
  http_response = http.respond_to?(:response) ? http.response : http.responses[:errback]
  options[:multi_request].present? && http_response.is_a?(Hash) ? http_response.values.map(&:response) : http_response
end

#multi_fetch_data(options = {}, &block) ⇒ Object



78
79
80
81
82
83
# File 'lib/asana_exception_notifier/request/core.rb', line 78

def multi_fetch_data(options = {}, &block)
  multi_manager.add options[:request_name], @http
  return unless options[:request_final]
  register_error_callback(multi_manager)
  register_success_callback(multi_manager, options, &block)
end

#register_error_callback(http) ⇒ void

This method returns an undefined value.

This method is used to reqister a error callback to a HTTP request object

Parameters:

  • http (EventMachine::HttpRequest)

    The HTTP object that will be used for reqisteringt the error callback

See Also:



113
114
115
# File 'lib/asana_exception_notifier/request/core.rb', line 113

def register_error_callback(http)
  http.errback { |error| callback_error(error) }
end

#register_success_callback(http, options) ⇒ void

This method returns an undefined value.

Method that is used to register a success callback to a http object

Parameters:

  • http (EventMachine::HttpRequest)

    The HTTP object that will be used for registering the success callback

  • callback (Lambda)

    The callback that will be called if the response is blank

  • block (Proc)

    If the response is not blank, the block will receive the response

See Also:



93
94
95
96
97
98
99
# File 'lib/asana_exception_notifier/request/core.rb', line 93

def register_success_callback(http, options)
  http.callback do
    res = callback_before_success(get_response_from_request(http, options))
    callback = options.fetch('callback', nil)
    block_given? ? yield(res) : callback.call(res)
  end
end