Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Update Faraday to v1.0 (#17)
Browse files Browse the repository at this point in the history
- Update the minimum version of faraday dependency to v1.0
- Remove faraday_middleware dependency and use native JSON lib to parse responses
- Bump minimum Ruby version to 2.3
- Remove sudo: false from .travis.yml (https://blog.travis-ci.com/2018-11-19-required-linux-infrastructure-migration)
- Update RuboCop version of Hound to 0.75 and remove Gemspec/RequiredRubyVersion rule
- Minor style fixes
  • Loading branch information
epistrephein authored Jan 22, 2020
1 parent 9f73f2c commit 8bddada
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .hound.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ fail_on_violations: true

rubocop:
config_file: .rubocop.yml
version: 0.71.0
version: 0.75.0
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ AllCops:
DisplayCopNames: true
DisplayStyleGuide: true

Gemspec/RequiredRubyVersion:
Enabled: false

Layout/AlignHash:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
language: ruby
sudo: false
script: bundle exec rspec
rvm:
- ruby-head
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
[Diff](https://github.com/epistrephein/rarbg/compare/v1.4.0...master)

#### Changed
- Update Faraday to v1.0 and drop `faraday_middleware` dependency ([#17](https://github.com/epistrephein/rarbg/pull/17)).
- Update Ruby minimum version to 2.3 ([#17](https://github.com/epistrephein/rarbg/pull/17)).
- Rename attr_reader `conn` to `connection` ([#18](https://github.com/epistrephein/rarbg/pull/18)).
- Remove unnecessary files and attributes from gemspec ([#21](https://github.com/epistrephein/rarbg/pull/21)).
- Update version constraints for Rake, following v13.0 release ([#15](https://github.com/epistrephein/rarbg/pull/15)).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ gem 'rarbg', '~> 1.4'

This gem wraps all API methods available from [RARBG TorrentAPI](https://torrentapi.org/apidocs_v2.txt?&app_id=rarbg-rubygem).

An authentication token is automatically generated on first request, stored with timestamp and renewed every 800 seconds.
An authentication token is automatically generated on the first request, stored with a timestamp and renewed every 800 seconds.

Rate limit (1req/2s) is automatically enforced.

Expand Down
26 changes: 11 additions & 15 deletions lib/rarbg/api.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'faraday'
require 'faraday_middleware'
require 'json'

# Main namespace for RARBG.
module RARBG
Expand Down Expand Up @@ -55,8 +55,6 @@ class API
# rarbg = RARBG::API.new
def initialize
@connection = Faraday.new(url: API_ENDPOINT) do |conn|
conn.request :json
conn.response :json, content_type: /\bjson$/
conn.response :logger if $VERBOSE
conn.adapter Faraday.default_adapter

Expand Down Expand Up @@ -104,10 +102,7 @@ def initialize
def list(params = {})
raise ArgumentError, 'Expected params hash' unless params.is_a?(Hash)

params.update(
mode: 'list',
token: token?
)
params.update(mode: 'list', token: token?)
call(params)
end

Expand Down Expand Up @@ -153,10 +148,7 @@ def list(params = {})
def search(params = {})
raise ArgumentError, 'Expected params hash' unless params.is_a?(Hash)

params.update(
mode: 'search',
token: token?
)
params.update(mode: 'search', token: token?)
call(params)
end

Expand Down Expand Up @@ -202,6 +194,7 @@ def validate(params)
normalize.each_pair do |key, proc|
params[key] = proc.call(params[key]) if params.key?(key)
end

params
end

Expand All @@ -220,6 +213,7 @@ def validate_search!(params)
SEARCH_KEYS.each do |k|
params["search_#{k}"] = params.delete(k) if params.key?(k)
end

params
end

Expand All @@ -235,20 +229,22 @@ def normalize
# Return or renew auth token.
def token?
if token.nil? || time >= (token_time.to_f + TOKEN_EXPIRATION)
response = request(get_token: 'get_token')
@token = response.fetch('token')
response = request(get_token: 'get_token')
@token = response.fetch('token')
@token_time = time
end

token
end

# Perform API request.
def request(params)
rate_limit!(RATE_LIMIT)
response = connection.get(nil, params)

response = connection.get(nil, params)
@last_request = time

return response.body if response.success?
return JSON.parse(response.body) if response.success?

raise APIError, "#{response.reason_phrase} (#{response.status})"
end
Expand Down
5 changes: 2 additions & 3 deletions rarbg.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ Gem::Specification.new do |spec|
spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE']
spec.require_path = 'lib'

spec.required_ruby_version = '>= 2.0'
spec.required_ruby_version = '>= 2.3'

spec.add_runtime_dependency 'faraday', '~> 0.12'
spec.add_runtime_dependency 'faraday_middleware', '~> 0.12'
spec.add_runtime_dependency 'faraday', '~> 1.0'

spec.add_development_dependency 'bundler', '>= 1.15', '< 3.0'
spec.add_development_dependency 'pry', '~> 0.10'
Expand Down

0 comments on commit 8bddada

Please sign in to comment.