2 require 'google/api_client'
3 require 'active_support/inflector'
6 ActiveSupport::Inflector.inflections do |inflect|
7 inflect.irregular 'specimen', 'specimens'
8 inflect.irregular 'human', 'humans'
13 original_verbosity = $VERBOSE
16 $VERBOSE = original_verbosity
25 attr_accessor :debuglevel
28 def initialize(opts={})
29 @application_version ||= 0.0
30 @application_name ||= File.split($0).last
32 @arvados_api_version = opts[:api_version] ||
33 ENV['ARVADOS_API_VERSION'] ||
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."
42 @suppress_ssl_warnings = opts[:suppress_ssl_warnings] || false
44 if @suppress_ssl_warnings
46 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
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.
54 self.arvados_api.schemas.each do |classname, schema|
55 next if classname.match /List$/
56 klass = Class.new(Arvados::Model) do
60 def self.api_models_sym
63 def self.api_model_sym
68 # Define the resource methods (create, get, update, delete, ...)
71 send(classname.underscore.split('/').last.pluralize.to_sym).
75 class << klass; self; end.class_eval do
76 define_method method_name do |*params|
77 self.api_exec(method_name.to_sym, *params)
82 # Give the new class access to the API
83 klass.instance_eval do
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
90 # This might produce confusing results when using multiple
92 Arvados.const_set classname, klass
94 self.class.class_eval do
95 define_method classname.underscore do
102 class Google::APIClient
103 def discovery_document(api, version)
105 return @discovery_documents["#{api}:#{version}"] ||=
107 response = self.execute!(
108 :http_method => :get,
109 :uri => self.discovery_uri(api, version),
110 :authenticated => false
112 response.body.class == String ? JSON.parse(response.body) : response.body
118 @client ||= Google::APIClient.
119 new(:host => @arvados_api_host,
120 :application_name => @application_name,
121 :application_version => @application_version.to_s)
125 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
128 def self.debuglog(message, verbosity=1)
129 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
139 def self.debuglog(*args)
140 arvados.class.debuglog *args
143 self.class.arvados.class.debuglog *args
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
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
159 @attributes_to_update[x] = y
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
167 @attributes_to_update[x] = @attributes[x]
172 @attributes_to_update.keys.each do |k|
173 @attributes_to_update[k] = @attributes[k]
175 j = self.class.api_exec :update, {
176 :uuid => @attributes[:uuid],
177 self.class.api_model_sym => @attributes_to_update.to_json
179 unless j.is_a? Hash and j[:uuid]
180 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
183 @attributes_to_update = {}
191 @attributes_to_update = {}