Module: SpreeCmCommissioner::Integrations::BookMeBusV1::ExternalClient::Connection

Included in:
Client
Defined in:
app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb

Constant Summary collapse

OAUTH_TOKEN_PATH =
'/oauth/token'.freeze
REQUEST_TIMEOUT =
30
OPEN_TIMEOUT =
10

Instance Method Summary collapse

Instance Method Details

#access_tokenObject

Thread-safe token access with automatic refresh on expiration



38
39
40
41
42
43
44
45
46
47
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 38

def access_token
  token_mutex.synchronize do
    @access_token ||= @integration.access_token
    @token_expires_at ||= @integration.token_expires_at

    fetch_and_persist_access_token! if token_expired?

    @access_token
  end
end

#connectionObject



27
28
29
30
31
32
33
34
35
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 27

def connection
  @connection ||= Faraday.new(url: @integration.base_url) do |faraday|
    faraday.request :json
    faraday.response :json, content_type: /\bjson$/
    faraday.options.timeout = REQUEST_TIMEOUT
    faraday.options.open_timeout = OPEN_TIMEOUT
    faraday.adapter Faraday.default_adapter
  end
end

#delete(url, params = nil, headers = nil) ⇒ Object



23
24
25
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 23

def delete(url, params = nil, headers = nil)
  execute_request { connection.delete(url, params, build_headers(headers)) }
end

#get(url, params = nil, headers = nil) ⇒ Object



7
8
9
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 7

def get(url, params = nil, headers = nil)
  execute_request { connection.get(url, params, build_headers(headers)) }
end

#patch(url, body = nil, headers = nil) ⇒ Object



19
20
21
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 19

def patch(url, body = nil, headers = nil)
  execute_request { connection.patch(url, body, build_headers(headers)) }
end

#post(url, body = nil, headers = nil) ⇒ Object



11
12
13
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 11

def post(url, body = nil, headers = nil)
  execute_request { connection.post(url, body, build_headers(headers)) }
end

#put(url, body = nil, headers = nil) ⇒ Object



15
16
17
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/connection.rb', line 15

def put(url, body = nil, headers = nil)
  execute_request { connection.put(url, body, build_headers(headers)) }
end