Class: WorkflowRESTClient

Inherits:
Object
  • Object
show all
Includes:
Workflow
Defined in:
lib/rbbt/rest/client.rb,
lib/rbbt/rest/client/get.rb,
lib/rbbt/rest/client/step.rb,
lib/rbbt/rest/client/adaptor.rb

Defined Under Namespace

Classes: RemoteStep

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, name) ⇒ WorkflowRESTClient

Returns a new instance of WorkflowRESTClient.



16
17
18
19
20
# File 'lib/rbbt/rest/client.rb', line 16

def initialize(url, name)
  Log.debug{ "Loading remote workflow #{ name }: #{ url }" }
  @url, @name = url, name
  init_remote_tasks
end

Instance Attribute Details

#asynchronous_exportsObject

Returns the value of attribute asynchronous_exports.



14
15
16
# File 'lib/rbbt/rest/client.rb', line 14

def asynchronous_exports
  @asynchronous_exports
end

#exec_exportsObject

Returns the value of attribute exec_exports.



14
15
16
# File 'lib/rbbt/rest/client.rb', line 14

def exec_exports
  @exec_exports
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/rbbt/rest/client.rb', line 14

def name
  @name
end

#synchronous_exportsObject

Returns the value of attribute synchronous_exports.



14
15
16
# File 'lib/rbbt/rest/client.rb', line 14

def synchronous_exports
  @synchronous_exports
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/rbbt/rest/client.rb', line 14

def url
  @url
end

Class Method Details

.capture_exceptionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbbt/rest/client/get.rb', line 28

def self.capture_exception
  begin
    yield
  rescue Exception => e
    raise e unless e.respond_to? :response
    begin
      klass, message = e.response.to_s.split " => "
      klass = Kernel.const_get klass
      raise klass.new message
    rescue
      raise e
    end
    raise $!
  end
end

.fix_hash(hash, fix_values = false) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rbbt/rest/client/get.rb', line 2

def self.fix_hash(hash, fix_values = false)
  fixed = {}
  hash.each do |key, value|
    fixed[key.to_sym] = case value
                        when TrueClass
                          value
                        when FalseClass
                          value
                        when Hash 
                          fix_hash(value)  
                        when (fix_values and String )
                          value.to_sym
                        when IO
                          value.read
                        when TSV::Dumper
                          value.stream
                        when Step
                          stream = get_stream(value)
                          stream || value.load
                        else
                          value
                        end
  end
  fixed
end

