Skip to content

Commit 6b56743

Browse files
committed
[VOICE-775] Add voice namespace and phone_number resource.
This commit adds a `Voice::PhoneNumber` resource, and a way to call `client.voice.phone_numbers.create(...)` etc. This is achieved using a `Delegator` class to forward method calls after the `voice` namespace back to the client object to be handled by `method_missing`.
1 parent adbbdde commit 6b56743

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

lib/zendesk_api/association.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'zendesk_api/helpers'
22

33
module ZendeskAPI
4-
# Represents an association between two resources
4+
# Represents an association between two resources
55
# @private
66
class Association
77
# @return [Hash] Options passed into the association
@@ -10,7 +10,7 @@ class Association
1010
# Options to pass in
1111
# * class - Required
1212
# * parent - Parent instance
13-
# * path - Optional path instead of resource name
13+
# * path - Optional path instead of resource name
1414
def initialize(options = {})
1515
@options = Hashie::Mash.new(options)
1616
end
@@ -32,7 +32,7 @@ def generate_path(*args)
3232
instance = args.first
3333

3434
namespace = @options[:class].to_s.split("::")
35-
namespace.delete("ZendeskAPI")
35+
%w(ZendeskAPI Voice).each { |ns| namespace.delete(ns) }
3636
has_parent = namespace.size > 1 || (options[:with_parent] && @options.parent)
3737

3838
if has_parent

lib/zendesk_api/client.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'zendesk_api/middleware/response/parse_iso_dates'
1616
require 'zendesk_api/middleware/response/raise_error'
1717
require 'zendesk_api/middleware/response/logger'
18+
require 'zendesk_api/delegator'
1819

1920
module ZendeskAPI
2021
# The top-level class that handles configuration and connection to the Zendesk API.
@@ -33,11 +34,13 @@ def method_missing(method, *args, &block)
3334
options = args.last.is_a?(Hash) ? args.pop : {}
3435

3536
@resource_cache[method] ||= { :class => nil, :cache => ZendeskAPI::LRUCache.new }
36-
3737
if !options.delete(:reload) && (cached = @resource_cache[method][:cache].read(options.hash))
3838
cached
3939
else
40-
@resource_cache[method][:class] ||= ZendeskAPI.const_get(ZendeskAPI::Helpers.modulize_string(Inflection.singular(method)))
40+
klass_as_const = ZendeskAPI::Helpers.modulize_string(Inflection.singular(method))
41+
klass = class_from_namespace(klass_as_const)
42+
43+
@resource_cache[method][:class] ||= klass
4144
@resource_cache[method][:cache].write(options.hash, ZendeskAPI::Collection.new(self, @resource_cache[method][:class], options))
4245
end
4346
end
@@ -109,6 +112,10 @@ def self.check_deprecated_namespace_usage(attributes, name)
109112
end
110113
end
111114

115+
def voice
116+
Delegator.new(self)
117+
end
118+
112119
protected
113120

114121
# Called by {#connection} to build a connection. Can be overwritten in a
@@ -158,6 +165,16 @@ def build_connection
158165

159166
private
160167

168+
def class_from_namespace(klass_as_const)
169+
[ZendeskAPI, ZendeskAPI::Voice].each do |ns|
170+
if ns.const_defined?(klass_as_const)
171+
return ns.const_get(klass_as_const)
172+
end
173+
end
174+
175+
nil
176+
end
177+
161178
def check_url
162179
if !config.allow_http && config.url !~ /^https/
163180
raise ArgumentError, "zendesk_api is ssl only; url must begin with https://"

lib/zendesk_api/delegator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ZendeskAPI
2+
class Delegator < SimpleDelegator; end
3+
end
4+

lib/zendesk_api/resources.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,10 @@ def self.singular_resource_name
466466
end
467467

468468
class Target < Resource; end
469+
470+
module Voice
471+
class PhoneNumber < Resource
472+
namespace "channels/voice"
473+
end
474+
end
469475
end

spec/core/client_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def build_connection
1919
ZendeskAPI::Client.new do |config|
2020
config.url = "http://www.google.com"
2121
end
22-
end.to raise_error(ArgumentError)
22+
end.to raise_error(ArgumentError)
2323
end
2424

2525
it "should not raise an exception when url isn't ssl and allow_http is set to true" do
@@ -115,7 +115,7 @@ def build_connection
115115

116116
context "#logger" do
117117
before(:each) do
118-
@client = ZendeskAPI::Client.new do |config|
118+
@client = ZendeskAPI::Client.new do |config|
119119
config.url = "https://example.zendesk.com/"
120120
config.logger = subject
121121
end
@@ -232,4 +232,15 @@ def build_connection
232232
client.connection.should == "FOO"
233233
client.connection.object_id.should == client.connection.object_id # it's cached
234234
end
235+
236+
context ZendeskAPI::Voice do
237+
it "defers to voice delegator" do
238+
ZendeskAPI::Client.any_instance.should_receive(:phone_numbers).once
239+
subject.voice.phone_numbers
240+
end
241+
242+
it "manages namespace correctly" do
243+
ZendeskAPI::Voice::PhoneNumber.new(subject, {}).path.should match(/channels\/voice\/phone_numbers/)
244+
end
245+
end
235246
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
describe ZendeskAPI::Voice::PhoneNumber, :delete_after do
2+
def valid_attributes
3+
{:number => "+14434064759", :country_code => "US", :toll_free => "false"}
4+
end
5+
6+
it_should_be_creatable
7+
it_should_be_updatable :nickname
8+
it_should_be_updatable :transcription
9+
it_should_be_updatable :recorded
10+
it_should_be_deletable
11+
end

0 commit comments

Comments
 (0)