solvebio 1.6.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.bumpversion.cfg +6 -0
- data/.gitignore +5 -4
- data/.travis.yml +1 -1
- data/Gemfile +3 -0
- data/README.md +34 -34
- data/Rakefile +1 -18
- data/bin/solvebio.rb +14 -16
- data/installer +64 -0
- data/lib/solvebio.rb +50 -11
- data/lib/solvebio/acccount.rb +4 -0
- data/lib/solvebio/annotation.rb +11 -0
- data/lib/solvebio/api_operations.rb +147 -0
- data/lib/solvebio/api_resource.rb +32 -0
- data/lib/solvebio/cli.rb +75 -0
- data/lib/solvebio/cli/auth.rb +106 -0
- data/lib/solvebio/cli/credentials.rb +54 -0
- data/lib/{cli → solvebio/cli}/irb.rb +0 -23
- data/lib/solvebio/cli/irbrc.rb +48 -0
- data/lib/solvebio/cli/tutorial.rb +12 -0
- data/lib/solvebio/client.rb +149 -0
- data/lib/solvebio/dataset.rb +60 -0
- data/lib/solvebio/dataset_field.rb +12 -0
- data/lib/solvebio/depository.rb +38 -0
- data/lib/solvebio/depository_version.rb +40 -0
- data/lib/solvebio/errors.rb +64 -0
- data/lib/solvebio/filter.rb +315 -0
- data/lib/solvebio/list_object.rb +73 -0
- data/lib/solvebio/locale.rb +43 -0
- data/lib/solvebio/query.rb +341 -0
- data/lib/solvebio/sample.rb +54 -0
- data/lib/solvebio/singleton_api_resource.rb +25 -0
- data/lib/solvebio/solve_object.rb +164 -0
- data/lib/solvebio/tabulate.rb +589 -0
- data/lib/solvebio/user.rb +4 -0
- data/lib/solvebio/util.rb +59 -0
- data/lib/solvebio/version.rb +3 -0
- data/solvebio.gemspec +10 -18
- data/test/helper.rb +6 -2
- data/test/solvebio/data/.gitignore +1 -0
- data/test/solvebio/data/.netrc +6 -0
- data/test/{data → solvebio/data}/netrc-save +0 -0
- data/test/solvebio/data/sample.vcf.gz +0 -0
- data/test/solvebio/data/test_creds +3 -0
- data/test/solvebio/test_annotation.rb +45 -0
- data/test/solvebio/test_client.rb +29 -0
- data/test/solvebio/test_conversion.rb +14 -0
- data/test/solvebio/test_credentials.rb +67 -0
- data/test/solvebio/test_dataset.rb +52 -0
- data/test/solvebio/test_depository.rb +24 -0
- data/test/solvebio/test_depositoryversion.rb +22 -0
- data/test/solvebio/test_error.rb +31 -0
- data/test/solvebio/test_filter.rb +86 -0
- data/test/solvebio/test_query.rb +282 -0
- data/test/solvebio/test_query_batch.rb +38 -0
- data/test/solvebio/test_query_init.rb +30 -0
- data/test/solvebio/test_query_tabulate.rb +73 -0
- data/test/solvebio/test_ratelimit.rb +31 -0
- data/test/solvebio/test_resource.rb +29 -0
- data/test/solvebio/test_sample_access.rb +60 -0
- data/test/solvebio/test_sample_download.rb +20 -0
- data/test/solvebio/test_tabulate.rb +129 -0
- data/test/solvebio/test_util.rb +39 -0
- metadata +100 -85
- data/Makefile +0 -17
- data/demo/README.md +0 -14
- data/demo/cheatsheet.rb +0 -31
- data/demo/dataset/facets.rb +0 -13
- data/demo/dataset/field.rb +0 -13
- data/demo/depository/README.md +0 -24
- data/demo/depository/all.rb +0 -13
- data/demo/depository/retrieve.rb +0 -13
- data/demo/depository/versions-all.rb +0 -13
- data/demo/query/query-filter.rb +0 -30
- data/demo/query/query.rb +0 -13
- data/demo/query/range-filter.rb +0 -18
- data/demo/test-api.rb +0 -98
- data/lib/cli/auth.rb +0 -122
- data/lib/cli/help.rb +0 -13
- data/lib/cli/irbrc.rb +0 -54
- data/lib/cli/options.rb +0 -75
- data/lib/client.rb +0 -154
- data/lib/credentials.rb +0 -67
- data/lib/errors.rb +0 -81
- data/lib/filter.rb +0 -312
- data/lib/locale.rb +0 -47
- data/lib/main.rb +0 -46
- data/lib/query.rb +0 -414
- data/lib/resource/annotation.rb +0 -23
- data/lib/resource/apiresource.rb +0 -241
- data/lib/resource/dataset.rb +0 -91
- data/lib/resource/datasetfield.rb +0 -37
- data/lib/resource/depository.rb +0 -50
- data/lib/resource/depositoryversion.rb +0 -69
- data/lib/resource/main.rb +0 -123
- data/lib/resource/sample.rb +0 -75
- data/lib/resource/solveobject.rb +0 -122
- data/lib/resource/user.rb +0 -5
- data/lib/tabulate.rb +0 -706
- data/lib/util.rb +0 -29
- data/test/Makefile +0 -9
- data/test/data/sample.vcf.gz +0 -0
- data/test/test-annotation.rb +0 -46
- data/test/test-auth.rb +0 -58
- data/test/test-client.rb +0 -27
- data/test/test-conversion.rb +0 -13
- data/test/test-dataset.rb +0 -42
- data/test/test-depository.rb +0 -35
- data/test/test-error.rb +0 -36
- data/test/test-filter.rb +0 -70
- data/test/test-netrc.rb +0 -52
- data/test/test-query-batch.rb +0 -40
- data/test/test-query-init.rb +0 -29
- data/test/test-query-paging.rb +0 -102
- data/test/test-query.rb +0 -71
- data/test/test-resource.rb +0 -40
- data/test/test-sample-access.rb +0 -59
- data/test/test-sample-download.rb +0 -20
- data/test/test-tabulate.rb +0 -131
- data/test/test-util.rb +0 -42
data/lib/cli/help.rb
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
module SolveBio
|
|
2
|
-
# Shell help
|
|
3
|
-
def help
|
|
4
|
-
puts <<-HELP
|
|
5
|
-
Constants SAMPLE_DEPO, SAMPLE_DEPO_VERSION, and SAMPLE_DATASET are
|
|
6
|
-
available for and example depository, depository_versions or dataset.
|
|
7
|
-
|
|
8
|
-
By setting environment variable SOLVEBIO_IRBRC, you can add your own
|
|
9
|
-
custom irb profile.
|
|
10
|
-
HELP
|
|
11
|
-
end
|
|
12
|
-
module_function :help
|
|
13
|
-
end
|
data/lib/cli/irbrc.rb
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# -*- ruby -*-
|
|
2
|
-
# irbrc profile for SolveBio
|
|
3
|
-
IRB.conf[:PROMPT_MODE] = :SIMPLE
|
|
4
|
-
IRB.conf[:PROMPT][:SIMPLE] = {
|
|
5
|
-
:PROMPT_C => '[SolveBio] In ?: ', # Prompt when continuing a statement
|
|
6
|
-
:PROMPT_I => '[SolveBio] In : ', # Normal prompt
|
|
7
|
-
:PROMPT_N => '[SolveBio] In +: ', # Prompt when indenting code
|
|
8
|
-
:PROMPT_S => '[SolveBio] In %l: ', # Prompt when continuing a string
|
|
9
|
-
:RETURN => "[SolveBio] Out : %s\n"
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
require_relative '../solvebio'
|
|
13
|
-
require_relative '../resource/apiresource'
|
|
14
|
-
include SolveBio::Auth
|
|
15
|
-
|
|
16
|
-
# Set some demo names that can be used.
|
|
17
|
-
SAMPLE_DEPO = 'ClinVar'
|
|
18
|
-
SAMPLE_DEPO_VERSION = "#{SAMPLE_DEPO}/2.0.0-1"
|
|
19
|
-
SAMPLE_DATASET = "#{SAMPLE_DEPO_VERSION}/Variants"
|
|
20
|
-
|
|
21
|
-
have_completion = nil
|
|
22
|
-
begin
|
|
23
|
-
require 'bond' and require 'bond/completion'
|
|
24
|
-
have_completion = 'bond'
|
|
25
|
-
rescue LoadError
|
|
26
|
-
begin
|
|
27
|
-
have_completion = require 'irb/completion'
|
|
28
|
-
rescue LoadError
|
|
29
|
-
have_completion = false
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
'irb/completion'
|
|
33
|
-
'bond' 'bond/completion'
|
|
34
|
-
|
|
35
|
-
puts <<-INTRO
|
|
36
|
-
You are in a SolveBio Interactive Ruby (irb) session...
|
|
37
|
-
Type SolveBio::help for help on SolveBio.
|
|
38
|
-
INTRO
|
|
39
|
-
|
|
40
|
-
unless have_completion
|
|
41
|
-
if have_completion != 'bond'
|
|
42
|
-
puts "You might get better completion using the 'bond' gem"
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Report whether we are logged in.
|
|
47
|
-
include SolveBio::Credentials
|
|
48
|
-
creds = get_credentials()
|
|
49
|
-
if creds
|
|
50
|
-
puts "You are logged in as #{creds[0]}"
|
|
51
|
-
else
|
|
52
|
-
puts 'You are not logged in yet. Login using "login [email]"'
|
|
53
|
-
|
|
54
|
-
end
|
data/lib/cli/options.rb
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
|
|
4
|
-
# SolveBio Ruby command-line option processing
|
|
5
|
-
|
|
6
|
-
require 'optparse'
|
|
7
|
-
require_relative '../client'
|
|
8
|
-
|
|
9
|
-
module SolveBio::CLIOptions
|
|
10
|
-
|
|
11
|
-
PROGRAM = 'solvebio.rb'
|
|
12
|
-
|
|
13
|
-
def show_version
|
|
14
|
-
"#{PROGRAM}, version #{SolveBio::VERSION}"
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Main parser for the SolveBio command line client
|
|
18
|
-
def setup_options(options, stdout=$stdout, stderr=$stderr)
|
|
19
|
-
|
|
20
|
-
OptionParser.new do |opts|
|
|
21
|
-
opts.banner = "Usage: solvebio.rb [options] <command> [<args>]"
|
|
22
|
-
opts.on_tail('-v', '--version',
|
|
23
|
-
'print the version') do
|
|
24
|
-
options[:version] = true
|
|
25
|
-
stdout.puts "#{PROGRAM}, version #{SolveBio::VERSION}"
|
|
26
|
-
exit 0
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
opts.on('--api-host NAME', String,
|
|
30
|
-
'Override the default SolveBio API host') do
|
|
31
|
-
|api_host|
|
|
32
|
-
options[:api_host] = api_host
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
opts.on('--api-key STRING', String,
|
|
36
|
-
'Manually provide a SolveBio API key') do
|
|
37
|
-
|api_key|
|
|
38
|
-
options[:api_key] = api_key
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
opts.on('-h', '--help', 'Display this screen') do
|
|
42
|
-
puts opts
|
|
43
|
-
puts <<-EOH
|
|
44
|
-
|
|
45
|
-
SolveBio Commands:
|
|
46
|
-
login [email] Login and save credentials. Use email if provided.
|
|
47
|
-
logout Logout and delete saved credentials
|
|
48
|
-
whoami Show your SolveBio email address
|
|
49
|
-
shell Open a SolveBio IRB shell
|
|
50
|
-
test Make sure the SolveBio API is working correctly
|
|
51
|
-
EOH
|
|
52
|
-
exit
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def process_options(argv)
|
|
60
|
-
options = {}
|
|
61
|
-
args = setup_options(options)
|
|
62
|
-
rest = args.parse argv
|
|
63
|
-
|
|
64
|
-
SolveBio::Client.client.api_host = options[:api_host] if
|
|
65
|
-
options[:api_host]
|
|
66
|
-
SolveBio::Client.client.api_key = options[:api_key] if
|
|
67
|
-
options[:api_key]
|
|
68
|
-
return options, rest, args
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
if __FILE__ == $0
|
|
72
|
-
include SolveBio::CLIOptions
|
|
73
|
-
options, rest, parser = process_options(ARGV)
|
|
74
|
-
p options, rest, parser
|
|
75
|
-
end
|
data/lib/client.rb
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
require 'openssl'
|
|
4
|
-
require 'rest_client'
|
|
5
|
-
require 'json'
|
|
6
|
-
require 'addressable/uri'
|
|
7
|
-
require_relative 'credentials'
|
|
8
|
-
require_relative 'errors'
|
|
9
|
-
|
|
10
|
-
# A requests-based HTTP client for SolveBio API resources
|
|
11
|
-
class SolveBio::Client
|
|
12
|
-
|
|
13
|
-
attr_reader :headers, :api_host
|
|
14
|
-
attr_accessor :api_key
|
|
15
|
-
|
|
16
|
-
# Add our own kind of Authorization tokens. This has to be
|
|
17
|
-
# done this way, late, because the rest-client gem looks for
|
|
18
|
-
# .netrc and will set basic authentication if it finds a match.
|
|
19
|
-
RestClient.add_before_execution_proc do | req, args |
|
|
20
|
-
if args[:authorization]
|
|
21
|
-
req.instance_variable_get('@header')['authorization'] = [args[:authorization]]
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def initialize(api_key=nil, api_host=nil)
|
|
26
|
-
@api_key = api_key || SolveBio::api_key
|
|
27
|
-
SolveBio::api_key ||= api_key
|
|
28
|
-
@api_host = api_host || SolveBio::API_HOST
|
|
29
|
-
|
|
30
|
-
# Mirroring comments from:
|
|
31
|
-
# http://ruby-doc.org/stdlib-2.1.2/libdoc/net/http/rdoc/Net/HTTP.html
|
|
32
|
-
# gzip compression is used in preference to deflate
|
|
33
|
-
# compression, which is used in preference to no compression.
|
|
34
|
-
@headers = {
|
|
35
|
-
:content_type => :json,
|
|
36
|
-
:accept => :json,
|
|
37
|
-
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
|
38
|
-
'User-Agent' => 'SolveBio Ruby Client %s [%s/%s]' % [
|
|
39
|
-
SolveBio::VERSION,
|
|
40
|
-
SolveBio::RUBY_IMPLEMENTATION,
|
|
41
|
-
SolveBio::RUBY_VERSION
|
|
42
|
-
]
|
|
43
|
-
}
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
DEFAULT_REQUEST_OPTS = {
|
|
47
|
-
:raw => false,
|
|
48
|
-
:default_headers => true
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
# Issues an HTTP GET across the wire via the Ruby 'rest-client'
|
|
52
|
-
# library. See *request()* for information on opts.
|
|
53
|
-
def get(url, opts={})
|
|
54
|
-
request('get', url, opts)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Issues an HTTP POST across the wire via the Ruby 'rest-client'
|
|
58
|
-
# library. See *request* for information on opts.
|
|
59
|
-
def post(url, data, opts={})
|
|
60
|
-
opts[:payload] =
|
|
61
|
-
if opts.member?(:no_json)
|
|
62
|
-
data
|
|
63
|
-
else
|
|
64
|
-
data.to_json
|
|
65
|
-
end
|
|
66
|
-
request('post', url, opts)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Issues an HTTP Request across the wire via the Ruby 'rest-client'
|
|
70
|
-
# library.
|
|
71
|
-
def request(method, url, opts={})
|
|
72
|
-
|
|
73
|
-
opts = DEFAULT_REQUEST_OPTS.merge(opts)
|
|
74
|
-
|
|
75
|
-
# Expand URL with API host if none was given
|
|
76
|
-
api_host = @api_host or SolveBio::API_HOST
|
|
77
|
-
|
|
78
|
-
if not api_host
|
|
79
|
-
raise SolveBio::Error.new('No SolveBio API host is set')
|
|
80
|
-
elsif not url.start_with?(api_host)
|
|
81
|
-
url = Addressable::URI.join(api_host, url).to_s
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
# Handle some default options and add authorization header
|
|
85
|
-
if opts[:default_headers] and @api_key
|
|
86
|
-
headers = @headers.merge(opts[:headers]||{})
|
|
87
|
-
authorization = "Token #{@api_key}"
|
|
88
|
-
else
|
|
89
|
-
headers = nil
|
|
90
|
-
authorization = nil
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
SolveBio::logger.debug('API %s Request: %s' % [method.upcase, url])
|
|
94
|
-
|
|
95
|
-
response = nil
|
|
96
|
-
RestClient::Request.
|
|
97
|
-
execute(:method => method,
|
|
98
|
-
:url => url,
|
|
99
|
-
:headers => headers,
|
|
100
|
-
:authorization => authorization,
|
|
101
|
-
:timeout => opts[:timeout] || 80,
|
|
102
|
-
:payload => opts[:payload]) do
|
|
103
|
-
|resp, request, result, &block|
|
|
104
|
-
response = resp
|
|
105
|
-
if response.code < 200 or response.code >= 400
|
|
106
|
-
self.handle_api_error(result)
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
response = JSON.parse(response) unless opts[:raw]
|
|
111
|
-
response
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
def self.handle_request_error(e)
|
|
115
|
-
# FIXME: go over this. It is still a rough translation
|
|
116
|
-
# from the python.
|
|
117
|
-
err = e.inspect
|
|
118
|
-
if e.kind_of?(requests.exceptions.RequestException)
|
|
119
|
-
msg = SolveBio::Error::Default_message
|
|
120
|
-
else
|
|
121
|
-
msg = "Unexpected error communicating with SolveBio.\n" +
|
|
122
|
-
"It looks like there's probably a configuration " +
|
|
123
|
-
'issue locally.\nIf this problem persists, let us ' +
|
|
124
|
-
'know at contact@solvebio.com.'
|
|
125
|
-
end
|
|
126
|
-
msg = msg + "\n\n(Network error: #{err}"
|
|
127
|
-
raise SolveBio::Error.new(nil, msg)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
# SolveBio's API error handler returns a SolveBio::Error. The
|
|
131
|
-
# *response* parameter is a (subclass) of Net::HTTPResponse.
|
|
132
|
-
def handle_api_error(response)
|
|
133
|
-
SolveBio::logger.info("API Error: #{response.msg}") unless
|
|
134
|
-
[400, 401, 403, 404].member?(response.code.to_i)
|
|
135
|
-
raise SolveBio::Error.new(response)
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
def self.client
|
|
139
|
-
@@client ||= SolveBio::Client.new()
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
def self.get(*args)
|
|
143
|
-
client.get(*args)
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def self.post(*args)
|
|
147
|
-
client.post(*args)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
def self.request(*args)
|
|
151
|
-
client.request(*args)
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
end
|
data/lib/credentials.rb
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# Deals with reading netrc credentials
|
|
4
|
-
require_relative 'main'
|
|
5
|
-
require 'netrc'
|
|
6
|
-
require 'addressable/uri'
|
|
7
|
-
|
|
8
|
-
#
|
|
9
|
-
# Raised if the credentials are not found.
|
|
10
|
-
#
|
|
11
|
-
class CredentialsError < RuntimeError
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
module SolveBio::Credentials
|
|
15
|
-
|
|
16
|
-
module_function
|
|
17
|
-
|
|
18
|
-
# SolveBio API host -- just the hostname
|
|
19
|
-
def api_host
|
|
20
|
-
Addressable::URI.parse(SolveBio::API_HOST).host
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def netrc_path
|
|
24
|
-
path =
|
|
25
|
-
if ENV['NETRC_PATH']
|
|
26
|
-
File.join(ENV['NETRC_PATH'], ".netrc")
|
|
27
|
-
else
|
|
28
|
-
Netrc.default_path
|
|
29
|
-
end
|
|
30
|
-
if not File.exist?(path)
|
|
31
|
-
raise IOError, "netrc file #{path} not found"
|
|
32
|
-
end
|
|
33
|
-
path
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
#
|
|
37
|
-
# Returns the tuple user / password given a path for the .netrc file.
|
|
38
|
-
# Raises CredentialsError if no valid netrc file is found.
|
|
39
|
-
#
|
|
40
|
-
def get_credentials
|
|
41
|
-
n = Netrc.read(netrc_path)
|
|
42
|
-
return n[api_host]
|
|
43
|
-
rescue Netrc::Error => e
|
|
44
|
-
raise CredentialsError, "Could not read .netrc file: #{e}"
|
|
45
|
-
end
|
|
46
|
-
module_function :get_credentials
|
|
47
|
-
|
|
48
|
-
def delete_credentials
|
|
49
|
-
n = Netrc.read(netrc_path)
|
|
50
|
-
n.delete(api_host)
|
|
51
|
-
n.save
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def save_credentials(email, api_key)
|
|
55
|
-
n = Netrc.read(netrc_path)
|
|
56
|
-
# Overwrites any existing credentials
|
|
57
|
-
n[api_host] = email, api_key
|
|
58
|
-
n.save
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Demo code
|
|
63
|
-
if __FILE__ == $0
|
|
64
|
-
include SolveBio::Credentials
|
|
65
|
-
puts "authentication: #{netrc_path}"
|
|
66
|
-
puts "creds", get_credentials
|
|
67
|
-
end
|
data/lib/errors.rb
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
require_relative 'main'
|
|
4
|
-
|
|
5
|
-
class SolveBio::Error < RuntimeError
|
|
6
|
-
Default_message =
|
|
7
|
-
'Unexpected error communicating with SolveBio. ' +
|
|
8
|
-
'If this problem persists, let us know at ' +
|
|
9
|
-
'contact@solvebio.com.'
|
|
10
|
-
|
|
11
|
-
attr_reader :json_body
|
|
12
|
-
attr_reader :status_code
|
|
13
|
-
attr_reader :message
|
|
14
|
-
attr_reader :field_errors
|
|
15
|
-
|
|
16
|
-
def initialize( response=nil, message=nil)
|
|
17
|
-
@json_body = nil
|
|
18
|
-
@status_code = nil
|
|
19
|
-
@message = message or Default_message
|
|
20
|
-
@field_errors = []
|
|
21
|
-
|
|
22
|
-
if response
|
|
23
|
-
@status_code = response.code.to_i
|
|
24
|
-
@message = response.message
|
|
25
|
-
begin
|
|
26
|
-
@json_body = JSON.parse(response.body)
|
|
27
|
-
rescue
|
|
28
|
-
@message = '404 Not Found.' if @status_code == 404
|
|
29
|
-
SolveBio.logger.debug(
|
|
30
|
-
"API Response (%d): No content." % @status_code)
|
|
31
|
-
else
|
|
32
|
-
SolveBio.logger.debug(
|
|
33
|
-
"API Response (#{@status_code}): #{@json_body}")
|
|
34
|
-
|
|
35
|
-
if [400, 401, 403, 404].member?(@status_code)
|
|
36
|
-
@message = 'Bad request.'
|
|
37
|
-
|
|
38
|
-
if @json_body.member?('detail')
|
|
39
|
-
@message = '%s' % @json_body['detail']
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
if @json_body.member?('non_field_errors')
|
|
43
|
-
@message = '%s.' % \
|
|
44
|
-
@json_body['non_field_errors'].join(', ')
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
@json_body.each do |k, v|
|
|
48
|
-
unless ['detail', 'non_field_errors'].member?(k)
|
|
49
|
-
v = v.join(', ') if v.kind_of?(Array)
|
|
50
|
-
@field_errors << ('%s (%s)' % [k, v])
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
unless @field_errors.empty?
|
|
55
|
-
@message += (' The following fields were missing ' +
|
|
56
|
-
'or invalid: %s' %
|
|
57
|
-
@field_errors.join(', '))
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
self
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def to_s
|
|
66
|
-
@message
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
# Demo code
|
|
71
|
-
if __FILE__ == $0
|
|
72
|
-
puts SolveBio::Error.new
|
|
73
|
-
puts SolveBio::Error.new(nil, 'Hi there').inspect
|
|
74
|
-
puts SolveBio::Error.new(nil, 'Hi there').to_s
|
|
75
|
-
puts SolveBio::Error.new(nil, ['Hello, ', 'again.']).inspect
|
|
76
|
-
|
|
77
|
-
require 'net/http'
|
|
78
|
-
response = Net::HTTPUnauthorized.new('HTTP 1.1', '404', 'No creds')
|
|
79
|
-
puts SolveBio::Error.new(response).to_s
|
|
80
|
-
|
|
81
|
-
end
|