Sha256: 46008862740ec5a1f16d633406b248c3c13831a87230aafdbafa8ff1435e197c

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module TwilioLookups
  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
            TwilioLookups::REST
          else
            TwilioLookups::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

2 entries across 2 versions & 1 rubygems

Version Path
twilio-lookups-0.0.2 lib/twilio-lookups/rest/utils.rb
twilio-lookups-0.0.1 lib/twilio-lookups/rest/utils.rb