Sha256: 4a9441d29a3b39894800aa196383932863dc1be6c68e91038f9627ae6ef48754

Contents?: true

Size: 1.41 KB

Versions: 31

Compression:

Stored size: 1.41 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

      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
          instance_variable_set("@#{r}", resource_class.new(path, @client))
        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

31 entries across 31 versions & 1 rubygems

Version Path
twilio-ruby-4.13.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.12.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.11.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.11.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.10.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.9.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.9.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.8.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.7.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.6.2 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.6.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.6.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.5.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.4.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.3.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.2.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.2.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.1.0 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.0.1 lib/twilio-ruby/rest/utils.rb
twilio-ruby-4.0.0 lib/twilio-ruby/rest/utils.rb