Class: Bouncie::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bouncie/client.rb

Overview

Class that wraps a Faraday connection in order to interact with the Bouncie API

Constant Summary collapse

API_ENDPOINT =
'https://api.bouncie.dev/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash)

    the options to create a ‘Bouncie::Client` with.

  • opts (Hash)

    a customizable set of options



20
21
22
# File 'lib/bouncie/client.rb', line 20

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/bouncie/client.rb', line 11

def options
  @options
end

Instance Method Details

#refresh!Object

rubocop:disable Metrics/AbcSize



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bouncie/client.rb', line 68

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 = JSON.parse(resp.body)
    @headers = headers.merge(Authorization: parsed_resp['access_token'])
    @client = build_client
  end
  resp
end

#trips(imei:, transaction_id: nil, gps_format: 'polyline', starts_after: nil, ends_before: nil) ⇒ Trip

Parameters:

  • imei (String)

    (Required) IMEI for the vehicle to retrieve trips for

  • transaction_id (String) (defaults to: nil)

    Unique Trip Identifier

  • gps_format (String) (defaults to: 'polyline')

    (Required) One of: ‘polyline` or `geojson`

  • starts_after (ISODate) (defaults to: nil)

    Will match trips with a starting time after this parameter. The window between starts-after and ends-before must be no longer than a week. If not provided, the last week will be used by default

  • ends_before (ISODate) (defaults to: nil)

    Will match trips with an ending time before this parameter

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bouncie/client.rb', line 30

def trips(imei:, transaction_id: nil, gps_format: 'polyline', starts_after: nil, ends_before: nil)
  request(
    http_method: :get,
    endpoint: 'trips',
    params: {
      imei: imei,
      transactionId: transaction_id,
      gpsFormat: gps_format,
      startsAfter: starts_after,
      endsBefore: ends_before
    }.compact
  ).map { |data| Bouncie::Trip.new(data) }
end

#userUser

Returns:



59
60
61
62
63
64
65
# File 'lib/bouncie/client.rb', line 59

def user
  data = request(
    http_method: :get,
    endpoint: 'user'
  )
  Bouncie::User.new(data)
end

#vehicles(imei: nil, vin: nil) ⇒ Vehicle

Parameters:

  • vin (String) (defaults to: nil)

    (optional) Vehicles with vin matching given value

  • imei (String) (defaults to: nil)

    (optional) Vehicles with imei matching given value

Returns:



47
48
49
50
51
52
53
54
55
56
# File 'lib/bouncie/client.rb', line 47

def vehicles(imei: nil, vin: nil)
  request(
    http_method: :get,
    endpoint: 'vehicles',
    params: {
      imei: imei,
      vin: vin
    }.compact
  ).map { |data| Bouncie::Vehicle.new(data) }
end