Class: OpenCPU::Client

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty, Errors
Defined in:
lib/opencpu/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
11
# File 'lib/opencpu/client.rb', line 8

def initialize
  self.class.base_uri OpenCPU.configuration.endpoint_url
  self.class.default_timeout(OpenCPU.configuration.timeout) unless OpenCPU.configuration.timeout.nil?
end

Instance Method Details

#convert_na_to_nil(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/opencpu/client.rb', line 35

def convert_na_to_nil(data)
  case data
  when 'NA'
    nil
  when Hash
    data.each { |k, v| data[k] = convert_na_to_nil(v) }
  when Array
    data.map! { |v| convert_na_to_nil(v) }
  else
    data
  end
end

#description(package, options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/opencpu/client.rb', line 27

def description(package, options = {})
  user          = options.fetch :user, :system
  github_remote = options.fetch :github_remote, false

  url = "#{package_url(package, user, github_remote)}/info"
  self.class.get(url, request_options(nil, :json))
end

#execute(package, function, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/opencpu/client.rb', line 13

def execute(package, function, options = {})
  user                      = options.fetch :user, :system
  data                      = options.fetch :data, {}
  format                    = options.fetch :format, :json
  github_remote             = options.fetch :github_remote, false
  should_convert_na_to_nil  = options.fetch :convert_na_to_nil, false

  process_query function_url(package, function, user, github_remote, :json), data, format do |response|
    output = JSON.parse(response.body)
    output = convert_na_to_nil(output) if should_convert_na_to_nil
    output
  end
end

#prepare(package, function, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opencpu/client.rb', line 48

def prepare(package, function, options = {})
  user = options.fetch :user, :system
  data = options.fetch :data, {}
  format = options.fetch :format, :json
  github_remote = options.fetch :github_remote, false
  process_query function_url(package, function, user, github_remote), data, format do |response|
    location  = response.headers['location']
    resources = response.body.split(/\n/)
    OpenCPU::DelayedCalculation.new(location, resources)
  end
end