Class: CloudRailSi::Services::Twilio

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

Constant Summary collapse

SERVICE_CODE =
{
  
    "sendSMSTW" => [
      [ "callFunc", "validateUserInput", "$P0", "$P1", "$P2", "$P3"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "POST"],
      [ "string.concat", "$L0.url", "https://api.twilio.com/2010-04-01/Accounts/", "$P0.accountSid", "/Messages.json"],
      [ "string.urlEncode", "$P1", "$P1"],
      [ "string.urlEncode", "$P2", "$P2"],
      [ "string.urlEncode", "$P3", "$P3"],
      [ "string.concat", "$L1", "From=", "$P1", "&To=", "$P2", "&Body=", "$P3"],
      [ "stream.stringToStream", "$L3", "$L1"],
      [ "set", "$L0.requestBody", "$L3"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L4", "$P0.accountSid", ":", "$P0.authToken"],
      [ "string.base64encode", "$L5", "$L4"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Basic ", "$L5"],
      [ "set", "$L0.requestHeaders.Content-Type", "application/x-www-form-urlencoded"],
      [ "create", "$L6", "Object"],
      [ "http.requestCall", "$L6", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L6"],
      [ "stream.streamToString", "$L7", "$L6.responseBody"],
      [ "debug.out", "$L7"]
      
    ],
  
    "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", 21, 4],
      [ "string.lastIndexOf", "$L0", "$P1", "+"],
      [ "if!=than", "$L0", 0, 2],
      [ "create", "$L1", "Error", "The 'From' phone number or ID is wrong. It should be either a phone number in E.164 format (e.g. +16175551212) or a MessagingServiceSid (e.g. MGec9516eb5a051a6b2901748b925a5a43).", "IllegalArgument"],
      [ "throwError", "$L1"],
      [ "if>than", "$L2", 34, 2],
      [ "create", "$L1", "Error", "The 'From' phone number or ID is wrong. It should be either a phone number in E.164 format (e.g. +16175551212) or a MessagingServiceSid (e.g. MGec9516eb5a051a6b2901748b925a5a43).", "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"]
      
    ],
  
    "validateResponse" => [
      [ "if>=than", "$P1.code", 400, 10],
      [ "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"],
      [ "json.parse", "$L0", "$P1.responseBody"],
      [ "string.concat", "$L2", "$P1.code", " - ", "$L0.message"],
      [ "create", "$L3", "Error", "$L2", "Http"],
      [ "throwError", "$L3"]
      
    ]
  
}

Instance Method Summary collapse

Constructor Details

#initialize(redirect_receiver, account_sid, auth_token) ⇒ Twilio

Returns a new instance of Twilio.



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/Twilio.rb', line 100

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

    ServiceCode::InitSelfTest.init_test('Twilio')

    @interpreter_storage['accountSid'] = 
    @interpreter_storage['authToken'] = auth_token
    

    

    # 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/Twilio.rb', line 122

def check_for_error(error)
  if (error)
    ServiceCode::Statistics.add_error('Twilio', '')
    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/Twilio.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/Twilio.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/Twilio.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/Twilio.rb', line 143

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

  check_for_error(ip.sandbox.thrown_error)
  return nil
end