class Crowdkit::API

Attributes

auto_pagination[W]
client[R]
stored_params[RW]

Public Class Methods

extract_class_name(name, options) click to toggle source

Extracts class name from options

@return [String]

@api private

# File lib/crowdkit/api.rb, line 120
def self.extract_class_name(name, options)
  if !options[:class_name]
    converted  = options.fetch(:full_name, name).to_s
    converted  = converted.split('_').map(&:capitalize).join
    class_name = options.fetch(:root, false) ? '': "#{self.name}::"
    class_name += converted
    class_name
  else
    options[:class_name]
  end
end
namespace(*names) click to toggle source

Defines a namespace

@param [Array] names

the name for the scope

@return [self]

@api public

# File lib/crowdkit/api.rb, line 99
def self.namespace(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}
  names   = names.map(&:to_sym)
  name    = names.pop
  return if public_method_defined?(name)

  class_name = extract_class_name(name, options)
  define_method(name) do |*args, &block|
    options = args.last.is_a?(Hash) ? args.pop : {}
    options[:_args] = args
    options[:_client] = @client
    API::Factory.new(class_name, stored_params.merge(options), &block)
  end
  self
end
new(options, &block) click to toggle source

This should only be called by API Factory to ensure _client gets set. We override this in Client as it’s the root of the API.

# File lib/crowdkit/api.rb, line 19
def initialize(options, &block)
  @client = options.delete(:_client)
  @stored_params = options
  with(options)
end

Public Instance Methods

arguments(args=(not_set = true), options={}, &block) click to toggle source

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.

# File lib/crowdkit/api.rb, line 33
def arguments(args=(not_set = true), options={}, &block)
  if not_set
    @arguments
  else
    @arguments = Arguments.new(self, options.symbolize_keys).parse(*args, &block)
  end
end
auto_pagination() click to toggle source
# File lib/crowdkit/api.rb, line 25
def auto_pagination
  @auto_pagination || config.auto_paginate
end
id_key() click to toggle source

overridden in children API’s that have their own id’s

# File lib/crowdkit/api.rb, line 68
def id_key
  :job_id
end
set(option, value=(not_set=true), ignore_setter=false, &block) click to toggle source

Set an option to a given value

@param [String] option @param [Object] value @param [Boolean] ignore_setter

@return [self]

@api public

# File lib/crowdkit/api.rb, line 50
def set(option, value=(not_set=true), ignore_setter=false, &block)
  raise ArgumentError, 'value not set' if block and !not_set
  return self if !not_set and value.nil?

  if not_set
    set_options option
    return self
  end

  if respond_to?("#{option}=") and not ignore_setter
    return __send__("#{option}=", value)
  end

  define_accessors option, value
  self
end
with(args) click to toggle source

Scope for passing request required arguments.

# File lib/crowdkit/api.rb, line 74
def with(args)
  case args
  when Hash
    #This happens when the job id is passed early in the chain
    if (custom = args.delete(:_args)) && custom.first
      stored_params[id_key] = custom.first
      set id_key, custom.first
    end
    set args
  when Fixnum, /^(\d+|([a-z])([a-z]|\d)*)$/
    stored_params[id_key] = args
    set id_key, args
  else
    ::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
  end
end

Private Instance Methods

define_accessors(option, value) click to toggle source

Define setters and getters

@api private

# File lib/crowdkit/api.rb, line 147
def define_accessors(option, value)
  setter = proc { |val|  set option, val, true }
  getter = proc { value }

  define_singleton_method("#{option}=", setter) if setter
  define_singleton_method(option, getter) if getter
end
define_singleton_method(method_name, content=Proc.new) click to toggle source

Dynamically define a method for setting request option

@api private

# File lib/crowdkit/api.rb, line 158
def define_singleton_method(method_name, content=Proc.new)
  (class << self; self; end).class_eval do
    undef_method(method_name) if method_defined?(method_name)
    if String === content
      class_eval("def #{method_name}() #{content}; end")
    else
      define_method(method_name, &content)
    end
  end
end
set_options(options) click to toggle source

Set multiple options

@api private

# File lib/crowdkit/api.rb, line 137
def set_options(options)
  unless options.respond_to?(:each)
    raise ArgumentError, 'cannot iterate over value'
  end
  options.each { |key, value| set(key, value) }
end