Class: Crowdkit::API
- Inherits:
-
Object
- Object
- Crowdkit::API
- 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
Direct Known Subclasses
Client, Client::Account, Client::Jobs, Client::Judgments, Client::Poll, Client::Statuses, Client::Units, Client::Workers, Client::Worksets
Defined Under Namespace
Modules: RequestMethods Classes: Arguments, Factory, ResponseWrapper
Instance Attribute Summary collapse
- #auto_pagination ⇒ Object
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#stored_params ⇒ Object
Returns the value of attribute stored_params.
Class Method Summary collapse
-
.extract_class_name(name, options) ⇒ String
private
Extracts class name from options.
-
.namespace(*names) ⇒ self
Defines a namespace.
Instance Method Summary collapse
-
#arguments(args = (not_set = true), options = {}, &block) ⇒ Object
Acts as setter and getter for api requests arguments parsing.
-
#id_key ⇒ Object
overridden in children API’s that have their own id’s.
-
#initialize(options, &block) ⇒ API
constructor
This should only be called by API Factory to ensure _config, and _client get set.
-
#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ self
Set an option to a given value.
-
#with(args) ⇒ Object
Scope for passing request required arguments.
Methods included from RequestMethods
Constructor Details
#initialize(options, &block) ⇒ API
This should only be called by API Factory to ensure _config, and _client get set. We override this in Client as it’s the root of the API.
19 20 21 22 23 24 |
# File 'lib/crowdkit/api.rb', line 19 def initialize(, &block) @config = .delete(:_config) @client = .delete(:_client) @stored_params = with() end |
Instance Attribute Details
#auto_pagination ⇒ Object
26 27 28 |
# File 'lib/crowdkit/api.rb', line 26 def auto_pagination @auto_pagination || @config.auto_paginate end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/crowdkit/api.rb', line 11 def client @client end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/crowdkit/api.rb', line 11 def config @config end |
#stored_params ⇒ Object
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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/crowdkit/api.rb', line 122 def self.extract_class_name(name, ) if ![:class_name] converted = .fetch(:full_name, name).to_s converted = converted.split('_').map(&:capitalize).join class_name = .fetch(:root, false) ? '': "#{self.name}::" class_name += converted class_name else [:class_name] end end |
.namespace(*names) ⇒ self
Defines a namespace
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/crowdkit/api.rb', line 100 def self.namespace(*names) = 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, ) define_method(name) do |*args, &block| = args.last.is_a?(Hash) ? args.pop : {} [:_args] = args [:_config] = @config [:_client] = @client API::Factory.new(class_name, stored_params.merge(), &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.
34 35 36 37 38 39 40 |
# File 'lib/crowdkit/api.rb', line 34 def arguments(args=(not_set = true), ={}, &block) if not_set @arguments else @arguments = Arguments.new(self, .symbolize_keys).parse(*args, &block) end end |
#id_key ⇒ Object
overridden in children API’s that have their own id’s
69 70 71 |
# File 'lib/crowdkit/api.rb', line 69 def id_key :job_id end |
#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ self
Set an option to a given value
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/crowdkit/api.rb', line 51 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 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/crowdkit/api.rb', line 75 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 |