Sha256: 51bb492817863f7bfd1660f14f0a6b719d1bb6861d1a46cc41af1642e0943b1e

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

module Twilio
  module REST
    module Utils

      def twilify(something)
        return key_map(something, :twilify) if something.is_a? Hash
        string = something.to_s
        string.split('_').map do |string_part|
          string_part[0,1].capitalize + string_part[1..-1]
        end.join
      end

      def detwilify(something)
        return key_map(something, :detwilify) if something.is_a? Hash
        string = something.to_s
        string = string[0,1].downcase + string[1..-1]
        string.gsub(/[A-Z][a-z]*/) { |s| "_#{s.downcase}" }
      end

      protected

      # Freeze the base list path.
      # Used on list resources so filters (such as /Local) do not affect the
      # instance resource path.
      def freeze_path
        @frozen_path = @path
      end

      def resource(*resources)
        custom_resource_names = { sms: 'SMS', sip: 'SIP' }
        resources.each do |r|
          resource = twilify r
          relative_path = custom_resource_names.fetch(r, resource)
          path = "#{@path}/#{relative_path}"
          enclosing_module = if @submodule == nil
            Twilio::REST
          else
            Twilio::REST.const_get(@submodule)
          end
          resource_class = enclosing_module.const_get resource
          resource_object = resource_class.new(path, @client)
          instance_variable_set("@#{r}", resource_object)
          if @frozen_path
            resource_object.instance_variable_set(:@frozen_path, @frozen_path)
          end
        end
        self.class.instance_eval { attr_reader *resources }
      end

      private

      def key_map(something, method)
        something = something.to_a.flat_map do |pair|
          [send(method, pair[0]).to_sym, pair[1]]
        end
        Hash[*something]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
twilio-ruby-4.10.0.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.9.1.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.9.0.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.8.3.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.8.2.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.8.1.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.8.0.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.7.0.edge lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.6.0.edge lib/twilio-ruby/rest/utils.rb