Class: SpreeUberDirect::Client
- Inherits:
-
Object
- Object
- SpreeUberDirect::Client
- Defined in:
- app/services/spree_uber_direct/client.rb
Overview
Thin wrapper around the Uber Direct API (api.uber.com/v1/customers/ {customer_id}/...). All Direct API access in this extension goes
through here.
Auth is fundamentally different from SpreeDoordash::Client: Uber Direct
uses OAuth2 client_credentials (auth.uber.com/oauth/v2/token), which
issues a genuine bearer token that outlives a single request — unlike
DoorDash's per-request-signed 5-minute JWT, there's a real token to
cache and refresh here. The cache lives on the Credential row itself
(see SpreeUberDirect::Credential#needs_refresh?) rather than
Rails.cache — no extra infrastructure, and naturally picks up a
rotated client_secret on the very next call since a fresh
Credential.find_by is re-resolved per client instance (not memoized —
same rationale as SpreeSquare::Client and SpreeDoordash::Client: an
admin can edit the credential mid-process).
Defined Under Namespace
Classes: MissingCredentialsError
Constant Summary collapse
- SANDBOX_BASE_URL =
'https://api.uber.com'.freeze
- AUTH_URL =
'https://auth.uber.com/oauth/v2/token'.freeze
- SCOPE =
'eats.deliveries'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #cancel_delivery(delivery_id) ⇒ Object
-
#create_delivery(payload) ⇒ Object
Create Delivery — formally dispatches from a still-open quote_id.
-
#create_quote(payload) ⇒ Object
Create Quote — validates coverage/pricing before formally creating a delivery, same "quote first" recommended flow as DoorDash's Drive API.
- #get_delivery(delivery_id) ⇒ Object
-
#initialize(credential: nil) ⇒ Client
constructor
A new instance of Client.
- #sandbox? ⇒ Boolean
Constructor Details
#initialize(credential: nil) ⇒ Client
Returns a new instance of Client.
34 35 36 37 |
# File 'app/services/spree_uber_direct/client.rb', line 34 def initialize(credential: nil) @credential = credential raise MissingCredentialsError, 'No Uber Direct credential connected and UBER_DIRECT_CLIENT_ID is not set' if client_id.blank? end |
Class Method Details
.for_store(store = Spree::Store.default) ⇒ Object
30 31 32 |
# File 'app/services/spree_uber_direct/client.rb', line 30 def self.for_store(store = Spree::Store.default) new(credential: SpreeUberDirect::Credential.find_by(store: store)) end |
.instance ⇒ Object
26 27 28 |
# File 'app/services/spree_uber_direct/client.rb', line 26 def self.instance for_store end |
Instance Method Details
#cancel_delivery(delivery_id) ⇒ Object
58 59 60 |
# File 'app/services/spree_uber_direct/client.rb', line 58 def cancel_delivery(delivery_id) request(:post, "/v1/customers/#{customer_id}/deliveries/#{delivery_id}/cancel") end |
#create_delivery(payload) ⇒ Object
Create Delivery — formally dispatches from a still-open quote_id.
50 51 52 |
# File 'app/services/spree_uber_direct/client.rb', line 50 def create_delivery(payload) request(:post, "/v1/customers/#{customer_id}/deliveries", payload) end |
#create_quote(payload) ⇒ Object
Create Quote — validates coverage/pricing before formally creating a delivery, same "quote first" recommended flow as DoorDash's Drive API.
45 46 47 |
# File 'app/services/spree_uber_direct/client.rb', line 45 def create_quote(payload) request(:post, "/v1/customers/#{customer_id}/delivery_quotes", payload) end |
#get_delivery(delivery_id) ⇒ Object
54 55 56 |
# File 'app/services/spree_uber_direct/client.rb', line 54 def get_delivery(delivery_id) request(:get, "/v1/customers/#{customer_id}/deliveries/#{delivery_id}") end |
#sandbox? ⇒ Boolean
39 40 41 |
# File 'app/services/spree_uber_direct/client.rb', line 39 def sandbox? @credential ? @credential.sandbox? : ENV.fetch('UBER_DIRECT_ENVIRONMENT', 'sandbox') == 'sandbox' end |