Module: Mfkessai::Client
- Included in:
- Customer, Destination, Examination, Ping, Transaction
- Defined in:
- lib/mfkessai/client.rb
Instance Method Summary collapse
- #_uname_uname ⇒ Object
- #_uname_ver ⇒ Object
- #check_api_key!(api_key) ⇒ Object
- #from_faraday_response(http_resp) ⇒ Object
- #general_api_error(status:, body:) ⇒ Object
- #handle_error_response(error_response:) ⇒ Object
- #handle_network_error(e) ⇒ Object
- #list_filter_parameters(page, per_page) ⇒ Object
- #request(url:, request_type:, request_body: nil) ⇒ Object
- #request_headers ⇒ Object
- #uname ⇒ Object
- #user_agent ⇒ Object
Instance Method Details
#_uname_uname ⇒ Object
118 119 120 121 122 |
# File 'lib/mfkessai/client.rb', line 118 def _uname_uname (`uname -a 2>/dev/null` || '').strip rescue Errno::ENOMEM # couldn't create subprocess "uname lookup failed" end |
#_uname_ver ⇒ Object
124 125 126 127 128 |
# File 'lib/mfkessai/client.rb', line 124 def _uname_ver (`ver` || '').strip rescue Errno::ENOMEM # couldn't create subprocess "uname lookup failed" end |
#check_api_key!(api_key) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/mfkessai/client.rb', line 200 def check_api_key!(api_key) unless api_key = "No API key provided. " \ 'Set your API key using "Mfkessai.api_key = <API-KEY>". ' \ "You can generate API keys from the Mfkessai web interface. " raise AuthenticationError.new(message: ) end return unless api_key =~ /\s/ = "Your API key is invalid, as it contains " \ "whitespace. (HINT: You can double-check your API key from the " raise AuthenticationError.new(message: ) end |
#from_faraday_response(http_resp) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mfkessai/client.rb', line 41 def from_faraday_response(http_resp) response = Struct.new(:data, :http_body, :http_headers, :http_status) resp = response.new # response bodyがokの場合、v1/pingからのリクエストなので処理を分ける if http_resp.body == 'ok' resp.http_body = http_resp.body resp.http_headers = http_resp.headers resp.http_status = http_resp.status else resp.data = JSON.parse(http_resp.body, symbolize_names: true) resp.http_body = http_resp.body resp.http_headers = http_resp.headers resp.http_status = http_resp.status end resp end |
#general_api_error(status:, body:) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/mfkessai/client.rb', line 193 def general_api_error(status:, body:) = "Invalid response object from API: #{body.inspect} \n HTTP response code was #{status})" APIConnectionError.new(message: , http_status: status, http_body: body) end |
#handle_error_response(error_response:) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mfkessai/client.rb', line 130 def handle_error_response(error_response:) case error_response[:status] when 400 data = JSON.parse(error_response[:body], symbolize_names: true) raise BadRequestError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) when 401 data = JSON.parse(error_response[:body], symbolize_names: true) raise AuthenticationError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) when 403 data = JSON.parse(error_response[:body], symbolize_names: true) raise ForbiddenError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) when 404 data = JSON.parse(error_response[:body], symbolize_names: true) raise NotFoundError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) when 500 data = JSON.parse(error_response[:body], symbolize_names: true) raise ServerError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) when 503 data = JSON.parse(error_response[:body], symbolize_names: true) raise ServerError.new(message: data[:detail], http_status: error_response[:status], http_body: error_response[:body], json_body: data) else raise ServerError.new(http_status: 500) end end |
#handle_network_error(e) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/mfkessai/client.rb', line 173 def handle_network_error(e) case e when Faraday::ConnectionFailed = "Unexpected error communicating when trying to connect to Mfkessai. " \ "You may be seeing this message because your DNS is not working. " when Faraday::SSLError = "Could not establish a secure connection to Mfkessai, you may " \ "need to upgrade your OpenSSL version. To check, try running " when Faraday::TimeoutError = "Could not connect to Mfkessai (#{Mfkessai.api_key}). " \ "Please check your internet connection and try again. " else = "Unexpected error communicating with Mfkessai. " end += " Request was retried" raise APIConnectionError.new(message: + "\n\n(Network error: #{e.})") end |
#list_filter_parameters(page, per_page) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/mfkessai/client.rb', line 59 def list_filter_parameters(page, per_page) params = {} params[:page] = page if page params[:per_page] = per_page if per_page '?' + URI.encode_www_form(params) end |
#request(url:, request_type:, request_body: nil) ⇒ Object
4 5 6 7 8 9 10 11 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 |
# File 'lib/mfkessai/client.rb', line 4 def request(url:, request_type:, request_body: nil) api_url = Mfkessai.api_url api_key = Mfkessai.api_key check_api_key!(api_key) url = "#{api_url}#{url}" uri = URI.parse(url) conn = Faraday.new(:ssl => {:verify => false}) do |c| c.use Faraday::Request::Multipart c.use Faraday::Request::UrlEncoded c.use Faraday::Response::RaiseError c.adapter Faraday.default_adapter end payload = request_body.nil? ? request_body : request_body.to_json http_resp = conn.run_request(request_type, uri, payload, request_headers) do |req| req..open_timeout = Mfkessai.open_timeout req..timeout = Mfkessai.read_timeout end from_faraday_response(http_resp) rescue JSON::ParserError => e raise general_api_error(status: http_resp.status, body: http_resp.body) rescue Faraday::ClientError => e if e.response handle_error_response(error_response: e.response) else handle_network_error(e) end rescue AuthenticationError => e raise AuthenticationError.new(http_status: 401, message: e.) rescue StandardError => e raise ServerError.new(http_status: 500) end |
#request_headers ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mfkessai/client.rb', line 66 def request_headers headers = { "User-Agent" => "Mfkessai/v1 RubyBindings/#{Mfkessai::VERSION}", "apiKey" => Mfkessai.api_key, "Content-Type" => "application/json", } headers["Mfkessai-Version"] = Mfkessai.api_version if Mfkessai.api_version begin headers.update( "X-Mfkessai-Client-User-Agent" => JSON.generate(user_agent) ) rescue StandardError => e headers.update( "X-Mfkessai-Client-Raw-User-Agent" => user_agent.inspect, :error => "#{e} (#{e.class})" ) end headers end |
#uname ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mfkessai/client.rb', line 103 def uname if File.exist?('/proc/version') File.read('/proc/version').strip else case RbConfig::CONFIG['host_os'] when /linux|darwin|bsd|sunos|solaris|cygwin/i _uname_uname when /mswin|mingw/i _uname_ver else "unknown platform" end end end |
#user_agent ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mfkessai/client.rb', line 88 def user_agent @uname ||= uname lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})" { :bindings_version => Mfkessai::VERSION, :lang => 'ruby', :lang_version => lang_version, :platform => RUBY_PLATFORM, :engine => defined?(RUBY_ENGINE) ? RUBY_ENGINE : '', :uname => @uname, :hostname => Socket.gethostname } end |