Class: RbbtRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/workflow/rest/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, name) ⇒ RbbtRestClient

Returns a new instance of RbbtRestClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rbbt/workflow/rest/client.rb', line 13

def initialize(url, name)
  @url = url
  @name = name
  task_names = JSON.parse(RestClient.get(@url + '?_format=json'))

  extend Workflow

  class << self 
    alias old_job job

    def job(task_name, name, *args)
      step = old_job(task_name, name, *args)

      class << step
        attr_accessor :remote_name
        attr_accessor :original_name
        attr_accessor :remote_export

        def remote_name
          URI.escape(@remote_name)
        end
      end

      step.original_name = name || "RestClient"

      step.remote_export = task_info(task_name)[:export]

      class << step
        def info
          if remote_name.nil?
            {:status => :done}
          else
            i = JSON.parse(RestClient.get(File.join(task.url, task.name.to_s, remote_name, 'info') + '?_format=json'))
            IndiferentHash.setup i
            i[:status] = i.delete(:status).to_sym if i.include? :status
            i[:messages] = i.delete(:messages) if i.include? :messages
            i
          end
        end

        def load
          case task.result_type.to_sym
          when :string
            RestClient.get(File.join(task.url, task.name.to_s, remote_name) + "?_format=raw")
          when :tsv
            TSV.open(StringIO.new(RestClient.get(File.join(task.url, task.name.to_s, remote_name) + "?_format=raw")))
          when :annotations
            Annotated.load_tsv(TSV.open(StringIO.new(RestClient.get(File.join(task.url, task.name.to_s, remote_name) + "?_format=raw"))))
          else
            res = JSON.parse(RestClient.get(File.join(task.url, task.name.to_s, remote_name) + "?_format=json"))
            res = TSV.setup(res) if task.result_type.to_sym == :tsv
            res
          end
        end

        def exec
          input_hash = Misc.zip2hash(task.inputs, @inputs)

          case remote_export.to_s 
          when "exec"
            url = File.join(task.url, task.name.to_s)
            if task.result_type.to_sym == :string
              res = RestClient.post(url, Misc.hash2GET_params(input_hash.merge(:_format => :raw)))
              res
            else
              res = RestClient.post(url, Misc.hash2GET_params(input_hash.merge(:_format => :json)))
              JSON.parse(res)
            end
          when "synchronous"
            @remote_name = RestClient.post(File.join(task.url, task.name.to_s), Misc.hash2GET_params(input_hash.merge(:_format => :json, :jobname => original_name)))
            self.load
          when "asynchronous"
            @remote_name = RestClient.post(File.join(task.url, task.name.to_s), Misc.hash2GET_params(input_hash.merge(:_format => :json, :jobname => original_name)))
            while not done?
              sleep 10
            end
            self.load
          end
        end

        def run
          exec
        end

        def fork
          input_hash = Misc.zip2hash(task.inputs, @inputs)

          case remote_export.to_s 
          when "exec"
            raise "Cannot export a remote function exported as 'exec'"
          when "synchronous"
            @remote_name = RestClient.post(File.join(task.url, task.name.to_s), Misc.hash2GET_params(input_hash.merge(:_format => :json, :jobname =>  original_name)))
            self
          when "asynchronous"
            @remote_name = RestClient.post(File.join(task.url, task.name.to_s), Misc.hash2GET_params(input_hash.merge(:_format => :json, :jobname =>  original_name)))
            self
          end
        end

        def clean
          return if remote_export.to_s == "exec"
          RestClient.delete(File.join(task.url, task.name.to_s, remote_name) + '?_format=json')
        end

        def name
          remote_name || @name
        end

      end

      step
    end
  end

  task_names.each do |task_name|
    task_info = JSON.parse(RestClient.get(File.join(@url, task_name, 'info') + '?_format=json'))
    IndiferentHash.setup(task_info)

    desc task_info[:description] if task_info.include? :description

    task_info[:inputs].each do |input_name|
      input_info = [input_name.to_sym] 
      IndiferentHash.setup(input_info)
      input_info << task_info[:input_types][input_name].to_sym if task_info[:input_types].include? input_name
      input_info << task_info[:input_descriptions][input_name] if task_info[:input_descriptions].include? input_name
      input_info << task_info[:input_defaults][input_name] if task_info[:input_defaults].include? input_name
      self.input *input_info
    end

    case task_info[:export]
    when "exec"
      export_exec task_name.to_sym
    when "synchronous"
      export_synchronous task_name.to_sym
    when "asynchronous"
      export_asynchronous task_name.to_sym
    end

    task task_name => task_info[:result_type] do |*inputs|
      raise "Placeholder, this should not execute"
    end
  end

  tasks.each do |name, task|
    class << task
      attr_accessor :url
    end
    task.url = @url
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/rbbt/workflow/rest/client.rb', line 8

def name
  @name
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/rbbt/workflow/rest/client.rb', line 8

def url
  @url
end

Instance Method Details

#to_sObject



9
10
11
# File 'lib/rbbt/workflow/rest/client.rb', line 9

def to_s
  @name
end