phenoscaperb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ require 'net/http'
2
+
3
+ NETWORKABLE_EXCEPTIONS = [Faraday::Error::ClientError,
4
+ URI::InvalidURIError,
5
+ Encoding::UndefinedConversionError,
6
+ ArgumentError,
7
+ NoMethodError,
8
+ TypeError]
@@ -0,0 +1,22 @@
1
+ module Phenoscape
2
+ # Custom error class for rescuing from all Phenoscape errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Phenoscape returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Phenoscape returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when Phenoscape returns the HTTP status code 500
12
+ class InternalServerError < Error; end
13
+
14
+ # Raised when Phenoscape returns the HTTP status code 502
15
+ class BadGateway < Error; end
16
+
17
+ # Raised when Phenoscape returns the HTTP status code 503
18
+ class ServiceUnavailable < Error; end
19
+
20
+ # Raised when Phenoscape returns the HTTP status code 504
21
+ class GatewayTimeout < Error; end
22
+ end
@@ -0,0 +1,71 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ # @private
5
+ module FaradayMiddleware
6
+ # @private
7
+ class RaiseHttpException < Faraday::Middleware
8
+ def call(env)
9
+ @app.call(env).on_complete do |response|
10
+ case response[:status].to_i
11
+ when 400
12
+ raise Phenoscape::BadRequest, error_message_400(response)
13
+ when 404
14
+ raise Phenoscape::NotFound, error_message_400(response)
15
+ when 500
16
+ raise Phenoscape::InternalServerError, error_message_500(response, "Something is technically wrong.")
17
+ when 502
18
+ raise Phenoscape::BadGateway, error_message_500(response, "The server returned an invalid or incomplete response.")
19
+ when 503
20
+ raise Phenoscape::ServiceUnavailable, error_message_500(response, "Crossref is rate limiting your requests.")
21
+ when 504
22
+ raise Phenoscape::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
23
+ end
24
+ end
25
+ end
26
+
27
+ def initialize(app)
28
+ super app
29
+ @parser = nil
30
+ end
31
+
32
+ private
33
+
34
+ def error_message_400(response)
35
+ "\n #{response[:method].to_s.upcase} #{response[:url].to_s}\n Status #{response[:status]}#{error_body(response[:body])}"
36
+ end
37
+
38
+ def error_body(body)
39
+ if not body.nil? and not body.empty? and body.kind_of?(String)
40
+ if is_json?(body)
41
+ body = ::MultiJson.load(body)
42
+ if body['message'].nil?
43
+ body = nil
44
+ elseif body['message'].length == 1
45
+ body = body['message']
46
+ else
47
+ body = body['message'].collect { |x| x['message'] }.join('; ')
48
+ end
49
+ end
50
+ end
51
+
52
+ if body.nil?
53
+ nil
54
+ else
55
+ ": #{body}"
56
+ end
57
+ end
58
+
59
+ def error_message_500(response, body=nil)
60
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
61
+ end
62
+
63
+ def is_json?(string)
64
+ MultiJson.load(string)
65
+ return true
66
+ rescue MultiJson::ParseError
67
+ return false
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,26 @@
1
+ # taken from: https://viget.com/extend/easy-gem-configuration-variables-with-defaults
2
+ module Configuration
3
+
4
+ def configuration
5
+ yield self
6
+ end
7
+
8
+ def define_setting(name, default = nil)
9
+ class_variable_set("@@#{name}", default)
10
+ define_class_method "#{name}=" do |value|
11
+ class_variable_set("@@#{name}", value)
12
+ end
13
+ define_class_method name do
14
+ class_variable_get("@@#{name}")
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def define_class_method(name, &block)
21
+ (class << self; self; end).instance_eval do
22
+ define_method name, &block
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,74 @@
1
+ require "faraday"
2
+ require "multi_json"
3
+ require "nokogiri"
4
+ require "phenoscaperb/error"
5
+ require "phenoscaperb/constants"
6
+ require 'phenoscaperb/utils'
7
+ require 'phenoscaperb/helpers/configuration'
8
+
9
+ ##
10
+ # Phenoscape::Request
11
+ #
12
+ # Class to perform HTTP requests to the Phenoscape API
13
+ module Phenoscape
14
+ class Request #:nodoc:
15
+
16
+ attr_accessor :endpt
17
+ attr_accessor :args
18
+ attr_accessor :verbose
19
+ attr_accessor :options
20
+ attr_accessor :ret
21
+ attr_accessor :acchd
22
+
23
+ def initialize(endpt, args, verbose, options, ret, acchd = "application/json")
24
+ self.endpt = endpt
25
+ self.args = args
26
+ self.verbose = verbose
27
+ self.options = options
28
+ self.ret = ret
29
+ self.acchd = acchd
30
+ end
31
+
32
+ def perform
33
+ if verbose
34
+ conn = Faraday.new(:url => Phenoscape.base_url, :request => self.options || []) do |f|
35
+ f.response :logger
36
+ f.use FaradayMiddleware::RaiseHttpException
37
+ f.options.params_encoder = Faraday::FlatParamsEncoder
38
+ f.adapter Faraday.default_adapter
39
+ end
40
+ else
41
+ conn = Faraday.new(:url => Phenoscape.base_url, :request => self.options || []) do |f|
42
+ f.use FaradayMiddleware::RaiseHttpException
43
+ f.options.params_encoder = Faraday::FlatParamsEncoder
44
+ f.adapter Faraday.default_adapter
45
+ end
46
+ end
47
+
48
+ conn.headers[:user_agent] = make_ua
49
+ conn.headers["X-USER-AGENT"] = make_ua
50
+ if !self.acchd.nil?
51
+ conn.headers["Accept"] = self.acchd
52
+ end
53
+
54
+ res = conn.get self.endpt, self.args
55
+
56
+ if !res.headers['content-type'].match(/json/).nil?
57
+ out = MultiJson.load(res.body)
58
+ else
59
+ case self.ret
60
+ when "hash"
61
+ out = Nokogiri::XML(res.body).to_hash
62
+ when "text"
63
+ out = res.body
64
+ when "noko"
65
+ out = Nokogiri::XML(res.body)
66
+ end
67
+ end
68
+
69
+ return out
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,43 @@
1
+ def make_ua
2
+ requa = 'Faraday/v' + Faraday::VERSION
3
+ habua = 'Phenoscaperb/v' + Phenoscape::VERSION
4
+ return requa + ' ' + habua
5
+ end
6
+
7
+ class Hash
8
+ def tostrings
9
+ Hash[self.map{|(k,v)| [k.to_s,v]}]
10
+ end
11
+ end
12
+
13
+ class Hash
14
+ def tosymbols
15
+ Hash[self.map{|(k,v)| [k.to_sym,v]}]
16
+ end
17
+ end
18
+
19
+ def check_data(x, y)
20
+ if len2(x) == 1
21
+ testdata = [x]
22
+ else
23
+ testdata = x
24
+ end
25
+
26
+ for z in testdata
27
+ if !y.include? z
28
+ raise z + ' is not one of the choices'
29
+ end
30
+ end
31
+ end
32
+
33
+ def len2(x)
34
+ if x.class == String
35
+ return [x].length
36
+ else
37
+ return x.length
38
+ end
39
+ end
40
+
41
+ def stop(x)
42
+ raise ArgumentError, x
43
+ end
@@ -0,0 +1,3 @@
1
+ module Phenoscape
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phenoscaperb/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'phenoscaperb'
8
+ s.version = Phenoscape::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.required_ruby_version = '>= 2.0'
11
+ s.date = '2017-12-11'
12
+ s.summary = "Phenoscape Client"
13
+ s.description = "Low Level Ruby Client for the Phenoscape API"
14
+ s.authors = "Scott Chamberlain"
15
+ s.email = 'myrmecocystus@gmail.com'
16
+ s.homepage = 'https://github.com/sckott/phenoscaperb'
17
+ s.licenses = 'MIT'
18
+
19
+ s.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.bindir = 'bin'
23
+ s.executables = ['ph']
24
+
25
+ s.add_development_dependency 'bundler', '~> 1.14', '>= 1.14.6'
26
+ s.add_development_dependency 'rake', '~> 12.0', '>= 12.0.0'
27
+ s.add_development_dependency 'test-unit', '~> 3.2', '>= 3.2.1'
28
+ s.add_development_dependency 'simplecov', '~> 0.14.1'
29
+ s.add_development_dependency 'codecov', '~> 0.1.10'
30
+ s.add_development_dependency 'json', '~> 2.0', '>= 2.0.3'
31
+ s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.3'
32
+ s.add_development_dependency 'webmock', '~> 2.3', '>= 2.3.2'
33
+
34
+ s.add_runtime_dependency 'faraday', '~> 0.12.0.1'
35
+ s.add_runtime_dependency 'faraday_middleware', '~> 0.11.0.1'
36
+ s.add_runtime_dependency 'thor', '~> 0.20.0'
37
+ s.add_runtime_dependency 'nokogiri', '~> 1.8', '>= 1.8.1'
38
+ s.add_runtime_dependency 'xml-to-hash', '~> 1.0', '>= 1.0.3'
39
+ s.add_runtime_dependency 'multi_json', '~> 1.12', '>= 1.12.1'
40
+ end
metadata ADDED
@@ -0,0 +1,318 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phenoscaperb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Chamberlain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.14.6
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.14'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.14.6
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '12.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 12.0.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '12.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 12.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: test-unit
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.2'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.2.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.2'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.2.1
73
+ - !ruby/object:Gem::Dependency
74
+ name: simplecov
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.14.1
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: 0.14.1
87
+ - !ruby/object:Gem::Dependency
88
+ name: codecov
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: 0.1.10
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: 0.1.10
101
+ - !ruby/object:Gem::Dependency
102
+ name: json
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '2.0'
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.3
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.0'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.0.3
121
+ - !ruby/object:Gem::Dependency
122
+ name: vcr
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '3.0'
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 3.0.3
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '3.0'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 3.0.3
141
+ - !ruby/object:Gem::Dependency
142
+ name: webmock
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '2.3'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 2.3.2
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '2.3'
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 2.3.2
161
+ - !ruby/object:Gem::Dependency
162
+ name: faraday
163
+ requirement: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 0.12.0.1
168
+ type: :runtime
169
+ prerelease: false
170
+ version_requirements: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - "~>"
173
+ - !ruby/object:Gem::Version
174
+ version: 0.12.0.1
175
+ - !ruby/object:Gem::Dependency
176
+ name: faraday_middleware
177
+ requirement: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - "~>"
180
+ - !ruby/object:Gem::Version
181
+ version: 0.11.0.1
182
+ type: :runtime
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - "~>"
187
+ - !ruby/object:Gem::Version
188
+ version: 0.11.0.1
189
+ - !ruby/object:Gem::Dependency
190
+ name: thor
191
+ requirement: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - "~>"
194
+ - !ruby/object:Gem::Version
195
+ version: 0.20.0
196
+ type: :runtime
197
+ prerelease: false
198
+ version_requirements: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - "~>"
201
+ - !ruby/object:Gem::Version
202
+ version: 0.20.0
203
+ - !ruby/object:Gem::Dependency
204
+ name: nokogiri
205
+ requirement: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - "~>"
208
+ - !ruby/object:Gem::Version
209
+ version: '1.8'
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: 1.8.1
213
+ type: :runtime
214
+ prerelease: false
215
+ version_requirements: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - "~>"
218
+ - !ruby/object:Gem::Version
219
+ version: '1.8'
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: 1.8.1
223
+ - !ruby/object:Gem::Dependency
224
+ name: xml-to-hash
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '1.0'
230
+ - - ">="
231
+ - !ruby/object:Gem::Version
232
+ version: 1.0.3
233
+ type: :runtime
234
+ prerelease: false
235
+ version_requirements: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - "~>"
238
+ - !ruby/object:Gem::Version
239
+ version: '1.0'
240
+ - - ">="
241
+ - !ruby/object:Gem::Version
242
+ version: 1.0.3
243
+ - !ruby/object:Gem::Dependency
244
+ name: multi_json
245
+ requirement: !ruby/object:Gem::Requirement
246
+ requirements:
247
+ - - "~>"
248
+ - !ruby/object:Gem::Version
249
+ version: '1.12'
250
+ - - ">="
251
+ - !ruby/object:Gem::Version
252
+ version: 1.12.1
253
+ type: :runtime
254
+ prerelease: false
255
+ version_requirements: !ruby/object:Gem::Requirement
256
+ requirements:
257
+ - - "~>"
258
+ - !ruby/object:Gem::Version
259
+ version: '1.12'
260
+ - - ">="
261
+ - !ruby/object:Gem::Version
262
+ version: 1.12.1
263
+ description: Low Level Ruby Client for the Phenoscape API
264
+ email: myrmecocystus@gmail.com
265
+ executables:
266
+ - ph
267
+ extensions: []
268
+ extra_rdoc_files: []
269
+ files:
270
+ - ".gitignore"
271
+ - ".travis.yml"
272
+ - CHANGELOG.md
273
+ - CONDUCT.md
274
+ - Gemfile
275
+ - Gemfile.lock
276
+ - LICENSE
277
+ - README.md
278
+ - Rakefile
279
+ - bin/ph
280
+ - lib/phenoscaperb.rb
281
+ - lib/phenoscaperb/Genes.rb
282
+ - lib/phenoscaperb/Ontotrace.rb
283
+ - lib/phenoscaperb/Studies.rb
284
+ - lib/phenoscaperb/Taxa.rb
285
+ - lib/phenoscaperb/Terms.rb
286
+ - lib/phenoscaperb/constants.rb
287
+ - lib/phenoscaperb/error.rb
288
+ - lib/phenoscaperb/faraday.rb
289
+ - lib/phenoscaperb/helpers/configuration.rb
290
+ - lib/phenoscaperb/request.rb
291
+ - lib/phenoscaperb/utils.rb
292
+ - lib/phenoscaperb/version.rb
293
+ - phenoscaperb.gemspec
294
+ homepage: https://github.com/sckott/phenoscaperb
295
+ licenses:
296
+ - MIT
297
+ metadata: {}
298
+ post_install_message:
299
+ rdoc_options: []
300
+ require_paths:
301
+ - lib
302
+ required_ruby_version: !ruby/object:Gem::Requirement
303
+ requirements:
304
+ - - ">="
305
+ - !ruby/object:Gem::Version
306
+ version: '2.0'
307
+ required_rubygems_version: !ruby/object:Gem::Requirement
308
+ requirements:
309
+ - - ">="
310
+ - !ruby/object:Gem::Version
311
+ version: '0'
312
+ requirements: []
313
+ rubyforge_project:
314
+ rubygems_version: 2.6.13
315
+ signing_key:
316
+ specification_version: 4
317
+ summary: Phenoscape Client
318
+ test_files: []