Class: Courier::Users::Tokens::UserToken

Inherits:
Object
  • Object
show all
Defined in:
lib/trycourier/users/tokens/types/user_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_key:, token: nil, expiry_date: nil, properties: nil, device: nil, tracking: nil, additional_properties: nil) ⇒ Users::Tokens::UserToken

Parameters:

  • token (String) (defaults to: nil)

    Full body of the token. Must match token in URL.

  • provider_key (Users::Tokens::ProviderKey)
  • expiry_date (Users::Tokens::ExpiryDate) (defaults to: nil)

    ISO 8601 formatted date the token expires. Defaults to 2 months. Set to false to disable expiration.

  • properties (Object) (defaults to: nil)

    Properties sent to the provider along with the token

  • device (Users::Tokens::Device) (defaults to: nil)

    Information about the device the token is associated with.

  • tracking (Users::Tokens::Tracking) (defaults to: nil)

    Information about the device the token is associated with.

  • additional_properties (OpenStruct) (defaults to: nil)

    Additional properties unmapped to the current class definition


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 23

def initialize(provider_key:, token: nil, expiry_date: nil, properties: nil, device: nil, tracking: nil,
               additional_properties: nil)
  # @type [String] Full body of the token. Must match token in URL.
  @token = token
  # @type [Users::Tokens::ProviderKey]
  @provider_key = provider_key
  # @type [Users::Tokens::ExpiryDate] ISO 8601 formatted date the token expires. Defaults to 2 months. Set to false to disable expiration.
  @expiry_date = expiry_date
  # @type [Object] Properties sent to the provider along with the token
  @properties = properties
  # @type [Users::Tokens::Device] Information about the device the token is associated with.
  @device = device
  # @type [Users::Tokens::Tracking] Information about the device the token is associated with.
  @tracking = tracking
  # @type [OpenStruct] Additional properties unmapped to the current class definition
  @additional_properties = additional_properties
end

Instance Attribute Details

#additional_propertiesObject (readonly)

Returns the value of attribute additional_properties.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def additional_properties
  @additional_properties
end

#deviceObject (readonly)

Returns the value of attribute device.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def device
  @device
end

#expiry_dateObject (readonly)

Returns the value of attribute expiry_date.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def expiry_date
  @expiry_date
end

#propertiesObject (readonly)

Returns the value of attribute properties.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def properties
  @properties
end

#provider_keyObject (readonly)

Returns the value of attribute provider_key.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def provider_key
  @provider_key
end

#tokenObject (readonly)

Returns the value of attribute token.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def token
  @token
end

#trackingObject (readonly)

Returns the value of attribute tracking.


13
14
15
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 13

def tracking
  @tracking
end

Class Method Details

.from_json(json_object:) ⇒ Users::Tokens::UserToken

Deserialize a JSON object to an instance of UserToken

Parameters:

  • json_object (JSON)

Returns:


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 45

def self.from_json(json_object:)
  struct = JSON.parse(json_object, object_class: OpenStruct)
  parsed_json = JSON.parse(json_object)
  token = struct.token
  provider_key = struct.provider_key
  if parsed_json["expiry_date"].nil?
    expiry_date = nil
  else
    expiry_date = parsed_json["expiry_date"].to_json
    expiry_date = Users::Tokens::ExpiryDate.from_json(json_object: expiry_date)
  end
  properties = struct.properties
  if parsed_json["device"].nil?
    device = nil
  else
    device = parsed_json["device"].to_json
    device = Users::Tokens::Device.from_json(json_object: device)
  end
  if parsed_json["tracking"].nil?
    tracking = nil
  else
    tracking = parsed_json["tracking"].to_json
    tracking = Users::Tokens::Tracking.from_json(json_object: tracking)
  end
  new(token: token, provider_key: provider_key, expiry_date: expiry_date, properties: properties,
      device: device, tracking: tracking, additional_properties: struct)
end

.validate_raw(obj:) ⇒ Void

Leveraged for Union-type generation, validate_raw attempts to parse the given hash and check each fields type against the current object’s property definitions.

Parameters:

  • obj (Object)

Returns:

  • (Void)

91
92
93
94
95
96
97
98
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 91

def self.validate_raw(obj:)
  obj.token&.is_a?(String) != false || raise("Passed value for field obj.token is not the expected type, validation failed.")
  obj.provider_key.is_a?(Users::Tokens::ProviderKey) != false || raise("Passed value for field obj.provider_key is not the expected type, validation failed.")
  obj.expiry_date.nil? || Users::Tokens::ExpiryDate.validate_raw(obj: obj.expiry_date)
  obj.properties&.is_a?(Object) != false || raise("Passed value for field obj.properties is not the expected type, validation failed.")
  obj.device.nil? || Users::Tokens::Device.validate_raw(obj: obj.device)
  obj.tracking.nil? || Users::Tokens::Tracking.validate_raw(obj: obj.tracking)
end

Instance Method Details

#to_json(*_args) ⇒ JSON

Serialize an instance of UserToken to a JSON object

Returns:

  • (JSON)

76
77
78
79
80
81
82
83
84
85
# File 'lib/trycourier/users/tokens/types/user_token.rb', line 76

def to_json(*_args)
  {
    "token": @token,
    "provider_key": @provider_key,
    "expiry_date": @expiry_date,
    "properties": @properties,
    "device": @device,
    "tracking": @tracking
  }.to_json
end