Fix versoining number for arvados gem.
[arvados.git] / sdk / ruby / lib / arvados.rb
1 require 'rubygems'
2 require 'google/api_client'
3 require 'active_support/inflector'
4 require 'json'
5
6 ActiveSupport::Inflector.inflections do |inflect|
7   inflect.irregular 'specimen', 'specimens'
8   inflect.irregular 'human', 'humans'
9 end
10
11 module Kernel
12   def suppress_warnings
13     original_verbosity = $VERBOSE
14     $VERBOSE = nil
15     result = yield
16     $VERBOSE = original_verbosity
17     return result
18   end
19 end
20
21 class Arvados
22
23   @@debuglevel = 0
24   class << self
25     attr_accessor :debuglevel
26   end
27
28   def initialize(opts={})
29     @application_version ||= 0.0
30     @application_name ||= File.split($0).last
31
32     @arvados_api_version = opts[:api_version] ||
33       ENV['ARVADOS_API_VERSION'] ||
34       'v1'
35     @arvados_api_host = opts[:api_host] ||
36       ENV['ARVADOS_API_HOST'] or
37       raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
38     @arvados_api_token = opts[:api_token] ||
39       ENV['ARVADOS_API_TOKEN'] or
40       raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
41
42     @suppress_ssl_warnings = opts[:suppress_ssl_warnings] || false
43
44     if @suppress_ssl_warnings
45       suppress_warnings do
46         OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
47       end
48     end
49
50     # Define a class and an Arvados instance method for each Arvados
51     # resource. After this, self.job will return Arvados::Job;
52     # self.job.new() and self.job.find() will do what you want.
53     _arvados = self
54     self.arvados_api.schemas.each do |classname, schema|
55       next if classname.match /List$/
56       klass = Class.new(Arvados::Model) do
57         def self.arvados
58           @arvados
59         end
60         def self.api_models_sym
61           @api_models_sym
62         end
63         def self.api_model_sym
64           @api_model_sym
65         end
66       end
67
68       # Define the resource methods (create, get, update, delete, ...)
69       self.
70         arvados_api.
71         send(classname.underscore.split('/').last.pluralize.to_sym).
72         discovered_methods.
73         collect(&:name).
74         each do |method_name|
75         class << klass; self; end.class_eval do
76           define_method method_name do |*params|
77             self.api_exec(method_name.to_sym, *params)
78           end
79         end
80       end
81
82       # Give the new class access to the API
83       klass.instance_eval do
84         @arvados = _arvados
85         # These should be pulled from the discovery document instead:
86         @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
87         @api_model_sym = classname.underscore.split('/').last.to_sym
88       end
89
90       # This might produce confusing results when using multiple
91       # Arvados instances.
92       Arvados.const_set classname, klass
93
94       self.class.class_eval do
95         define_method classname.underscore do
96           klass
97         end
98       end
99     end
100   end
101
102   class Google::APIClient
103     def discovery_document(api, version)
104       api = api.to_s
105       return @discovery_documents["#{api}:#{version}"] ||=
106         begin
107           response = self.execute!(
108                                    :http_method => :get,
109                                    :uri => self.discovery_uri(api, version),
110                                    :authenticated => false
111                                    )
112           response.body.class == String ? JSON.parse(response.body) : response.body
113         end
114     end
115   end
116
117   def client
118     @client ||= Google::APIClient.
119       new(:host => @arvados_api_host,
120           :application_name => @application_name,
121           :application_version => @application_version.to_s)
122   end
123
124   def arvados_api
125     @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
126   end
127
128   def self.debuglog(message, verbosity=1)
129     $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
130   end
131
132   class Model
133     def self.arvados_api
134       arvados.arvados_api
135     end
136     def self.client
137       arvados.client
138     end
139     def self.debuglog(*args)
140       arvados.class.debuglog *args
141     end
142     def debuglog(*args)
143       self.class.arvados.class.debuglog *args
144     end
145     def self.api_exec(method, parameters={})
146       parameters = parameters.
147         merge(:api_token => ENV['ARVADOS_API_TOKEN'])
148       parameters.each do |k,v|
149         parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
150       end
151       result = client.
152         execute(:api_method => arvados_api.send(api_models_sym).send(method),
153                 :authenticated => false,
154                 :parameters => parameters)
155       JSON.parse result.body, :symbolize_names => true
156     end
157
158     def []=(x,y)
159       @attributes_to_update[x] = y
160       @attributes[x] = y
161     end
162     def [](x)
163       if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
164         # We won't be notified via []= if these change, so we'll just
165         # assume they are going to get changed, and submit them if
166         # save() is called.
167         @attributes_to_update[x] = @attributes[x]
168       end
169       @attributes[x]
170     end
171     def save
172       @attributes_to_update.keys.each do |k|
173         @attributes_to_update[k] = @attributes[k]
174       end
175       j = self.class.api_exec :update, {
176         :uuid => @attributes[:uuid],
177         self.class.api_model_sym => @attributes_to_update.to_json
178       }
179       unless j.is_a? Hash and j[:uuid]
180         debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
181         nil
182       else
183         @attributes_to_update = {}
184         @attributes = j
185       end
186     end
187
188     protected
189
190     def initialize(j)
191       @attributes_to_update = {}
192       @attributes = j
193     end
194   end
195 end