Class: Sendly::NumberPurchase

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/numbers_resource.rb

Overview

The result of a buy request. The API responds 202 with one of three statuses:

  • “provisioning”: the purchase succeeded and the number is being provisioned. number carries the new PhoneNumber.

  • “documents_required” / “payment_required”: the purchase is paused pending a hosted Sendly step. action carries the hand-off object, which holds TWO distinct identifiers:

    • actionCode — a 32-hex action identifier (read via #action_identifier). Use THIS to poll the action and to re-call buy (pass it as the action_code: argument).

    • code — a short user code (read via #action_code). DISPLAY ONLY: show it to the human to type on the hosted page to prove terminal access. Never pass it back as action_code:.

    Hand the user action_url + #action_code, wait for completion, then re-call buy with the same body plus action_code: set to #action_identifier. requirements describes what’s missing (a JSON array).

The raw parsed response is preserved verbatim on #raw so callers can read any field the server adds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ NumberPurchase

Returns a new instance of NumberPurchase.



90
91
92
93
94
95
96
# File 'lib/sendly/numbers_resource.rb', line 90

def initialize(data)
  @raw = data
  @status = data["status"]
  @number = data["number"] ? PhoneNumber.new(data["number"]) : nil
  @requirements = data["requirements"]
  @action = data["action"]
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



88
89
90
# File 'lib/sendly/numbers_resource.rb', line 88

def action
  @action
end

#numberObject (readonly)

Returns the value of attribute number.



88
89
90
# File 'lib/sendly/numbers_resource.rb', line 88

def number
  @number
end

#rawObject (readonly)

Returns the value of attribute raw.



88
89
90
# File 'lib/sendly/numbers_resource.rb', line 88

def raw
  @raw
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



88
89
90
# File 'lib/sendly/numbers_resource.rb', line 88

def requirements
  @requirements
end

#statusObject (readonly)

Returns the value of attribute status.



88
89
90
# File 'lib/sendly/numbers_resource.rb', line 88

def status
  @status
end

Instance Method Details

#action_codeString?

The short user code shown to the human to type on the hosted page to prove terminal access. DISPLAY ONLY — to re-buy/poll, use #action_identifier, not this.

Returns:

  • (String, nil)


129
130
131
# File 'lib/sendly/numbers_resource.rb', line 129

def action_code
  action && (action["code"] || action[:code])
end

#action_expires_atInteger, ...

Expiry of the action, as an epoch-milliseconds number (the server sends a number, not an ISO-8601 string). Older payloads may carry it under expires_at; both are accepted.

Returns:

  • (Integer, String, nil)


138
139
140
# File 'lib/sendly/numbers_resource.rb', line 138

def action_expires_at
  action && (action["expiresAt"] || action["expires_at"] || action[:expiresAt] || action[:expires_at])
end

#action_identifierString?

The 32-hex action identifier. Use THIS to poll the action’s status and to re-call buy (pass it as the action_code: argument). NOT for display.

Returns:

  • (String, nil)


120
121
122
# File 'lib/sendly/numbers_resource.rb', line 120

def action_identifier
  action && (action["actionCode"] || action["action_code"] || action[:actionCode] || action[:action_code])
end

#action_urlString?

Returns The hosted Sendly page URL the user must visit.

Returns:

  • (String, nil)

    The hosted Sendly page URL the user must visit.



111
112
113
# File 'lib/sendly/numbers_resource.rb', line 111

def action_url
  action && (action["url"] || action[:url])
end

#documents_required?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/sendly/numbers_resource.rb', line 102

def documents_required?
  status == "documents_required"
end

#payment_required?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/sendly/numbers_resource.rb', line 106

def payment_required?
  status == "payment_required"
end

#provisioning?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/sendly/numbers_resource.rb', line 98

def provisioning?
  status == "provisioning"
end

#to_hObject



142
143
144
145
146
147
# File 'lib/sendly/numbers_resource.rb', line 142

def to_h
  {
    status: status, number: number&.to_h,
    requirements: requirements, action: action
  }.compact
end