Class: Trails::Twilio::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/trails/twilio/account.rb

Constant Summary collapse

MAX_SMS_LENGTH =
160

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Account

Returns a new instance of Account.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/trails/twilio/account.rb', line 5

def initialize( opts = {} )
  _logger = opts[:logger] || ActiveRecord::Base.logger rescue Logger.new( STDERR )
  if( !opts.blank? ) 
    _logger.warn "overriding default opts #{self.class.config.inspect} with #{opts.inspect}"
  else
    opts = self.class.config[self.class.config.keys.first]
  end
  @config = opts.dup
  @sid = @config[:sid] || raise( "no sid specified on #{self}" )
  @token = @config[:token]
  @logger = _logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/trails/twilio/account.rb', line 4

def config
  @config
end

Class Method Details

.from_request(request) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/trails/twilio/account.rb', line 22

def self.from_request( request )
  sid = sid_from_request( request )
  unless( config.has_key?( sid ) )
    logger.warn{ "unknown twilio account #{sid}. Request params: #{request.inspect}" }
    raise Trails::Exception::UnknownAccount.new( sid )
  end
   = new( config[sid].dup )
  raise Trails::Exception::InvalidSignature unless .verify_caller( request )
  
end

.sid_from_request(request) ⇒ Object



18
19
20
# File 'lib/trails/twilio/account.rb', line 18

def self.sid_from_request( request )
  ( :development == RAILS_ENV.to_sym ) ? request.params['AccountSid'] : request.env["HTTP_X_TWILIO_ACCOUNTSID"]
end

Instance Method Details

#call(number, handler_url, opts = {}) ⇒ Object

Make outgoing calls: Required:

  • number

  • handler_url

Options:

  • :caller

  • :method

  • :timeout



47
48
49
50
51
52
53
54
55
56
# File 'lib/trails/twilio/account.rb', line 47

def call( number, handler_url, opts = {} )
  params = {
    'Caller' => opts['Caller'] || opts[:caller],
    'Called' => number,
    'Url' => handler_url,
    'Method' => opts['Method'] || opts[:method] || 'GET',
    'Timeout' => opts['Timeout'] || opts[:timeout] || 15
  }
  request( 'Calls', 'POST', params )
end

#incoming_numbers(reset = false) ⇒ Object

Sample Response:

[{"SmsFallbackUrl"=>nil, "SmsUrl"=>nil, "PhoneNumber"=>"4253954994", "AccountSid"=>"AC6c25b3d8b4f0a2a4e49e4936398a2180", "Capabilities"=>{"SMS"=>"false", "Voice"=>"false"}, "Method"=>"POST", "Sid"=>"PNc939a026d5a332d22c23ca94a161ce29", "DateUpdated"=>"Sat, 20 Mar 2010 17:52:33 -0700", "DateCreated"=>"Sat, 20 Mar 2010 17:52:33 -0700", "Url"=>nil, "FriendlyName"=>"(425) 395-4994", "VoiceFallbackUrl"=>nil, "SmsFallbackMethod"=>"POST", "VoiceCallerIdLookup"=>"false", "SmsMethod"=>"POST", "VoiceFallbackMethod"=>"POST"}]


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/trails/twilio/account.rb', line 79

def incoming_numbers( reset = false )
  if( @incoming_numbers.nil? || reset )
    response = 
      request( 'IncomingPhoneNumbers', 'GET' )
    
    if( 200 == response.code.to_i )
      @raw_incoming_numbers = Hash.from_xml( response.body ) 
    else
      raise "got response code #{response.code} and body #{response.body}"
    end
    @incoming_numbers = [@raw_incoming_numbers['TwilioResponse']['IncomingPhoneNumbers']['IncomingPhoneNumber']].flatten # returns an array even when it's a single entry
  end
  return @incoming_numbers
end

#outgoing_numbers(reset = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/trails/twilio/account.rb', line 94

def outgoing_numbers( reset = false )
  if( @outgoing_numbers.nil? || reset )
    response = 
      request( 'OutgoingCallerIds', 'GET' )
    @outgoing_numbers_raw = Hpricot( response.body ) if( 200 == response.code.to_i )
    @outgoing_numbers = @outgoing_numbers_raw.search( '//phonenumber').
      collect{|j| j.inner_html}
  end
  return @outgoing_numbers
end

#provision_number(options = {}) ⇒ Object

options: [:area_code, :friendly_name, :url, :sms_url] sameple return:

{"SmsFallbackUrl"=>nil,
   "SmsUrl"=>nil,
   "PhoneNumber"=>"4253954994",
   "AccountSid"=>"AC6c25b3d8b4f0a2a4e49e4936398a2180",
   "Capabilities"=>{"SMS"=>"false",
   "Voice"=>"false"},
   "Method"=>"POST",
   "Sid"=>"PNc939a026d5a332d22c23ca94a161ce29",
   "DateUpdated"=>"Sat,
   20 Mar 2010 17:52:33 -0700",
   "DateCreated"=>"Sat,
   20 Mar 2010 17:52:33 -0700",
   "Url"=>nil,
   "FriendlyName"=>"(425) 395-4994",
   "VoiceFallbackUrl"=>nil,
   "SmsFallbackMethod"=>"POST",
   "VoiceCallerIdLookup"=>"false",
   "SmsMethod"=>"POST",
   "VoiceFallbackMethod"=>"POST"}


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/trails/twilio/account.rb', line 126

def provision_number( options = {} )
  params = {}
  [:area_code, :friendly_name, :url, :sms_url].each do |key|
    params[key.to_s.camelize] = options[key] if options.has_key?( key )
  end

  response = request( 'IncomingPhoneNumbers/Local', 'POST', params )
  if( 201 == response.code.to_i )
    raw_number_response = Hash.from_xml( response.body )
  else
    raise "while trying to acquire a new number, got response #{response.code} and body: #{response.body}"
  end
  raw_number_response["TwilioResponse"]["IncomingPhoneNumber"]
end

#release_number(sid) ⇒ Object



141
142
143
# File 'lib/trails/twilio/account.rb', line 141

def release_number( sid )
  request( File.join( 'IncomingPhoneNumbers', sid ), 'DELETE' )
end

#request(resource, method = 'GET', params = {}) ⇒ Object

just specify the resource (e.g. ‘Calls’ ) and it will append it to the base uri (“/#api_version/Accounts/#sid/”) and then call twilio.



148
149
150
151
# File 'lib/trails/twilio/account.rb', line 148

def request( resource, method = 'GET', params = {})
  url = File.join( base_uri, resource )
  make_request( url, method, params )
end

#send_sms(number, body, opts = {}) ⇒ Object

Required:

  • number: to

  • body: text

Options:

  • :from: number

  • :method: GET/POST



67
68
69
70
71
72
73
74
75
# File 'lib/trails/twilio/account.rb', line 67

def send_sms( number, body, opts = {} )
  params = {
    'From' => opts[:from] || @config[:default_number],
    'To' => number,
    'Body' => body,
    'Method' => opts[:method] || 'POST'
  }
  request( 'SMS/Messages', 'POST', params ) 
end

#verify_caller(request) ⇒ Object



33
34
35
36
# File 'lib/trails/twilio/account.rb', line 33

def verify_caller( request )
  # TODO: check caller credentials here. :)
  return true
end