Skip to content

Commit

Permalink
Merge 0fe960c into dc400a7
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Aug 14, 2019
2 parents dc400a7 + 0fe960c commit b03e49f
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 59 deletions.
8 changes: 0 additions & 8 deletions .rubocop.yml
Expand Up @@ -10,25 +10,17 @@ Style/ClassAndModuleChildren:
Enabled: false

# disable for now
Style/AccessorMethodName:
Enabled: false
Style/ClassVars:
Enabled: false
Style/Documentation:
Enabled: false
Style/IfUnlessModifier:
Enabled: false
Style/RescueModifier:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/LineLength:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -23,8 +23,8 @@ The Unipept CLI is available as a *gem*. This means it can easily be installed w

```bash
$ gem install unipept
Successfully installed unipept-0.7.1
Parsing documentation for unipept-0.7.1
Successfully installed unipept-0.8.0
Parsing documentation for unipept-0.8.0
Done installing documentation for unipept after 0 seconds
1 gem installed
```
Expand All @@ -33,7 +33,7 @@ After successful installation, the unipept command should be available:

```bash
$ unipept -v
0.7.1
0.8.0
```

The help can be accessed by running `unipept -h`.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.7.1
0.8.0
4 changes: 1 addition & 3 deletions lib/commands/peptfilter.rb
Expand Up @@ -33,9 +33,7 @@ class Peptfilter
end

pept = pept.chomp
if Peptfilter.filter(pept, minlen, maxlen, lacks, contains)
puts pept
end
puts pept if Peptfilter.filter(pept, minlen, maxlen, lacks, contains)
end
end
end
Expand Down
22 changes: 18 additions & 4 deletions lib/commands/unipept/api_runner.rb
@@ -1,3 +1,5 @@
require_relative '../../retryable_typhoeus'

module Unipept
class Commands::ApiRunner < Cri::CommandRunner
attr_reader :configuration
Expand All @@ -21,13 +23,13 @@ def initialize(args, opts, cmd)
# - the host
# - the user agent
def set_configuration
@host = get_host
@host = host
@user_agent = 'Unipept CLI - unipept ' + Unipept::VERSION
end

# Returns the host. If a value is defined by both an option and the config
# file, the value of the option is used.
def get_host
def host
# find host in opts first
host = options[:host] ? options[:host] : @configuration['host']

Expand Down Expand Up @@ -117,15 +119,27 @@ def run
batch_order = Unipept::BatchOrder.new

