Module: BlommingApi::PrivateHelpers
- Included in:
- Client
- Defined in:
- lib/blomming_api/private_helpers.rb
Instance Method Summary collapse
-
#api_url(api_endpoint_uri) ⇒ Object
build complete URL of any API request endpoint URI.
-
#authenticate_refresh(e) ⇒ Object
Errors managers.
- #fatal_error!(e) ⇒ Object
-
#feed_or_retry(&restclient_call_block) ⇒ Object
feed_or_retry:.
-
#http_status_code_401?(e) ⇒ Boolean
Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method After your bearer token has expired, each request done with that stale token will return an HTTP code 401.
-
#http_status_code_4xx?(e) ⇒ Boolean
404: not found 422: Invalid or blank request body given (sell services endpoints).
-
#http_status_code_5xx?(e) ⇒ Boolean
possible temporary server problem ?.
-
#request_params(params = {}) ⇒ Object
return the hash to be used as HTTP header in all API requests, embedding authentication token and all optional parameters.
- #server_error!(e) ⇒ Object
- #socket_error!(e) ⇒ Object
- #unknown_error!(e) ⇒ Object
Instance Method Details
#api_url(api_endpoint_uri) ⇒ Object
build complete URL of any API request endpoint URI
9 10 11 |
# File 'lib/blomming_api/private_helpers.rb', line 9 def api_url (api_endpoint_uri) "#{@domain}#{@api_version}#{api_endpoint_uri}" end |
#authenticate_refresh(e) ⇒ Object
Errors managers
111 112 113 114 115 116 117 118 119 |
# File 'lib/blomming_api/private_helpers.rb', line 111 def authenticate_refresh(e) STDERR.puts "#{Time.now}: HTTP status code: #{e.response.code}: #{e.response.body}. Invalid or expired token. Retry in #@retry_seconds seconds." # sleep maybe useless here sleep @retry_seconds # authenticate with refresh token authenticate :refresh end |
#fatal_error!(e) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/blomming_api/private_helpers.rb', line 134 def fatal_error!(e) STDERR.puts "#{Time.now}: restclient error. http status code: #{e.response.code}: #{e.response.body}." # # survive_on_fatal_error initialized in config file # unless @survive_on_fatal_error # Process die! exit else # no data! return nil end end |
#feed_or_retry(&restclient_call_block) ⇒ Object
feed_or_retry:
-
call RestClient verb, passed in a block (feed_or_retry is an iterator)
-
without any exception (http errors): return data (convert response data from JSON to Ruby Hash)
-
in case of exceptions: manage connection/RestClient exceptions (by example retry the API call in case of authentication token expired).
In case of fatal error:
if @survive_on_fatal_error if false (default) process die with exit if @survive_on_fatal_error if true (must be specified in config file) feed_or_retry return value: nil (no data available!).
arguments
&restclient_call_block
the block to be passed (containing call to RestCient method)
examples
feed_or_retry { RestClient.get url, req }
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 |
# File 'lib/blomming_api/private_helpers.rb', line 49 def feed_or_retry (&restclient_call_block) begin # call RestClient verb json_data = restclient_call_block.call # IP connection error, wrong url rescue SocketError => e socket_error! e retry # RestClient exceptions, manage HTTP status code rescue RestClient::Exception => e if http_status_code_401? e authenticate_refresh e retry elsif http_status_code_4xx? e return fatal_error! e elsif http_status_code_5xx? e server_error! e retry # any other RestClient exception else return fatal_error! e end rescue => e return unknown_error! e else # HTTP status 200: return data (hash from JSON)! MultiJson.load json_data end end |
#http_status_code_401?(e) ⇒ Boolean
Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method After your bearer token has expired, each request done with that stale token will return an HTTP code 401
93 94 95 |
# File 'lib/blomming_api/private_helpers.rb', line 93 def http_status_code_401?(e) 401 == e.response.code end |
#http_status_code_4xx?(e) ⇒ Boolean
404: not found 422: Invalid or blank request body given (sell services endpoints)
99 100 101 |
# File 'lib/blomming_api/private_helpers.rb', line 99 def http_status_code_4xx?(e) [400, 404, 422].include? e.response.code end |
#http_status_code_5xx?(e) ⇒ Boolean
possible temporary server problem ?
104 105 106 |
# File 'lib/blomming_api/private_helpers.rb', line 104 def http_status_code_5xx?(e) [500, 520].include? e.response.code end |
#request_params(params = {}) ⇒ Object
return the hash to be used as HTTP header in all API requests, embedding authentication token and all optional parameters
17 18 19 |
# File 'lib/blomming_api/private_helpers.rb', line 17 def request_params(params={}) { authorization: "Bearer #{@access_token}", params: params } end |
#server_error!(e) ⇒ Object
121 122 123 124 125 |
# File 'lib/blomming_api/private_helpers.rb', line 121 def server_error!(e) STDERR.puts "#{Time.now}: HTTP status code: #{e.response.code}: #{e.response.body}. Retry in #@retry_seconds seconds." sleep @retry_seconds end |
#socket_error!(e) ⇒ Object
127 128 129 130 131 |
# File 'lib/blomming_api/private_helpers.rb', line 127 def socket_error!(e) STDERR.puts "#{Time.now}: socket error: #{e}. Possible net connection problems. Retry in #@retry_seconds seconds." sleep @retry_seconds end |
#unknown_error!(e) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/blomming_api/private_helpers.rb', line 150 def unknown_error!(e) STDERR.puts "#{Time.now}: error: #{e.class}: #{e.message}" # # survive_on_fatal_error initialized in config file # unless @survive_on_fatal_error # Process die! exit else # no data! return nil end end |