Class: Crowdkit::API

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RequestMethods
Defined in:
lib/crowdkit/api.rb,
lib/crowdkit/api/factory.rb,
lib/crowdkit/api/arguments.rb,
lib/crowdkit/api/request_methods.rb,
lib/crowdkit/api/response_wrapper.rb

Defined Under Namespace

Modules: RequestMethods Classes: Arguments, Factory, ResponseWrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods

#agent, #method

Constructor Details

#initialize(options, &block) ⇒ API

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.



19
20
21
22
23
# File 'lib/crowdkit/api.rb', line 19

def initialize(options, &block)
  @client = options.delete(:_client)
  @stored_params = options
  with(options)
end

Instance Attribute Details

#auto_paginationObject



25
26
27
# File 'lib/crowdkit/api.rb', line 25

def auto_pagination
  @auto_pagination || config.auto_paginate
end

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/crowdkit/api.rb', line 11

def client
  @client
end

#stored_paramsObject

Returns the value of attribute stored_params.



10
11
12
# File 'lib/crowdkit/api.rb', line 10

def stored_params
  @stored_params
end

Class Method Details

.extract_class_name(name, options) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts class name from options



120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ self

Defines a namespace



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

Instance Method Details

#arguments(args = (not_set = true), options = {}, &block) ⇒ Object

Acts as setter and getter for api requests arguments parsing.

Returns Arguments instance.



33
34
35
36
37
38
39
# 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

#id_keyObject

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



68
69
70
# File 'lib/crowdkit/api.rb', line 68

def id_key
  :job_id
end

#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ self

Set an option to a given value

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Scope for passing request required arguments.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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