.get_json(url, params = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rbbt/rest/client/get.rb', line 63

def self.get_json(url, params = {})
  Log.debug{ "RestClient get_json: #{ url } - #{Misc.fingerprint params }" }
  params = params.merge({ :_format => 'json' })

  res = capture_exception do
    RestClient.get(URI.encode(url), :params => params)
  end

  begin
    JSON.parse(res)
  rescue
    res
  end
end

.get_raw(url, params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rbbt/rest/client/get.rb', line 44

def self.get_raw(url, params = {})
  Log.debug{ "RestClient get_raw: #{ url } - #{Misc.fingerprint params}" }
  params = params.merge({ :_format => 'raw' })
  capture_exception do
    Misc.insist(2, 0.5) do
      RestClient.get(URI.encode(url), :params => params)
    end
  end
end

.post_jobname(url, params = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rbbt/rest/client/get.rb', line 54

def self.post_jobname(url, params = {})
  Log.debug{ "RestClient post_jobname: #{ url } - #{Misc.fingerprint params}" }
  params = params.merge({ :_format => 'jobname' })

  capture_exception do
    RestClient.post(URI.encode(url), params)
  end
end

.post_json(url, params = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbbt/rest/client/get.rb', line 78

def self.post_json(url, params = {})
  if url =~ /_cache_type=:exec/
    JSON.parse(Open.open(url, :nocache => true))
  else
    params = params.merge({ :_format => 'json' })

    res = capture_exception do
      RestClient.post(URI.encode(url), params)
    end

    begin
      JSON.parse(res)
    rescue
      res
    end
  end
end

Instance Method Details

#documentationObject



7
8
9
# File 'lib/rbbt/rest/client/adaptor.rb', line 7

def documentation
  @documention ||= IndiferentHash.setup(WorkflowRESTClient.get_json(File.join(url, "documentation"),{}))
end

#exported_tasksObject



29
30
31
# File 'lib/rbbt/rest/client/adaptor.rb', line 29

def exported_tasks
  (@asynchronous_exports  + @synchronous_exports + @exec_exports).compact.flatten
end

#init_remote_tasksObject



59
60
61
62
63
64
65
# File 'lib/rbbt/rest/client/adaptor.rb', line 59

def init_remote_tasks
  task_exports = WorkflowRESTClient.get_json(url)
  @asynchronous_exports = task_exports["asynchronous"].collect{|task| task.to_sym }
  @synchronous_exports = task_exports["synchronous"].collect{|task| task.to_sym }
  @exec_exports = task_exports["exec"].collect{|task| task.to_sym }
  nil
end

#job(task, name, inputs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbbt/rest/client.rb', line 26

def job(task, name, inputs)
  task_info = task_info(task)
  fixed_inputs = {}
  inputs.each do |k,v| 
    fixed_inputs[k] = case v
                      when TSV
                        v.to_s
                      else
                        v
                      end
  end

  RemoteStep.new(url, task, name, fixed_inputs, task_info[:result_type], task_info[:result_description], @exec_exports.include?(task))
end

#load_id(id) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rbbt/rest/client.rb', line 41

def load_id(id)
  task, name = id.split("/")
  step = RemoteStep.new url, task, nil
  step.name = name
  step.result_type = task_info(task)[:result_type]
  step.result_description = task_info(task)[:result_description]
  step
end

#load_tasksObject



44
45
46
47
# File 'lib/rbbt/rest/client/adaptor.rb', line 44

def load_tasks
  exported_tasks.each{|name| tasks[name]}
  nil
end

#task_dependenciesObject



49
50
51
52
53
54
55
56
57
# File 'lib/rbbt/rest/client/adaptor.rb', line 49

def task_dependencies
  @task_dependencies ||= Hash.new do |hash,task| 
    hash[task] = if exported_tasks.include? task
      WorkflowRESTClient.get_json(File.join(url, task.to_s, 'dependencies'))
    else
      []
    end
  end
end

#task_info(task) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbbt/rest/client/adaptor.rb', line 11

def task_info(task)
  @task_info ||= {}
  @task_info[task]
  
  if @task_info[task].nil?
    task_info = WorkflowRESTClient.get_json(File.join(url, task.to_s, 'info'))
    task_info = WorkflowRESTClient.fix_hash(task_info)

    task_info[:result_type] = task_info[:result_type].to_sym
    task_info[:export] = task_info[:export].to_sym
    task_info[:input_types] = WorkflowRESTClient.fix_hash(task_info[:input_types], true)
    task_info[:inputs] = task_info[:inputs].collect{|input| input.to_sym }

    @task_info[task] = task_info
  end
  @task_info[task]
end

#tasksObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/rbbt/rest/client/adaptor.rb', line 33

def tasks
  @tasks ||= Hash.new do |hash,task_name| 
    info = task_info(task_name)
    task = Task.setup info do |*args|
      raise "This is a remote task" 
    end
    task.name = task_name.to_sym
    hash[task_name] = task
  end
end

#to_sObject



22
23
24
# File 'lib/rbbt/rest/client.rb', line 22

def to_s
  name
end

#workflow_descriptionObject



3
4
5
# File 'lib/rbbt/rest/client/adaptor.rb', line 3

def workflow_description
  WorkflowRESTClient.get_raw(File.join(url, 'description'))
end