Module: Mapbox
- Defined in:
- lib/mapbox.rb,
lib/mapbox/matrix.rb,
lib/mapbox/geocoder.rb,
lib/mapbox/isochrone.rb,
lib/mapbox/tilequery.rb,
lib/mapbox/directions.rb,
lib/mapbox/map_matching.rb,
lib/mapbox/optimization.rb,
lib/mapbox/api_operations.rb,
lib/mapbox/authentication_error.rb
Defined Under Namespace
Modules: APIOperations, HashUtils Classes: AuthenticationError, Directions, Geocoder, Isochrone, MapMatching, MapboxError, Matrix, Optimization, Tilequery
Constant Summary collapse
- LATITUDE_KEY =
'latitude'.freeze
- LONGITUDE_KEY =
'longitude'.freeze
Class Attribute Summary collapse
-
.access_token ⇒ Object
Returns the value of attribute access_token.
-
.api_base ⇒ Object
Returns the value of attribute api_base.
-
.request_open_timeout ⇒ Object
Returns the value of attribute request_open_timeout.
-
.request_opts ⇒ Object
Returns the value of attribute request_opts.
-
.request_timeout ⇒ Object
Returns the value of attribute request_timeout.
Class Method Summary collapse
- .api_error(error, rcode, rbody, error_obj) ⇒ Object
- .authentication_error(error, rcode, rbody, error_obj) ⇒ Object
- .execute_request(opts) ⇒ Object
- .general_api_error(rcode, rbody) ⇒ Object
- .handle_api_error(rcode, rbody) ⇒ Object
- .handle_restclient_error(e, api_base_url = nil) ⇒ Object
- .invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
- .parse(response) ⇒ Object
- .request(method, url, api_key, params = {}, headers = {}, api_base_url = nil) ⇒ Object
- .uri_encode(params) ⇒ Object
- .url_encode(key) ⇒ Object
Class Attribute Details
.access_token ⇒ Object
Returns the value of attribute access_token.
14 15 16 |
# File 'lib/mapbox.rb', line 14 def access_token @access_token end |
.api_base ⇒ Object
Returns the value of attribute api_base.
14 15 16 |
# File 'lib/mapbox.rb', line 14 def api_base @api_base end |
.request_open_timeout ⇒ Object
Returns the value of attribute request_open_timeout.
14 15 16 |
# File 'lib/mapbox.rb', line 14 def request_open_timeout @request_open_timeout end |
.request_opts ⇒ Object
Returns the value of attribute request_opts.
14 15 16 |
# File 'lib/mapbox.rb', line 14 def request_opts @request_opts end |
.request_timeout ⇒ Object
Returns the value of attribute request_timeout.
14 15 16 |
# File 'lib/mapbox.rb', line 14 def request_timeout @request_timeout end |
Class Method Details
.api_error(error, rcode, rbody, error_obj) ⇒ Object
134 135 136 |
# File 'lib/mapbox.rb', line 134 def self.api_error(error, rcode, rbody, error_obj) StandardError.new(error[:message], rcode, rbody, error_obj) end |
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
130 131 132 |
# File 'lib/mapbox.rb', line 130 def self.authentication_error(error, rcode, rbody, error_obj) AuthenticationError.new(error[:message], rcode, rbody, error_obj) end |
.execute_request(opts) ⇒ Object
84 85 86 |
# File 'lib/mapbox.rb', line 84 def self.execute_request(opts) RestClient::Request.execute(opts) end |
.general_api_error(rcode, rbody) ⇒ Object
100 101 102 103 |
# File 'lib/mapbox.rb', line 100 def self.general_api_error(rcode, rbody) StandardError.new("Invalid response object from API: #{rbody.inspect} " + "(HTTP response code was #{rcode})") end |
.handle_api_error(rcode, rbody) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mapbox.rb', line 105 def self.handle_api_error(rcode, rbody) begin error_obj = JSON.parse(rbody) error = error_obj[:error] or raise StandardError.new # escape from parsing rescue JSON::ParserError, StandardError raise general_api_error(rcode, rbody) end case rcode when 400, 404 raise invalid_request_error error, rcode, rbody, error_obj when 401 raise authentication_error error, rcode, rbody, error_obj else raise api_error error, rcode, rbody, error_obj end end |
.handle_restclient_error(e, api_base_url = nil) ⇒ Object
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 |
# File 'lib/mapbox.rb', line 138 def self.handle_restclient_error(e, api_base_url=nil) api_base_url = @api_base unless api_base_url = "Please check your internet connection and try again. " \ "If this problem persists, you should check Mapbox's service status at " \ "https://status.mapbox.com/, or let us know at [email protected]." case e when RestClient::RequestTimeout = "Could not connect to Mapbox (#{api_base_url}). #{}" when RestClient::ServerBrokeConnection = "The connection to the server (#{api_base_url}) broke before the " \ "request completed. #{}" when RestClient::SSLCertificateNotVerified = "Could not verify Mapbox's SSL certificate. " \ "Please make sure that your network is not intercepting certificates. " \ "(Try going to https://api.mapbox.com/ in your browser.) " \ "If this problem persists, let us know at [email protected]." when SocketError = "Unexpected error communicating when trying to connect to Mapbox. " \ "You may be seeing this message because your DNS is not working. " \ "To check, try running 'host mapbox.com' from the command line." else = "Unexpected error communicating with Mapbox. " \ "If this problem persists, let us know at [email protected]." end raise StandardError.new( + "\n\n(Network error: #{e.})") end |
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
125 126 127 128 |
# File 'lib/mapbox.rb', line 125 def self.invalid_request_error(error, rcode, rbody, error_obj) StandardError.new(error[:message], error[:param], rcode, rbody, error_obj) end |
.parse(response) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mapbox.rb', line 88 def self.parse(response) begin # Would use :symbolize_names => true, but apparently there is # some library out there that makes symbolize_names not work. response = JSON.parse(response.body) rescue JSON::ParserError raise general_api_error(response.code, response.body) end response end |
.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil) ⇒ Object
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 |
# File 'lib/mapbox.rb', line 17 def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil) api_base_url = api_base_url || @api_base params = params || {} unless access_token ||= @access_token raise AuthenticationError.new('No API key provided. ' \ 'Set your API key using "Mapbox.access_token = <access_token>". ' \ 'You can generate API keys from the Mapbox web interface. ' \ 'See https://mapbox.com/developers for details, or email [email protected] ' \ 'if you have any questions.') end url = api_base_url + url params['access_token'] = access_token case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters url += "#{URI.parse(url).query ? '&' : '?'}#{URI.encode_www_form(params)}" if params && params.any? payload = nil end @request_opts = {:verify_ssl => OpenSSL::SSL::VERIFY_PEER, :ssl_ca_file => @ssl_bundle_path} @request_opts.update( :method => method, :open_timeout => @request_open_timeout || 30, :payload => payload, :url => url, :timeout => @request_timeout || 80) begin response = execute_request(@request_opts) rescue SocketError => e handle_restclient_error(e, api_base_url) rescue NoMethodError => e # Work around RestClient bug if e. =~ /\WRequestFailed\W/ e = StandardError.new('Unexpected HTTP response code') handle_restclient_error(e, api_base_url) else raise end rescue RestClient::ExceptionWithResponse => e if e.response handle_api_error(e, e.response) else handle_restclient_error(e, api_base_url) end rescue RestClient::Exception, Errno::ECONNREFUSED => e handle_restclient_error(e, api_base_url) end [parse(response), api_key] end |
.uri_encode(params) ⇒ Object
75 76 77 78 |
# File 'lib/mapbox.rb', line 75 def self.uri_encode(params) params. map { |k,v| "#{k}=#{url_encode(v)}" }.join('&') end |
.url_encode(key) ⇒ Object
80 81 82 |
# File 'lib/mapbox.rb', line 80 def self.url_encode(key) URI.encode_www_form_component(key.to_s) end |