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.
-
#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 _client gets 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 _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(, &block) @client = .delete(:_client) @stored_params = with() end |
Instance Attribute Details
#auto_pagination ⇒ Object
25 26 27 |
# File 'lib/crowdkit/api.rb', line 25 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 |
#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
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/crowdkit/api.rb', line 120 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
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) = 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 [:_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.
33 34 35 36 37 38 39 |
# File 'lib/crowdkit/api.rb', line 33 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
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
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 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 |