Class: TwilioResource::Base

Inherits:
ReactiveResource::Base
  • Object
show all
Defined in:
lib/twilio_resource/base.rb

Overview

Encapsulates the changes that need to be made to active_resource’s defaults in order to communicate with Twilio. There are a few main issues with ActiveResource’s defaults:

  1. Twilio’s urls don’t take an extension. ActiveResource requires endpoints to have an extension corresponding to the type of data, for example: resource.xml or resource.json. Twilio’s are always xml, but have no extension.

  2. Twilio uses capitalized names in their URLs. For instance, instead of ‘/account/1/calls/1’, they use ‘/Account/1/Calls/1’.

  3. Twilio takes form encoded params, like token=blah&sid=foo, but returns data in XML. These changes are encapsulated in ActiveResource::Formats::TwilioFormat.

All of the Twilio ActiveResource classes inherit from this class.

Direct Known Subclasses

Account, Call, IncomingPhoneNumber, OutgoingCallerId

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.sidObject Also known as: user

Returns the value of attribute sid.



23
24
25
# File 'lib/twilio_resource/base.rb', line 23

def sid
  @sid
end

.tokenObject Also known as: password

Returns the value of attribute token.



24
25
26
# File 'lib/twilio_resource/base.rb', line 24

def token
  @token
end

Class Method Details

.collection_path(prefix_options = {}, query_options = nil) ⇒ Object

Add logging to these requests



40
41
42
43
44
# File 'lib/twilio_resource/base.rb', line 40

def self.collection_path(prefix_options = {}, query_options = nil)
  path = super(prefix_options, query_options)
  TwilioResource.logger.info("Request: #{path}")
  path
end

.custom_method_collection_url(method_name, options = {}) ⇒ Object

Add logging to these requests



47
48
49
50
51
# File 'lib/twilio_resource/base.rb', line 47

def self.custom_method_collection_url(method_name, options = {})
  path = super(method_name, options)
  TwilioResource.logger.info("Request: #{path}")
  path
end

.element_nameObject

Twilio uses capitalized path names



54
55
56
# File 'lib/twilio_resource/base.rb', line 54

def self.element_name
  super.camelize
end

.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object

Add logging to these requests



33
34
35
36
37
# File 'lib/twilio_resource/base.rb', line 33

def self.element_path(id, prefix_options = {}, query_options = nil)
  path = super(id, prefix_options, query_options)
  TwilioResource.logger.info("Request: #{path}")
  path
end

.query_string(params) ⇒ Object



58
59
60
61
62
63
# File 'lib/twilio_resource/base.rb', line 58

def self.query_string(params)
  # camelize all the param keys, because that's what Twilio expects
  fixed_params_list = params.map {|k, v| [k.to_s.camelize, v]}.flatten
  fixed_params_hash = Hash[*fixed_params_list] # Hash really needs a decent #map
  super(fixed_params_hash)
end

Instance Method Details

#save(*params) ⇒ Object

we should wrap the exceptions we can



66
67
68
69
70
71
72
# File 'lib/twilio_resource/base.rb', line 66

def save(*params)
  begin
    super(*params)
  rescue => e
    raise TwilioResource::Exception.find_exception(e)
  end
end