Class: CloudRailSi::Services::Nexmo

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/services/Nexmo.rb

Constant Summary collapse

SERVICE_CODE =
{
  
    "SendNexmoSMS" => [
      [ "callFunc", "validateUserInput", "$P0", "$P1", "$P2", "$P3"],
      [ "create", "$L0", "Object"],
      [ "create", "$L1", "Object"],
      [ "create", "$L2", "Object"],
      [ "create", "$L3", "String"],
      [ "create", "$L4", "String"],
      [ "string.urlEncode", "$L3", "$P3"],
      [ "string.urlEncode", "$L5", "$P1"],
      [ "string.concat", "$L0.url", "https://rest.nexmo.com/sms/json?api_key=", "$P0.clientID", "&api_secret=", "$P0.clientSecret", "&to=", "$P2", "&from=", "$L5", "&text=", "$L3"],
      [ "set", "$L0.method", "POST"],
      [ "set", "$L1.Content-Type", "application/x-www-form-urlencoded"],
      [ "set", "$L1.Content-Length", "0"],
      [ "set", "$L0.requestHeaders", "$L1"],
      [ "debug.out", "$L0.url"],
      [ "http.requestCall", "$L2", "$L0"],
      [ "debug.out", "$L2"],
      [ "callFunc", "checkError", "$P0", "$L2"]
      
    ],
  
    "validateUserInput" => [
      [ "if==than", "$P1", nil, 2],
      [ "create", "$L1", "Error", "One of the arguments is 'null'. You need to assign a value to it.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "if==than", "$P2", nil, 2],
      [ "create", "$L1", "Error", "One of the arguments is 'null'. You need to assign a value to it.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "if==than", "$P3", nil, 2],
      [ "create", "$L1", "Error", "One of the arguments is 'null'. You need to assign a value to it.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P1"],
      [ "if==than", "$L2", 0, 2],
      [ "create", "$L1", "Error", "The 'From' number parameter is empty.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P2"],
      [ "if==than", "$L2", 0, 2],
      [ "create", "$L1", "Error", "The 'To' number parameter is empty.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P3"],
      [ "if==than", "$L2", 0, 2],
      [ "create", "$L1", "Error", "The message is empty.", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P1"],
      [ "if>than", "$L2", 16, 2],
      [ "create", "$L1", "Error", "The 'From' phone number is too big, it should have maximum 15 digits. Example: +16175551212", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "string.lastIndexOf", "$L0", "$P2", "+"],
      [ "if!=than", "$L0", 0, 2],
      [ "create", "$L1", "Error", "The 'To' phone number isn't in E.164 format. Example: +16175551212", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P2"],
      [ "if>than", "$L2", 16, 2],
      [ "create", "$L1", "Error", "The 'To' phone number is too big, it should have maximum 15 digits. Example: +16175551212", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "size", "$L2", "$P3"],
      [ "if>than", "$L2", 1600, 2],
      [ "create", "$L1", "Error", "The length of the message exceeds the 1600 allowed characters.", "IllegalArgument"],
      [ "throwError", "$L1"]
      
    ],
  
    "checkError" => [
      [ "if>=than", "$P1.code", 400, 8],
      [ "if==than", "$P1.code", 401, 2],
      [ "create", "$L3", "Error", "Invalid credentials or access rights. Make sure that your application has read and write permission.", "Authentication"],
      [ "throwError", "$L3"],
      [ "if==than", "$P1.code", 503, 2],
      [ "create", "$L3", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
      [ "throwError", "$L3"],
      [ "create", "$L3", "Error", "$P1.message", "Http"],
      [ "throwError", "$L3"],
      [ "stream.streamToString", "$L11", "$P1.responseBody"],
      [ "create", "$L12", "Object"],
      [ "json.parse", "$L12", "$L11"],
      [ "debug.out", "$L12"],
      [ "set", "$L13", "$L12.messages.0.status"],
      [ "set", "$L14", "$L12.messages.0.error-text"],
      [ "debug.out", "$L13"],
      [ "if!=than", "$L13", "0", 2],
      [ "create", "$L15", "Error", "$L14", "Http"],
      [ "throwError", "$L15"]
      
    ]
  
}

Instance Method Summary collapse

Constructor Details

#initialize(redirect_receiver, client_id, client_secret) ⇒ Nexmo

Returns a new instance of Nexmo.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cloudrail_si/services/Nexmo.rb', line 100

def initialize(redirect_receiver, client_id, client_secret)
    @interpreter_storage = {}
    @persistent_storage = [{}]
    @instance_dependency_storage = {
        redirect_receiver: redirect_receiver
    }

    ServiceCode::InitSelfTest.init_test('Nexmo')

    @interpreter_storage['clientID'] = client_id
    @interpreter_storage['clientSecret'] = client_secret
    

    

    # call init servicecode function
    ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
    # check and call the initialization function if is available

    ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
end

Instance Method Details

#check_for_error(error) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cloudrail_si/services/Nexmo.rb', line 122

def check_for_error(error)
  if (error)
    ServiceCode::Statistics.add_error('Nexmo', '')
    case (error.type)
    when 'IllegalArgument'
      raise Errors::IllegalArgumentError.new(error.message)
    when 'Authentication'
      raise Errors::AuthenticationError.new(error.message)
    when 'NotFound'
      raise Errors::NotFoundError.new(error.message)
    when 'Http'
      raise Errors::HttpError.new(error.message)
    when 'ServiceUnavailable'
      raise Errors::ServiceUnavailableError.new(error.message)
    else
      raise Errors::StandardError.new(error.message)
    end
  end
end

#load_as_string(saved_state) ⇒ Object



158
159
160
161
162
163
# File 'lib/cloudrail_si/services/Nexmo.rb', line 158

def load_as_string(saved_state)
	sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
	ip = ServiceCode::Interpreter.new(sandbox)
	ip.load_as_string(saved_state)
	@persistent_storage = sandbox.persistent_storage
end

#resume_login(execution_state, callback) ⇒ Object



165
166
167
168
169
170
# File 'lib/cloudrail_si/services/Nexmo.rb', line 165

def (execution_state, callback)
  sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
  sandbox.load_state_from_string(execution_state)
  ip = ServiceCode::Interpreter.new(sandbox)
  ip.resume_function('Authenticating:login', @interpreter_storage, callback)
end

#save_as_stringObject



153
154
155
156
# File 'lib/cloudrail_si/services/Nexmo.rb', line 153

def save_as_string
	ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
	return ip.save_as_string()
end

#send_sms(from_name, to_number, content) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/cloudrail_si/services/Nexmo.rb', line 143

def send_sms(from_name, to_number, content)
  ServiceCode::Statistics.add_call('Nexmo', 'send_sms')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('SendNexmoSMS', @interpreter_storage, from_name, to_number, content)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end