class ActionTexter::NexmoResponse

Nexmo response

Constants

SUCCESS_RESPONSE_CODE

Attributes

cost[R]

TODO: Some of these should be moved to Response if they are common enough.

original[R]

TODO: Some of these should be moved to Response if they are common enough.

parts[R]

TODO: Some of these should be moved to Response if they are common enough.

parts_count[R]

TODO: Some of these should be moved to Response if they are common enough.

reference[R]

TODO: Some of these should be moved to Response if they are common enough.

remaining_balance[R]

TODO: Some of these should be moved to Response if they are common enough.

Private Instance Methods

process_response(raw) click to toggle source
# File lib/action_texter/nexmo.rb, line 19
def process_response(raw)
  @success = true
  @original = JSON.parse(raw)
  @parts_count = @original["message-count"].to_i
  @cost = BigDecimal.new("0")
  @reference = @original["messages"].first["client-ref"] # They should all be the same, we only record it the first time.
  @remaining_balance = @original["messages"].last["remaining-balance"] # I hope the last one is the lowest one, the cost of a single message shouldn't make that big of a difference anyway.
  @parts = []
  error_messages = []
  @original["messages"].each do |raw_part|
    if @success # Update the contents of success to status of this part unless @succes is already failed.
      @success = raw_part["status"] == SUCCESS_RESPONSE_CODE
    end
    part = {:id => raw_part["message-id"],
            :to => raw_part["to"],
            :success => raw_part["status"] == SUCCESS_RESPONSE_CODE}
    part[:reference] = raw_part["client-ref"] if raw_part.has_key? "client-ref"
    if raw_part.has_key? "message-price"
      part[:cost] = raw_part["message-price"]
      @cost += BigDecimal.new(raw_part["message-price"])
    end
    if raw_part.has_key? "remaining-balance"
      part[:remaining_balance] = BigDecimal.new(raw_part["remaining-balance"])
    end
    if raw_part.has_key? "error-text"
      part[:error_message] = raw_part["error-text"]
      error_messages << part[:error_message]
    end
    @parts << part
  end
  if error_messages.any?
    @error_message = error_messages.uniq.join(", ")
  end
end