Class: TwilioResource::Base

Inherits:
ActiveResource::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

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.



21
22
23
# File 'lib/twilio_resource/base.rb', line 21

def sid
  @sid
end

.tokenObject Also known as: password

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

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

Have to override this to make empty extensions work (without the dot)



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

def self.collection_path(prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  extension = format.extension.blank? ? "" : ".#{format.extension}"
  path = "#{prefix(prefix_options)}#{collection_name}#{extension}#{query_string(query_options)}"
  TwilioResource.logger.info("Request: #{path}")
  path
end

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

Have to override this to make empty extensions work (without the dot)



49
50
51
52
53
54
55
# File 'lib/twilio_resource/base.rb', line 49

def self.custom_method_collection_url(method_name, options = {})
  prefix_options, query_options = split_options(options)
  extension = format.extension.blank? ? "" : ".#{format.extension}"
  path = "#{prefix(prefix_options)}#{collection_name}/#{method_name}#{extension}#{query_string(query_options)}"
  TwilioResource.logger.info("Request: #{path}")
  path
end

.element_nameObject

Twilio uses capitalized path names



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

def self.element_name
  super.camelize
end

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

Have to override this to make empty extensions work (without the dot)



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

def self.element_path(id, prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  extension = format.extension.blank? ? "" : ".#{format.extension}"
  path = "#{prefix(prefix_options)}#{collection_name}/#{id}#{extension}#{query_string(query_options)}"
  TwilioResource.logger.info("Request: #{path}")
  path
end

.query_string(params) ⇒ Object



62
63
64
65
66
67
# File 'lib/twilio_resource/base.rb', line 62

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



70
71
72
73
74
75
76
77
# File 'lib/twilio_resource/base.rb', line 70

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

end