Module: Monkeylearn::Extractors

Extended by:
Requests
Defined in:
lib/monkeylearn/extractors.rb

Class Method Summary collapse

Methods included from Requests

get_connection, get_exception_class, raise_for_status, request, throttled?

Class Method Details

.build_endpoint(*args) ⇒ Object



15
16
17
# File 'lib/monkeylearn/extractors.rb', line 15

def build_endpoint(*args)
  File.join('extractors', *args) + '/'
end

.detail(module_id) ⇒ Object



65
66
67
# File 'lib/monkeylearn/extractors.rb', line 65

def detail(module_id)
  request(:get, build_endpoint(module_id))
end

.extract(module_id, data, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/monkeylearn/extractors.rb', line 27

def extract(module_id, data, options = {})
  options[:batch_size] ||= Monkeylearn::Defaults.default_batch_size
  batch_size = options[:batch_size]
  validate_batch_size batch_size

  endpoint = build_endpoint(module_id, 'extract')

  if Monkeylearn.auto_batch
    responses = (0...data.length).step(batch_size).collect do |start_idx|
      sliced_data = {data: data.slice(start_idx, batch_size)}
      if options.key? :production_model
        sliced_data[:production_model] = options[:production_model]
      end
      request(:post, endpoint, sliced_data)
    end
    return Monkeylearn::MultiResponse.new(responses)
  else
    body = {data: data}
    if options.key? :production_model
        body[:production_model] = options[:production_model]
    end
    return request(:post, endpoint, body)
  end

end

.list(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/monkeylearn/extractors.rb', line 53

def list(options = {})
  if options.key?(:order_by)
    options[:order_by] = validate_order_by_param(options[:order_by])
  end
  query_params = {
    page: options[:page],
    per_page: options[:per_page],
    order_by: options[:order_by]
  }.delete_if { |k,v| v.nil? }
  request(:get, build_endpoint, nil, query_params)
end

.validate_batch_size(batch_size) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/monkeylearn/extractors.rb', line 19

def validate_batch_size(batch_size)
  max_size = Monkeylearn::Defaults.max_batch_size
  if batch_size >  max_size
    raise MonkeylearnError, "The param batch_size is too big, max value is #{max_size}."
  end
  true
end