Skip to content

Commit

Permalink
refactor the way objects are built from json to make it more easily o…
Browse files Browse the repository at this point in the history
…verrideable
  • Loading branch information
andrewmbenton committed Sep 5, 2011
1 parent db61d26 commit 97d15cb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/twilio-ruby/rest/available_phone_numbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Twilio
module REST
class AvailablePhoneNumbers < ListResource
def initialize(uri, client)
@resource_name = 'countries'
@instance_class = Twilio::REST.const_get 'Country'
@uri, @client = uri, client
@instance_class = Twilio::REST::Country
@list_key, @instance_id_key = 'countries', 'country_code'
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/twilio-ruby/rest/available_phone_numbers/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Twilio
module REST
class Local < ListResource
def initialize(uri, client)
@resource_name = 'available_phone_numbers'
@instance_class = Twilio::REST::AvailablePhoneNumber
@uri, @client = uri, client
@instance_class = Twilio::REST::AvailablePhoneNumber
@list_key, @instance_id_key = 'available_phone_numbers', 'sid'
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twilio-ruby/rest/available_phone_numbers/toll_free.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Twilio
module REST
class TollFree < ListResource
def initialize(uri, client)
@resource_name = 'available_phone_numbers'
@instance_class = Twilio::REST::AvailablePhoneNumber
@uri, @client = uri, client
@instance_class = Twilio::REST::AvailablePhoneNumber
@list_key, @instance_id_key = 'available_phone_numbers', 'sid'
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/twilio-ruby/rest/conferences/participants.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Twilio
module REST
class Participants < ListResource
private
def instance_sid_key
'call_sid'
def initialize(uri, client)
super
# hard-code the json key since participants don't have sids
@instance_id_key = 'call_sid'
end
end

Expand Down
20 changes: 6 additions & 14 deletions lib/twilio-ruby/rest/list_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ class ListResource
include Utils

def initialize(uri, client)
@resource_name = self.class.name.split('::')[-1]
@instance_class = Twilio::REST.const_get @resource_name.chop
@uri, @client = uri, client
resource_name = self.class.name.split('::')[-1]
@instance_class = Twilio::REST.const_get resource_name.chop
@list_key, @instance_id_key = detwilify(resource_name), 'sid'
end

def inspect # :nodoc:
Expand All @@ -26,9 +27,9 @@ def inspect # :nodoc:
def list(params = {})
raise "Can't get a resource list without a REST Client" unless @client
response = @client.get @uri, params
resources = response[detwilify(@resource_name)]
resources = response[@list_key]
resource_list = resources.map do |resource|
@instance_class.new "#{@uri}/#{resource[instance_sid_key]}", @client,
@instance_class.new "#{@uri}/#{resource[@instance_id_key]}", @client,
resource
end
# set the +total+ property on the array
Expand Down Expand Up @@ -71,18 +72,9 @@ def get(sid)
def create(params={})
raise "Can't create a resource without a REST Client" unless @client
response = @client.post @uri, params
@instance_class.new "#{@uri}/#{response[instance_sid_key]}", @client,
@instance_class.new "#{@uri}/#{response[@instance_id_key]}", @client,
response
end

private

##
# Return the JSON key containing the sid of each instance. This method is
# overridden for conference participants and perhaps other resources.
def instance_sid_key
'sid'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/twilio-ruby/rest/sms/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Messages < ListResource
def initialize(uri, client)
super
# hard-code the json key since 'messages' doesn't exist in the response
@resource_name = 'sms_messages'
@list_key = 'sms_messages'
end
end

Expand Down

0 comments on commit 97d15cb

Please sign in to comment.