lib/bouncie/client.rb in bouncie-0.4.0 vs lib/bouncie/client.rb in bouncie-0.6.0
- old
+ new
@@ -9,12 +9,15 @@
API_ENDPOINT = 'https://api.bouncie.dev/v1'
attr_reader :options
# @param [Hash] options the options to create a `Bouncie::Client` with.
- # @option opts [String] :api_key your Bouncie app's API key. Retrieve this from https://www.bouncie.dev/apps. Required.
+ # @option opts [String] :client_id your Bouncie app's client_id. Retrieve this from https://www.bouncie.dev/apps. Required.
+ # @option opts [String] :client_id your Bouncie app's client_secret. Retrieve this from https://www.bouncie.dev/apps. Required.
+ # @option opts [String] :redirect_uri the same redirect uri used to retrieve the token initially. Used for verification only. Required.
# @option opts [String] :authorization_code code from a user who grants access to their information via OAuth. Required.
+ # @option opts [String] :access_token token from a user who grants access to their information via OAuth. Required.
# @option opts [Hash] :headers hash of any additional headers to add to HTTP requests
def initialize(options)
@options = options
end
@@ -28,42 +31,68 @@
request(
http_method: :get,
endpoint: 'trips',
params: {
imei: imei,
- transaction_id: transaction_id,
- gps_format: gps_format,
- starts_after: starts_after,
- ends_before: ends_before
+ transactionId: transaction_id,
+ gpsFormat: gps_format,
+ startsAfter: starts_after,
+ endsBefore: ends_before
}.compact
).map { |data| Bouncie::Trip.new(data) }
end
# @param vin [String] (optional) Vehicles with vin matching given value
# @param imei [String] (optional) Vehicles with imei matching given value
# @return [Vehicle]
def vehicles(imei: nil, vin: nil)
request(
http_method: :get,
- endpoint: 'vehicles',
- params: {
+ endpoint: 'vehicles',
+ params: {
imei: imei,
- vin: vin
+ vin: vin
}.compact
).map { |data| Bouncie::Vehicle.new(data) }
end
+ # @return [User]
+ def user
+ data = request(
+ http_method: :get,
+ endpoint: 'user'
+ )
+ Bouncie::User.new(data)
+ end
+
+ def refresh!
+ resp = Faraday.post('https://auth.bouncie.com/oauth/token', {
+ client_id: options[:client_id],
+ client_secret: options[:client_secret],
+ grant_type: 'authorization_code',
+ code: options[:authorization_code],
+ redirect_uri: options[:redirect_uri]
+ })
+ if resp.success?
+ parsed_resp = Oj.load(resp.body)
+ @headers = headers.merge(Authorization: parsed_resp['access_token'])
+ @client = build_client
+ end
+ resp
+ end
+
private
def headers
- @headers ||= {
- 'AuthorizationCode' => options[:authorization_code],
- 'ApiKey' => options[:api_key]
- }.merge(options[:headers] || {})
+ @headers = { Authorization: options[:access_token] }.merge(options[:headers] || {})
end
def client
- @client ||= Faraday.new(API_ENDPOINT, headers: headers) do |client|
+ @client ||= build_client
+ end
+
+ def build_client
+ Faraday.new(API_ENDPOINT, headers: headers) do |client|
client.request :url_encoded
client.adapter Faraday.default_adapter
end
end