Sha256: 69d074799db0ccaab44af51ad3784bcee12e8a392b8e24cba81aacef5527c330

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module TelphinApi
  # An API method. It is responsible for generating it's full name and determining it's type.
  class Method
    include Resolvable
    
    # A pattern for names of methods with a boolean result.
    PREDICATE_NAMES = /^is.*\?$/
    
    # Calling the API method.
    # It delegates the network request to `API.call` and result processing to `Result.process`.
    # @param [Hash] args Arguments for the API method.
    def call(args = {}, &block)
      response = API.call(full_method, args, token)
      Result.process(response, type, block)
    end
    
  private
    def full_method
      [@previous_resolver.name, @name].compact.map { |part| camelize(part).gsub(/[^A-Za-z.]/, '') }
    end
    
    def type
      @name =~ PREDICATE_NAMES ? :boolean : :anything
    end
    
    # camelize('get_profiles')
    # => 'getProfiles'
    def camelize(name)
      words = name.split('_')
      first_word = words.shift
      
      words.each do |word|
        word.sub!(/^[a-z]/, &:upcase)
      end
      
      words.unshift(first_word).join
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
telphin_api-1.0.4 lib/telphin_api/method.rb
telphin_api-1.0.3 lib/telphin_api/method.rb
telphin_api-1.0.2 lib/telphin_api/method.rb
telphin_api-1.0.1 lib/telphin_api/method.rb
telphin_api-1.0.0 lib/telphin_api/method.rb