Class: CivicSIPSdk::Client

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

Constant Summary collapse

BASE_URL =
'https://api.civic.com/sip'
AUTH_CODE_PATH =
'scopeRequest/authCode'
PUBLIC_HEX =
'049a45998638cfb3c4b211d72030d9ae8329a242db63bfb0076a54e7647370a8ac5708b57af6065805d5a6be72332620932dbb35e8d318fce18e7c980a0eb26aa1'
MIMETYPE_JSON =
'application/json'
ENV_VAR =
'CIVIC_SIP_SDK_ENV'
TEST_ENV =
'test'
HTTP_REQUEST_METHOD =
'POST'

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Client

Creates a client

Parameters:



24
25
26
27
# File 'lib/civic_sip_sdk/client.rb', line 24

def initialize(config:)
  @config = config
  @test_env = ENV[ENV_VAR] == TEST_ENV
end

Instance Method Details

#exchange_code(jwt_token:) ⇒ CivicSIPSdk::UserData

Exchange authorization code in the form of a JWT Token for the user data requested in the scope request.

Parameters:

  • jwt_token (String)

    a JWT token that contains the authorization code

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/civic_sip_sdk/client.rb', line 34

def exchange_code(jwt_token:)
  json_body_str = JSON.generate('authToken' => jwt_token)

  response = HTTParty.post(
    "#{BASE_URL}/#{@config.env}/#{AUTH_CODE_PATH}",
    headers: {
      'Content-Type' => MIMETYPE_JSON,
      'Accept' => MIMETYPE_JSON,
      'Content-Length' => json_body_str.size.to_s,
      'Authorization' => authorization_header(body: json_body_str)
    },
    body: json_body_str
  )

  unless response.code == 200
    raise StandardError.new(
      "Failed to exchange JWT token. HTTP status: #{response.code}, response body: #{response.body}"
    )
  end

  res_payload = JSON.parse(response.body)
  extract_user_data(response: res_payload)
end