Class: Scale

Inherits:
Object
  • Object
show all
Defined in:
lib/scale.rb,
lib/scale/api.rb,
lib/scale/api/tasks.rb,
lib/scale/api/errors.rb,
lib/scale/api/callback.rb,
lib/scale/api/task_list.rb,
lib/scale/api/tasks/base_task.rb

Defined Under Namespace

Classes: Api

Constant Summary collapse

VALID_TASK_TYPES =
[
  "datacollection",
  "categorization",
  "comparison",
  "annotation",
  "polygonannotation",
  "lineannotation",
  "transcription",
  "audiotranscription",
  "pointannotation",
  "segmentannotation"
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, callback_auth_key: nil, default_request_params: {}, logging: false, callback_url: nil) ⇒ Scale

Returns a new instance of Scale.



39
40
41
42
43
44
45
46
# File 'lib/scale.rb', line 39

def initialize(api_key: nil, callback_auth_key: nil, default_request_params: {}, logging: false, callback_url: nil)
  Scale.validate_api_key(api_key)

  self.api_key = api_key
  self.callback_auth_key = callback_auth_key
  self.default_request_params = default_request_params.merge(callback_url: callback_url)
  self.logging = logging
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodId, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scale.rb', line 17

def method_missing(methodId, *args, &block)
  str = methodId.id2name
  match = /^create_([a-z_]+)_task$/.match(str)
  if match
    taskType = match[1].gsub(/[^a-z]/, '')
    taskType = "annotation" if taskType == "imagerecognition"
    if VALID_TASK_TYPES.include?(taskType)
      create_task(match[1], *args)
    else
      raise ArgumentError.new("Method `#{methodId}` doesn't exist.")
    end
  else
    raise ArgumentError.new("Method `#{methodId}` doesn't exist.")
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



2
3
4
# File 'lib/scale.rb', line 2

def api_key
  @api_key
end

#callback_auth_keyObject

Returns the value of attribute callback_auth_key.



2
3
4
# File 'lib/scale.rb', line 2

def callback_auth_key
  @callback_auth_key
end

#default_request_paramsObject

Returns the value of attribute default_request_params.



2
3
4
# File 'lib/scale.rb', line 2

def default_request_params
  @default_request_params
end

#loggingObject

Returns the value of attribute logging.



2
3
4
# File 'lib/scale.rb', line 2

def logging
  @logging
end

Class Method Details

.validate_api_key(api_key) ⇒ Object



33
34
35
36
37
# File 'lib/scale.rb', line 33

def self.validate_api_key(api_key)
  if api_key.length < 5 || !(api_key.start_with?('live') || api_key.start_with?('test')) 
    raise Api::APIKeyInvalid
  end
end

Instance Method Details

#build_callback(params, callback_key: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/scale.rb', line 68

def build_callback(params, callback_key: nil)
  callback = Api::Callback.new(params, callback_key: callback_key, client: client)
  
  if block_given?
    yield callback
  else 
    callback
  end
end

#clientObject



56
57
58
# File 'lib/scale.rb', line 56

def client
  @client ||= Api.new(api_key, callback_auth_key, default_request_params, logging)
end

#create_task(type, args = {}) ⇒ Object



64
65
66
# File 'lib/scale.rb', line 64

def create_task(type, args = {})
  client.create_task(type, args)
end

#live?Boolean Also known as: live, live_mode?

Returns:

  • (Boolean)


48
49
50
# File 'lib/scale.rb', line 48

def live?
  api_key.start_with?('live')
end

#tasksObject



60
61
62
# File 'lib/scale.rb', line 60

def tasks
  @tasks ||= Scale::Api::Tasks.new(client)
end

#test?Boolean Also known as: test, test_mode?

Returns:

  • (Boolean)


52
53
54
# File 'lib/scale.rb', line 52

def test?
  api_key.start_with?('test')
end