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
-
#base_url ⇒ String
THe base_url of the API.
-
#hostname ⇒ String
THe hostname from where the badges are fetched from.
-
#params ⇒ Hash
THe params received from URL.
Instance Method Summary collapse
-
#callback_before_success(response) ⇒ String
Callback that is used before returning the response the the instance.
-
#callback_error(error) ⇒ void
Method that is used to react when an error happens in a HTTP request and prints out an error message.
-
#em_connection_options ⇒ Hash
Returns the connection options used for connecting to API’s.
-
#em_request(url, options) ⇒ EventMachine::HttpRequest
instantiates an eventmachine http request object that will be used to make the htpp request.
-
#em_request_options(params = {}) ⇒ Hash
Returns the request options used for connecting to API’s.
-
#fetch_data(options = {}, &block) ⇒ void
Method that fetch the data from a URL and registers the error and success callback to the HTTP object.
- #get_error_from_request(http, options) ⇒ Object
- #multi_fetch_data(options = {}, &block) ⇒ Object
-
#register_error_callback(http) ⇒ void
This method is used to reqister a error callback to a HTTP request object.
-
#register_success_callback(http, options) ⇒ void
Method that is used to register a success callback to a http object.
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_url ⇒ String
Returns 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 { 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 (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, ) uri = Addressable::URI.parse(url) = .merge(ssl: { sni_hostname: uri.host }) em_request = EventMachine::HttpRequest.new(url, ) em_request.send(.fetch(:http_method, 'get'), ) 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( = {}, &block) = .symbolize_keys if [:multi_request] && multi_manager.present? multi_fetch_data(, &block) else register_error_callback(@http) register_success_callback(@http, , &block) end end def multi_fetch_data( = {}, &block) multi_manager.add [:request_name], @http return unless [:request_final] register_error_callback(multi_manager) register_success_callback(multi_manager, , &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, ) http.callback do res = callback_before_success(get_response_from_request(http, )) callback = .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, ) http_response = http.respond_to?(:response) ? http.response : http.responses[:errback] [: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 |
#hostname ⇒ String
Returns 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 { 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 (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, ) uri = Addressable::URI.parse(url) = .merge(ssl: { sni_hostname: uri.host }) em_request = EventMachine::HttpRequest.new(url, ) em_request.send(.fetch(:http_method, 'get'), ) 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( = {}, &block) = .symbolize_keys if [:multi_request] && multi_manager.present? multi_fetch_data(, &block) else register_error_callback(@http) register_success_callback(@http, , &block) end end def multi_fetch_data( = {}, &block) multi_manager.add [:request_name], @http return unless [:request_final] register_error_callback(multi_manager) register_success_callback(multi_manager, , &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, ) http.callback do res = callback_before_success(get_response_from_request(http, )) callback = .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, ) http_response = http.respond_to?(:response) ? http.response : http.responses[:errback] [: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 |
#params ⇒ Hash
Returns 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 { 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 (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, ) uri = Addressable::URI.parse(url) = .merge(ssl: { sni_hostname: uri.host }) em_request = EventMachine::HttpRequest.new(url, ) em_request.send(.fetch(:http_method, 'get'), ) 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( = {}, &block) = .symbolize_keys if [:multi_request] && multi_manager.present? multi_fetch_data(, &block) else register_error_callback(@http) register_success_callback(@http, , &block) end end def multi_fetch_data( = {}, &block) multi_manager.add [:request_name], @http return unless [:request_final] register_error_callback(multi_manager) register_success_callback(multi_manager, , &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, ) http.callback do res = callback_before_success(get_response_from_request(http, )) callback = .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, ) http_response = http.respond_to?(:response) ? http.response : http.responses[:errback] [: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
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
127 128 129 |
# File 'lib/asana_exception_notifier/request/core.rb', line 127 def callback_error(error) log_exception(error) end |
#em_connection_options ⇒ 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 { 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
52 53 54 55 56 57 |
# File 'lib/asana_exception_notifier/request/core.rb', line 52 def em_request(url, ) uri = Addressable::URI.parse(url) = .merge(ssl: { sni_hostname: uri.host }) em_request = EventMachine::HttpRequest.new(url, ) em_request.send(.fetch(:http_method, 'get'), ) end |
#em_request_options(params = {}) ⇒ 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 (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
68 69 70 71 72 73 74 75 76 |
# File 'lib/asana_exception_notifier/request/core.rb', line 68 def fetch_data( = {}, &block) = .symbolize_keys if [:multi_request] && multi_manager.present? multi_fetch_data(, &block) else register_error_callback(@http) register_success_callback(@http, , &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, ) http_response = http.respond_to?(:response) ? http.response : http.responses[:errback] [: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( = {}, &block) multi_manager.add [:request_name], @http return unless [:request_final] register_error_callback(multi_manager) register_success_callback(multi_manager, , &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
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
93 94 95 96 97 98 99 |
# File 'lib/asana_exception_notifier/request/core.rb', line 93 def register_success_callback(http, ) http.callback do res = callback_before_success(get_response_from_request(http, )) callback = .fetch('callback', nil) block_given? ? yield(res) : callback.call(res) end end |