Skip to content

Commit

Permalink
Define next-gen list parser behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
skimbrel authored and AlexPayment committed Feb 12, 2015
1 parent 7824e96 commit 74191c0
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/twilio-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'twilio-ruby/rest/errors'
require 'twilio-ruby/rest/utils'
require 'twilio-ruby/rest/list_resource'
require 'twilio-ruby/rest/next_gen_list_resource'
require 'twilio-ruby/rest/instance_resource'
require 'twilio-ruby/rest/sandbox'
require 'twilio-ruby/rest/accounts'
Expand Down
29 changes: 29 additions & 0 deletions lib/twilio-ruby/rest/next_gen_list_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Twilio
module REST
class NextGenListResource < Twilio::REST::ListResource
def list(params={}, full_path=false)
raise "Can't get a resource list without a REST Client" unless @client
response = @client.get @path, params, true
list_key = response['meta']['key']
raise "Couldn't find a list key in response meta" unless list_key
resources = response[list_key]
resource_list = resources.map do |resource|
@instance_class.new "#{@path}/#{resource[@instance_id_key]}", @client,
resource
end
client, list_class = @client, self.class
resource_list.instance_eval do
eigenclass = class << self; self; end
eigenclass.send :define_method, :next_page, &lambda {
if response['meta']['next_page_url']
list_class.new(response['meta']['next_page_url'], client).list({})
else
[]
end
}
end
resource_list
end
end
end
end
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/activities.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Activities < ListResource; end
class Activities < Twilio::REST::NextGenListResource; end
class Activity < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/reservations.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Reservations < ListResource; end
class Reservations < Twilio::REST::NextGenListResource; end
class Reservation < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/task_queues.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class TaskQueues < ListResource; end
class TaskQueues < Twilio::REST::NextGenListResource; end
class TaskQueue < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/task_queues_statistics.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class TaskQueuesStatistics < ListResource
class TaskQueuesStatistics < Twilio::REST::NextGenListResource
def initialize(path, client)
@path, @client = path, client
@instance_class = Twilio::REST::TaskRouter::TaskQueueStatistics
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Tasks < ListResource; end
class Tasks < Twilio::REST::NextGenListResource; end

class Task < InstanceResource
def initialize(path, client, params={})
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/workers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Workers < ListResource; end
class Workers < Twilio::REST::NextGenListResource; end
class Worker < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/workers_statistics.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class WorkersStatistics < InstanceResource; end
class WorkersStatistics < Twilio::REST::InstanceResource; end
class WorkerStatistics < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/workflows.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Workflows < ListResource; end
class Workflows < Twilio::REST::NextGenListResource; end
class Workflow < InstanceResource; end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/task_router/workspaces.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Twilio
module REST
module TaskRouter
class Workspaces < ListResource; end
class Workspaces < Twilio::REST::NextGenListResource; end

class Workspace < InstanceResource
def initialize(path, client, params={})
Expand Down
2 changes: 1 addition & 1 deletion spec/rest/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
end

it 'should have task queues statistics resource' do
FakeWeb.register_uri(:get, %r/taskrouter\.twilio\.com/, body: '{"task_queues_statistics": [{"task_queue_sid": "WQ123"}]}')
FakeWeb.register_uri(:get, %r/taskrouter\.twilio\.com/, body: '{"meta": {"key": "task_queues_statistics"}, "task_queues_statistics": [{"task_queue_sid": "WQ123"}]}')
client = Twilio::REST::TaskRouterClient.new('someSid', 'someToken', 'someSid')
expect(client).to respond_to(:task_queues_statistics)
expect(client.task_queues_statistics[0].task_queue_sid).to eq('WQ123')
Expand Down

0 comments on commit 74191c0

Please sign in to comment.