Class: AsanaExceptionNotifier::Request::Client

Inherits:
Object
  • Object
show all
Includes:
Core, EM::Deferrable
Defined in:
lib/asana_exception_notifier/request/client.rb

Overview

class used to make request in deferrable way

Instance Attribute Summary collapse

Attributes included from Core

#base_url, #hostname, #params

Instance Method Summary collapse

Methods included from Core

#callback_before_success, #callback_error, #em_connection_options, #em_request, #fetch_data, #get_error_from_request, #multi_fetch_data, #register_error_callback, #register_success_callback

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

Constructor Details

#initialize(api_key, url, options, &callback) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/asana_exception_notifier/request/client.rb', line 11

def initialize(api_key, url, options, &callback)
  @api_key = api_key
  @url = url

  @options = options.symbolize_keys
  @request_name = @options.fetch(:request_name, '')
  @request_final = @options.fetch(:request_final, false)
  @action = @options.fetch(:action, '')

  self.callback(&callback)

  send_request_and_rescue
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def action
  @action
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def api_key
  @api_key
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def options
  @options
end

#request_finalObject (readonly)

Returns the value of attribute request_final.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def request_final
  @request_final
end

#request_nameObject (readonly)

Returns the value of attribute request_name.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def request_name
  @request_name
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/asana_exception_notifier/request/client.rb', line 9

def url
  @url
end

Instance Method Details

#callback_task_creation(data) ⇒ Object



80
81
82
# File 'lib/asana_exception_notifier/request/client.rb', line 80

def callback_task_creation(data)
  data.fetch('errors', {}).present? ? handle_error(data) : succeed(data)
end

#em_request_optionsObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/asana_exception_notifier/request/client.rb', line 29

def em_request_options
  request = setup_em_options(@options).delete(:em_request)
  params = {
    head: (request[:head] || {}).merge(
      'Authorization' => "Bearer #{@api_key}"
    ),
    body: request[:body]
  }
  super(params)
end

#handle_all_responses(http_response) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/asana_exception_notifier/request/client.rb', line 54

def handle_all_responses(http_response)
  @multi_manager.requests.delete(@http) if @multi_manager.present?
  if http_response.is_a?(Hash) && %i(callback errback).all? { |key| http_response.symbolize_keys.keys.include?(key) }
    handle_multi_response(http_response)
  else
    handle_response(http_response, @options.fetch(:request_name, ''))
  end
end

#handle_error(error, key = '') ⇒ Object



69
70
71
72
# File 'lib/asana_exception_notifier/request/client.rb', line 69

def handle_error(error, key = '')
  logger.debug("[AsanaExceptionNotifier]: Task #{key} #{@action} returned:  #{error}")
  fail(error)
end

#handle_multi_response(http_response) ⇒ Object



63
64
65
66
67
# File 'lib/asana_exception_notifier/request/client.rb', line 63

def handle_multi_response(http_response)
  logger.debug('[AsanaExceptionNotifier]: Handling multi responses')
  get_multi_request_values(http_response, :callback).each { |request_name, response| handle_response(response, request_name) }
  get_multi_request_values(http_response, :errback).each { |request_name, response| handle_error(response, request_name) }
end

#handle_response(http_response, key = '') ⇒ Object



74
75
76
77
78
# File 'lib/asana_exception_notifier/request/client.rb', line 74

def handle_response(http_response, key = '')
  logger.debug("[AsanaExceptionNotifier]: Task #{key} #{@action} returned:  #{http_response}")
  data = JSON.parse(http_response)
  callback_task_creation(data)
end

#multi_managerObject



25
26
27
# File 'lib/asana_exception_notifier/request/client.rb', line 25

def multi_manager
  @multi_manager ||= options.fetch(:multi_manager, nil)
end

#send_requestObject



48
49
50
51
52
# File 'lib/asana_exception_notifier/request/client.rb', line 48

def send_request
  fetch_data(@options) do |http_response|
    handle_all_responses(http_response)
  end
end

#send_request_and_rescueObject



40
41
42
43
44
45
46
# File 'lib/asana_exception_notifier/request/client.rb', line 40

def send_request_and_rescue
  @http = em_request(@url, @options)
  send_request
rescue => exception
  log_exception(exception)
  fail(result: { message: exception })
end