Skip to content

Commit

Permalink
Remove dependency to Faraday
Browse files Browse the repository at this point in the history
Remove production dependency to Faraday, switch to Net::HTTP. This
change was made so that this gem depends on no external production
dependencies, making it safer to use in projects. The version was also
bumped to 0.3.3.
  • Loading branch information
leesharma committed May 22, 2016
1 parent 5e4769f commit 9eec31b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
3 changes: 0 additions & 3 deletions lib/rescuetime/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require 'faraday'
require 'csv'

require 'rescuetime/query_buildable'

require 'rescuetime/requester'
Expand Down
6 changes: 5 additions & 1 deletion lib/rescuetime/collection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'csv'

require 'rescuetime/query_buildable'

module Rescuetime
Expand Down Expand Up @@ -34,7 +36,9 @@ def <<(terms)
#
# @see Rescuetime::Requester#get
def all
parse_response Requester.get(HOST, params).body
requester = Rescuetime::Requester
host = HOST
parse_response requester.get(host, params)
end

# Enumerates over the collection of Rescuetime report records. Performs
Expand Down
28 changes: 18 additions & 10 deletions lib/rescuetime/requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class << self
# @param [String] host request host
# @param [Hash] params request parameters
# @param [#get] http HTTP requester
# @return [Faraday::Response]
# @return [String]
#
# @raise [Rescuetime::MissingCredentialsError] if no api key is present
# @raise [Rescuetime::InvalidCredentialsError] if api key is incorrect
Expand All @@ -29,42 +29,50 @@ class << self
# not 200
#
# @see Rescuetime::Client#api_key=
def get(host, params, http: Faraday)
def get(host, params, http: Net::HTTP)
# Fail if no api key is provided
unless params[:key] && !params[:key].to_s.empty?
fail(Rescuetime::MissingCredentialsError)
end

req_params = params.delete_if { |_, v| !v || v.to_s.empty? }
response = http.get host, req_params
uri = set_uri host, params
response = http.get_response uri

fail_or_return response
fail_or_return_body response
end

private

def set_uri(host, params)
req_params = params.delete_if { |_, v| !v || v.to_s.empty? }
uri = URI(host)
uri.query = URI.encode_www_form(req_params)
uri
end

# Checks for an error in the Rescuetime response. If an error was recieved
# raise the appropriate Rescuetime::Error. Otherwise, return the response.
#
# @param [Faraday::Response] response HTTP response from rescuetime.com
# @return [Faraday::Response] 200 HTTP response from rescuetime.com
# @param [Net::HTTPResponse] response HTTP response from rescuetime.com
# @return [String] valid response body
#
# @raise [Rescuetime::InvalidCredentialsError] if api key is incorrect
# @raise [Rescuetime::InvalidQueryError] if query is badly formed
# @raise [Rescuetime::Error] an error that varies based on the
# response status
def fail_or_return(response)
def fail_or_return_body(response)
# match the response body to known error messages with 200 status
case response.body
when key_not_found? then fail Rescuetime::InvalidCredentialsError
when invalid_query? then fail Rescuetime::InvalidQueryError
end

# check the response status for errors
error = Rescuetime::Error::CODES[response.status.to_i]
status = response.code.to_i
error = Rescuetime::Error::CODES[status]
fail(error) if error

response
response.body
end

# Returns lambda that returns true if the response body states that the
Expand Down
2 changes: 1 addition & 1 deletion lib/rescuetime/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# lib/rescuetime/version.rb
module Rescuetime
# rescuetime gem version number
VERSION = '0.3.2'
VERSION = '0.3.3'
end
4 changes: 1 addition & 3 deletions rescuetime.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ Gem::Specification.new do |spec|
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0")
.reject { |f| f.match(%r{^(test|spec|features)/}) }
.reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.required_ruby_version = '>= 2.0.0'

spec.add_dependency 'faraday', '~> 0.9.1'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'

Expand Down

0 comments on commit 9eec31b

Please sign in to comment.