lib/spaceship/client.rb in spaceship-0.4.0 vs lib/spaceship/client.rb in spaceship-0.5.0

- old
+ new

@@ -46,11 +46,11 @@ def self.login(user = nil, password = nil) instance = self.new if instance.login(user, password) instance else - raise InvalidUserCredentialsError.new + raise InvalidUserCredentialsError.new, "Invalid User Credentials" end end def self.hostname raise "You must implemented self.hostname" @@ -74,20 +74,22 @@ # The logger in which all requests are logged # /tmp/spaceship[time].log by default def logger unless @logger + # rubocop:disable Style/GlobalVars if $verbose || ENV["VERBOSE"] @logger = Logger.new(STDOUT) else # Log to file by default path = "/tmp/spaceship#{Time.now.to_i}.log" @logger = Logger.new(path) end + # rubocop:enable Style/GlobalVars @logger.formatter = proc do |severity, datetime, progname, msg| - string = "[#{datetime.strftime('%H:%M:%S')}]: #{msg}\n" + "[#{datetime.strftime('%H:%M:%S')}]: #{msg}\n" end end @logger end @@ -108,13 +110,13 @@ results = [] loop do page += 1 current = yield(page) - results = results + current + results += current - break if ((current || []).count < page_size) # no more results + break if (current || []).count < page_size # no more results end return results end @@ -142,11 +144,11 @@ user ||= data.username password = data.password end if user.to_s.strip.empty? or password.to_s.strip.empty? - raise NoUserCredentialsError.new("No login data provided") + raise NoUserCredentialsError.new, "No login data provided" end send_login_request(user, password) # different in subclasses end @@ -169,18 +171,18 @@ private # Is called from `parse_response` to store the latest csrf_token (if available) def store_csrf_tokens(response) if response and response.headers - tokens = response.headers.select { |k, v| %w[csrf csrf_ts].include?(k) } - if tokens and not tokens.empty? + tokens = response.headers.select { |k, v| %w(csrf csrf_ts).include?(k) } + if tokens and !tokens.empty? @csrf_tokens = tokens end end end - # memoize the last csrf tokens from responses + # memorize the last csrf tokens from responses def csrf_tokens @csrf_tokens || {} end def request(method, url_or_path = nil, params = nil, headers = {}, &block) @@ -218,12 +220,12 @@ def log_response(method, url, response) logger.debug("#{method.upcase}: #{url}: #{response.body}") end - # Actually sends the request to the remote server - # Automatically retries the request up to 3 times if something goes wrong + # Actually sends the request to the remote server + # Automatically retries the request up to 3 times if something goes wrong def send_request(method, url_or_path, params, headers, &block) with_retry do @client.send(method, url_or_path, params, headers, &block) end end @@ -233,11 +235,11 @@ content = response.body[expected_key] else content = response.body end - if content == nil - raise UnexpectedResponse.new(response.body) + if content.nil? + raise UnexpectedResponse.new, response.body else store_csrf_tokens(response) content end end