batch_iterator.iterate(input_iterator) do |input_slice, batch_id, fasta_mapper|
request = Typhoeus::Request.new(
request = ::RetryableTyphoeus::Request.new(
@url,
method: :post,
body: construct_request_body(input_slice),
accept_encoding: 'gzip',
headers: { 'User-Agent' => @user_agent }
)

request.on_complete do |resp|
# Failure callback: retry if retries not exceeded
request.on_failure do |resp|
if resp.request.retries <= 0
block = handle_response(resp, batch_id, fasta_mapper)
batch_order.wait(batch_id, &block)
else
resp.request.retries -= 1
hydra.queue_front resp.request
end
end

# Success callback
request.on_success do |resp|
block = handle_response(resp, batch_id, fasta_mapper)
batch_order.wait(batch_id, &block)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/commands/unipept/config.rb
@@ -1,9 +1,7 @@
module Unipept
class Commands::Config < Cri::CommandRunner
def run
if arguments.size == 0 || arguments.size > 2
abort command.help
end
abort command.help if arguments.size == 0 || arguments.size > 2

key, value = *arguments

Expand Down
4 changes: 1 addition & 3 deletions lib/commands/uniprot.rb
Expand Up @@ -62,9 +62,7 @@ def self.get_uniprot_entry(accession, format)
else
# other format has been specified, just download and output
resp = Typhoeus.get("http://www.uniprot.org/uniprot/#{accession}.#{format}")
if resp.success?
resp.response_body
end
resp.response_body if resp.success?
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/retryable_typhoeus.rb
@@ -0,0 +1,21 @@
# Retryable Typheous
# Inspiration: https://gist.github.com/kunalmodi/2939288
# Patches the request and hydra to allow requests to get resend when they fail

module RetryableTyphoeus
require 'typhoeus'

include Typhoeus

DEFAULT_RETRIES = 10

class Request < Typhoeus::Request
attr_accessor :retries

def initialize(base_url, options = {})
@retries = (options.delete(:retries) || DEFAULT_RETRIES)

super
end
end
end
12 changes: 6 additions & 6 deletions test/commands/unipept/test_api_runner.rb
Expand Up @@ -22,14 +22,14 @@ def test_config_host
runner = new_runner('test', { host: 'http://param_host' }, %w(a b c))
runner.options.delete(:host)
runner.configuration['host'] = 'http://config_host'
host = runner.get_host
host = runner.host
assert_equal('http://config_host', host)
end

def test_param_host
runner = new_runner('test', { host: 'http://param_host' }, %w(a b c))
runner.configuration.delete('host')
host = runner.get_host
host = runner.host
assert_equal('http://param_host', host)
end

Expand All @@ -39,7 +39,7 @@ def test_no_host
runner.options.delete(:host)
_out, err = capture_io_while do
assert_raises SystemExit do
runner.get_host
runner.host
end
end
assert(err.start_with? 'WARNING: no host has been set')
Expand All @@ -48,19 +48,19 @@ def test_no_host
def test_host_priority
runner = new_runner('test', { host: 'http://param_host' }, %w(a b c))
runner.configuration['host'] = 'http://config_host'
host = runner.get_host
host = runner.host
assert_equal('http://param_host', host)
end

def test_http_host
runner = new_runner('test', { host: 'param_host' }, %w(a b c))
host = runner.get_host
host = runner.host
assert_equal('http://param_host', host)
end

def test_https_host
runner = new_runner('test', { host: 'https://param_host' }, %w(a b c))
host = runner.get_host
host = runner.host
assert_equal('https://param_host', host)
end

Expand Down
20 changes: 10 additions & 10 deletions test/test_formatters.rb
Expand Up @@ -38,15 +38,15 @@ def formatter
end

def test_header
assert_equal('', formatter.header(TestObject.get_object))
assert_equal('', formatter.header(TestObject.test_object))
end

def test_type
assert_equal('', formatter.type)
end

def test_format
assert_equal(TestObject.get_object, formatter.format(TestObject.get_object))
assert_equal(TestObject.test_object, formatter.format(TestObject.test_object))
end
end

Expand All @@ -56,15 +56,15 @@ def formatter
end

def test_header
assert_equal('', formatter.header(TestObject.get_object))
assert_equal('', formatter.header(TestObject.test_object))
end

def test_type
assert_equal('json', formatter.type)
end

def test_format
assert_equal(TestObject.as_json, formatter.format(TestObject.get_object))
assert_equal(TestObject.as_json, formatter.format(TestObject.test_object))
end
end

Expand All @@ -75,7 +75,7 @@ def formatter

def test_header
fasta = [['peptide', '>test']]
object = [TestObject.get_object, TestObject.get_object]
object = [TestObject.test_object, TestObject.test_object]
assert_equal(TestObject.as_csv_header, formatter.header(object))
assert_equal('fasta_header,' + TestObject.as_csv_header, formatter.header(object, fasta))
end
Expand All @@ -85,14 +85,14 @@ def test_type
end

def test_format
object = [TestObject.get_object, TestObject.get_object]
object = [TestObject.test_object, TestObject.test_object]
csv = [TestObject.as_csv, TestObject.as_csv, ''].join("\n")
assert_equal(csv, formatter.format(object))
end

def test_format_with_fasta
fasta = [['>test', '5']]
object = [TestObject.get_object, TestObject.get_object]
object = [TestObject.test_object, TestObject.test_object]
csv = ['>test,' + TestObject.as_csv, '>test,' + TestObject.as_csv, ''].join("\n")
assert_equal(csv, formatter.format(object, fasta))
end
Expand All @@ -104,20 +104,20 @@ def formatter
end

def test_header
assert_equal('', formatter.header(TestObject.get_object))
assert_equal('', formatter.header(TestObject.test_object))
end

def test_type
assert_equal('xml', formatter.type)
end

def test_format
assert_equal(TestObject.as_xml, formatter.format(TestObject.get_object))
assert_equal(TestObject.as_xml, formatter.format(TestObject.test_object))
end
end

class TestObject
def self.get_object
def self.test_object
JSON.parse('{"integer": 5, "string": "string", "list": ["a", 2, false]}')
end

Expand Down

0 comments on commit b03e49f

Please sign in to comment.