Module: Diffend::Voting::Request
- Defined in:
- lib/diffend/voting/request.rb
Overview
Module responsible for doing request to Diffend
Constant Summary collapse
- CONNECTION_EXCEPTIONS =
List of connection exceptions
[ Errno::ECONNRESET, Errno::ENETUNREACH, Errno::EHOSTUNREACH, Errno::ECONNREFUSED ].freeze
- TIMEOUT_EXCEPTIONS =
List of timeout exceptions
[ Net::OpenTimeout, Net::ReadTimeout ].freeze
Class Method Summary collapse
-
.assign_auth(request, config) ⇒ Object
Assigns basic authorization if provided in the config.
-
.assign_payload(request, payload) ⇒ Object
Assigns payload as json.
-
.build_http(command, project_id) ⇒ Object
Builds http connection object.
-
.build_request(uri, config, payload) ⇒ Net::HTTP::Post
Build http post request and assigns headers and payload.
- .build_request_error_message ⇒ Object
- .build_unhandled_exception_message ⇒ Object
-
.call(command, payload, config) ⇒ Net::HTTPResponse
Execute request to Diffend.
-
.endpoint_url(command, project_id) ⇒ String
Provides diffend endpoint url.
- .exponential_backoff(retry_count) ⇒ Object
- .prepare_report(payload, exception) ⇒ Object
Class Method Details
.assign_auth(request, config) ⇒ Object
Assigns basic authorization if provided in the config
109 110 111 112 113 114 115 |
# File 'lib/diffend/voting/request.rb', line 109 def assign_auth(request, config) return unless config return unless config.shareable_id return unless config.shareable_key request.basic_auth(config.shareable_id, config.shareable_key) end |
.assign_payload(request, payload) ⇒ Object
Assigns payload as json
121 122 123 |
# File 'lib/diffend/voting/request.rb', line 121 def assign_payload(request, payload) request.body = JSON.dump(payload: payload) end |
.build_http(command, project_id) ⇒ Object
Builds http connection object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/diffend/voting/request.rb', line 78 def build_http(command, project_id) uri = URI(endpoint_url(command, project_id)) Net::HTTP.start( uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE, open_timeout: 5, read_timeout: 5 ) { |http| yield(http, uri) } end |
.build_request(uri, config, payload) ⇒ Net::HTTP::Post
Build http post request and assigns headers and payload
98 99 100 101 102 103 |
# File 'lib/diffend/voting/request.rb', line 98 def build_request(uri, config, payload) Net::HTTP::Post .new(uri.request_uri, HEADERS) .tap { |request| assign_auth(request, config) } .tap { |request| assign_payload(request, payload) } end |
.build_request_error_message ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/diffend/voting/request.rb', line 141 def <<~MSG \nWe were unable to process your request at this time. We recorded this incident in our system and will review it.\n If you think that this is a bug, don't hesitate.\n Create an issue on https://github.com/diffend-io/diffend-ruby/issues\n MSG end |
.build_unhandled_exception_message ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/diffend/voting/request.rb', line 149 def <<~MSG \nSomething went really wrong. We recorded this incident in our system and will review it.\n This is a bug, don't hesitate.\n Create an issue on https://github.com/diffend-io/diffend-ruby/issues\n MSG end |
.call(command, payload, config) ⇒ Net::HTTPResponse
Execute request to Diffend
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 |
# File 'lib/diffend/voting/request.rb', line 37 def call(command, payload, config) retry_count ||= 0 build_http(command, config.project_id) do |http, uri| http.request(build_request(uri, config, payload)) end rescue *CONNECTION_EXCEPTIONS => e Bundler.ui.error('We experienced a connection issue, retrying...') sleep(exponential_backoff(retry_count)) retry_count += 1 retry if retry_count < 3 puts prepare_report(payload, e) Bundler.ui.error('^^^ Above is the dump of your request ^^^') Bundler.ui.error() exit 1 rescue *TIMEOUT_EXCEPTIONS => e Bundler.ui.error('We experienced a timeout issue, retrying...') sleep(exponential_backoff(retry_count)) retry_count += 1 retry if retry_count < 3 puts prepare_report(payload, e) Bundler.ui.error('^^^ Above is the dump of your request ^^^') Bundler.ui.error() exit 1 rescue StandardError => e puts prepare_report(payload, e) Bundler.ui.error('^^^ Above is the dump of your request ^^^') Bundler.ui.error() exit 1 end |
.endpoint_url(command, project_id) ⇒ String
Provides diffend endpoint url
131 132 133 134 135 |
# File 'lib/diffend/voting/request.rb', line 131 def endpoint_url(command, project_id) return ENV['DIFFEND_URL'] if ENV.key?('DIFFEND_URL') "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}" end |
.exponential_backoff(retry_count) ⇒ Object
137 138 139 |
# File 'lib/diffend/voting/request.rb', line 137 def exponential_backoff(retry_count) 2**(retry_count + 1) end |
.prepare_report(payload, exception) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/diffend/voting/request.rb', line 157 def prepare_report(payload, exception) { request_id: SecureRandom.uuid, payload: payload, exception: { class: exception.class, message: exception., backtrace: exception.backtrace } }.to_json end |