Skip to content
This repository was archived by the owner on Jul 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--format documentation
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
rvm:
- "1.9.3"
- "2.0.0"
- "2.1.2"
- ruby-head

matrix:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source 'https://rubygems.org'
gem 'pry'
gemspec
1 change: 1 addition & 0 deletions lib/twilio-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
%w<resource finder persistable deletable associations association_proxy>.each { |lib| require File.join(File.dirname(__FILE__), 'twilio', "#{lib}.rb") }

module Twilio
VERSION = "2.1.1"
API_ENDPOINT = 'https://api.twilio.com/2010-04-01'
APIError = Class.new StandardError
ConfigurationError = Class.new StandardError
Expand Down
3 changes: 1 addition & 2 deletions lib/twilio.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
require File.dirname(__FILE__) +'/twilio-rb.rb'
warn 'require "twilio" is deprecated and will be removed in a future release. Use require "twilio-rb" instead.'

warn 'require "twilio" is deprecated and will be removed in a future release. Use require "twilio-rb" instead.'
5 changes: 2 additions & 3 deletions lib/twilio/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Twilio
class Account
include Twilio::Resource
include Twilio::Persistable
extend Twilio::Associations
extend Twilio::Finder
extend Twilio::Associations
extend Twilio::Finder

mutable_attributes :friendly_name, :status

Expand All @@ -19,4 +19,3 @@ def resource_path(account_sid)

end
end

8 changes: 3 additions & 5 deletions lib/twilio/available_phone_number.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Twilio
class AvailablePhoneNumber
include Twilio::Resource
include Twilio::Resource
extend Twilio::Finder

class << self
Expand All @@ -10,12 +10,10 @@ def all(opts={})
number_type = opts.delete('TollFree') ? 'TollFree' : 'Local'
params = { :query => opts } if opts.any?

handle_response get "/Accounts/#{Twilio::Config.account_sid}/AvailablePhoneNumbers/#{country_code}/#{number_type}.json", params
handle_response get "/Accounts/#{Twilio::Config.account_sid}/AvailablePhoneNumbers/#{country_code}/#{number_type}.json", params
end

private :new

undef_method :count

end

# Shortcut for creating a new incoming phone number. Delegates to Twilio::IncomingPhoneNumber.create accepting the same options as that method does.
Expand Down
12 changes: 7 additions & 5 deletions lib/twilio/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def update_attributes(attrs={})
end

private

def resource_name #:nodoc:
klass.name.demodulize.pluralize
end
Expand Down Expand Up @@ -76,25 +77,26 @@ def method_missing(id, *args, &blk) #:nodoc:
end

