Sha256: 9fb016296ce8f7993f8efaf401fa98b24c4a38e45141ae763274574dc071cb8b

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

module Twilio
  module REST
    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
      end
    
      # Grab a list of this kind of resource and return it as an array.
      def list(params={})
        raise "Can't get a resource list without a Twilio::Client" unless @client
        response = @client.get @uri, params
        resources = response[detwilify(@resource_name)]
        resources.map do |resource|
          @instance_class.new "#{@uri}/#{resource['sid']}", @client, resource
        end
      end

      # Return an empty instance resource object with the proper URI.
      def get(sid)
        @instance_class.new "#{@uri}/#{sid}", @client
      end
    
      # Return a newly created resource.
      def create(params={})
        raise "Can't create a resource without a Twilio::Client" unless @client
        response = @client.post @uri, params
        @instance_class.new "#{@uri}/#{response['sid']}", @client, response
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
twilio-ruby-0.4.0 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.3.3 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.3.2 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.3.1 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.3.0 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.2.1 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.2.0 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.1.2 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.1.1 lib/twilio-ruby/rest/list_resource.rb
twilio-ruby-0.1.0 lib/twilio-ruby/rest/list_resource.rb