def add_predicate(attribute) # :nodoc:
metaclass.class_eval do
singleton_class.class_eval do
define_method(attribute) { self['status'] =~ /^#{attribute.gsub '?', ''}/i ? true : false }
end
end

def add_attr_writer(attribute) # :nodoc:
metaclass.class_eval do
singleton_class.class_eval do
define_method(attribute) { |value| self[attribute.to_s.gsub(/\=$/, '').to_sym] = value }
end
end

def add_attr_reader(attribute) #:nodoc:
metaclass.class_eval do
singleton_class.class_eval do
define_method(attribute) { self[attribute] }
end
end

def metaclass #:nodoc:
class << self; self; end
def respond_to_missing?(attribute, include_private = false)
attribute = attribute.to_s
attribute =~ /\?\Z/ || self[attribute.gsub '=', ''].present? || super
end

def self.included(base)
Expand Down
151 changes: 31 additions & 120 deletions spec/account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

describe Twilio::Account do
it_behaves_like 'a collection resource'

before { Twilio::Config.setup :account_sid => 'AC000000000000', :auth_token => '79ad98413d911947f0ba369d295ae7a3' }

Expand All @@ -20,151 +21,61 @@ def stub_api_call(response_file, account_sid=nil)

let(:account) { Twilio::Account.create params }

describe '.count' do
before { stub_api_call 'list_accounts' }
it 'returns the account count' do
Twilio::Account.count.should == 6
end

it 'accepts options to refine the search' do
query = '.json?FriendlyName=example'
stub_request(:get, resource_uri + query).
to_return :body => canned_response('list_accounts'), :status => 200
Twilio::Account.count :friendly_name => 'example'
a_request(:get, resource_uri + query).should have_been_made
end
end

describe '.all' do
before { stub_api_call 'list_accounts' }
let(:resp) { resp = Twilio::Account.all }
it 'returns a collection of objects with a length corresponding to the response' do
resp.length.should == 1
end

it 'returns a collection containing instances of Twilio::Account' do
resp.all? { |r| r.is_a? Twilio::Account }.should be_true
end

JSON.parse(canned_response('list_accounts'))['accounts'].each_with_index do |obj,i|
obj.each do |attr, value|
specify { resp[i].send(attr).should == value }
end
end

it 'accepts options to refine the search' do
query = '.json?FriendlyName=example&Page=5'
stub_request(:get, resource_uri + query).
to_return :body => canned_response('list_accounts'), :status => 200
Twilio::Account.all :page => 5, :friendly_name => 'example'
a_request(:get, resource_uri + query).should have_been_made
end
end

describe '.find' do
context 'for a valid account' do
before do
stub_request(:get, resource_uri + '/AC2a0747eba6abf96b7e3c3ff0b4530f6e' + '.json').
to_return :body => canned_response('account'), :status => 200
end

let(:account) { Twilio::Account.find 'AC2a0747eba6abf96b7e3c3ff0b4530f6e' }

it 'returns an instance of Twilio::Account' do
account.should be_a Twilio::Account
end

JSON.parse(canned_response('account')).each do |k,v|
specify { account.send(k).should == v }
end
end

context 'for a string that does not correspond to a real account' do
before do
stub_request(:get, resource_uri + '/phony' + '.json').to_return :status => 404
end
it 'returns nil' do
account = Twilio::Account.find 'phony'
account.should be_nil
end
end
end

describe '.create' do
before { stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account')}
describe "#friendly_name=" do
let(:account) { Twilio::Account.create params }

it 'creates a new incoming account on the account' do
account
a_request(:post, resource_uri + '.json').with(:body => post_body).should have_been_made
end

it 'returns an instance of Twilio::Account' do
account.should be_a Twilio::Account
end

JSON.parse(canned_response('account')).map do |k,v|
specify { account.send(k).should == v }
end
end


describe '#update_attributes' do
before do
stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account')
stub_request(:post, resource_uri + '/' + account.sid + '.json').with(:body => post_body).
to_return :body => canned_response('account')
stub_request(:post, resource_uri + '/' + account.sid + '.json').
with(:body => URI.encode("FriendlyName=foo")).to_return :body => canned_response('account'), :status => 201
end
context 'when the resource has been persisted' do
it 'updates the API account the new parameters' do
account.update_attributes params
a_request(:post, resource_uri + '/' + account.sid + '.json').with(:body => post_body).should have_been_made
end
end
end

%w<friendly_name>.each do |meth|
describe "##{meth}=" do
let(:account) { Twilio::Account.create params }

before do
stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account')
stub_request(:post, resource_uri + '/' + account.sid + '.json').
with(:body => URI.encode("#{meth.camelize}=foo")).to_return :body => canned_response('account'), :status => 201
end

it "updates the #{meth} property with the API" do
account.send "#{meth}=", 'foo'
a_request(:post, resource_uri + '/' + account.sid + '.json').
with(:body => URI.encode("#{meth.camelize}=foo")).should have_been_made
end
it "updates the friendly_name property with the API" do
account.friendly_name = 'foo'
a_request(:post, resource_uri + '/' + account.sid + '.json').
with(:body => URI.encode("FriendlyName=foo")).should have_been_made
end
end

describe "#active?" do
before { stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account') }

it 'returns true when the account is active' do
account.should be_active
expect(account).to be_active
end

it 'returns false when the account is inactive' do
account.attributes['status'] = 'dead'
account.should_not be_active
expect(account).not_to be_active
end
end

describe "#suspended?" do
before { stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account') }
it 'returns true when the account is suspended' do
account.attributes['status'] = 'suspended'
account.should be_suspended
expect(account).to be_suspended
end
it 'returns false when the account not suspended' do
account.should_not be_suspended
expect(account).not_to be_suspended
end
end
%w<friendly_name status>.each do |meth|
describe "##{meth}=" do
before { stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account') }
before do
stub_request(:post, resource_uri + '.json').with(:body => post_body).
to_return :body => canned_response('account')
request
end

let(:request) do
stub_request(:post, resource_uri + '/' + account.sid + '.json').
with(:body => "#{meth.camelize}=foo").
to_return :body => canned_response('account'), :status => 201
end

it 'updates the friendly name' do
stub_request(:post, resource_uri + '/' + account.sid + '.json').with(:body => "#{meth.camelize}=foo").to_return :body => canned_response('account'), :status => 201

account.send "#{meth}=", 'foo'
a_request(:post, resource_uri + '/' + account.sid + '.json').with(:body => "#{meth.camelize}=foo").should have_been_made
end
Expand All @@ -177,7 +88,7 @@ def stub_api_call(response_file, account_sid=nil)
stub_request(:post, resource_uri + '.json').with(:body => post_body).to_return :body => canned_response('account')
[:calls, :recordings, :conferences, :incoming_phone_numbers, :notifications, :outgoing_caller_ids, :transcriptions].each do |association|
klass = Twilio.const_get association.to_s.classify
klass.expects(:foo).with :account_sid => account.sid
expect(klass).to receive(:foo).with account_sid: account.sid
account.send(association).foo
end
end
Expand All @@ -187,7 +98,7 @@ def stub_api_call(response_file, account_sid=nil)
account = Twilio::Account.new JSON.parse(canned_response('connect_account'))
[:calls, :recordings, :conferences, :incoming_phone_numbers, :notifications, :outgoing_caller_ids, :transcriptions].each do |association|
klass = Twilio.const_get association.to_s.classify
klass.expects(:foo).with :account_sid => account.sid, :connect => true
expect(klass).to receive(:foo).with account_sid: account.sid, :connect => true
account.send(association).foo
end
end
Expand Down
Loading