Skip to content

Commit 1d8dd9a

Browse files
committed
fix lint errors reported by Rubocup and set new gem versions as dependency
1 parent 222a2ed commit 1d8dd9a

File tree

5 files changed

+35
-38
lines changed

5 files changed

+35
-38
lines changed

.rubocop.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
# human should be able to read 200 chars per line
12
Layout/LineLength:
2-
Max: 100
3+
Max: 200
34

5+
# relax branch condition size, code can be too verbose
46
Metrics/AbcSize:
5-
Max: 21
7+
Max: 40
68

9+
# short clear method name
710
Metrics/MethodLength:
811
Max: 25
912

10-
Style/FrozenStringLiteralComment:
13+
# no performance implication
14+
Style/OptionalBooleanParameter:
1115
Enabled: false
1216

13-
Style/RescueStandardError:
14-
Enabled: false
15-
16-
# %i is not supported by older languages
17+
# %i(array) is not common in Ruby
1718
Style/SymbolArray:
1819
Enabled: false
1920

Gemfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ group :test, :development do
1212
# code coloring for yard
1313
gem 'redcarpet'
1414
# documentation generation
15-
gem 'yard'
15+
gem 'yard', '~>0.9.28'
1616
# linter for ruby
17-
gem 'rubocop'
17+
gem 'rubocop', '~>1.30.1'
1818
# test for ruby
19-
gem 'rspec'
19+
gem 'rspec', '~>3.11'
2020
# code coverage to monitor rspec tests
21-
gem 'simplecov'
21+
gem 'simplecov',
2222

2323
platforms :mri do
2424
gem "byebug"

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ task oobt: %i[readme doc check build install demo]
6969

7070
desc "execute all the steps"
7171
task default: %i[dependency version readme doc build test oobt]
72+
73+
desc "format ruby code using rubocop"
74+
task :format do
75+
sh('rubocop --auto-correct')
76+
end

lib/serpapi/client.rb

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Client implementation for SerpApi.com
22
#
33
module SerpApi
4-
54
# Client for SerpApi.com
65
#
76
class Client
@@ -10,7 +9,7 @@ class Client
109
# Backend service URL
1110
BACKEND = 'serpapi.com'.freeze
1211

13-
# HTTP timeout requests
12+
# HTTP timeout requests
1413
attr_reader :timeout,
1514
# Query parameters
1615
:params
@@ -31,7 +30,7 @@ class Client
3130
# key can be either a symbol or a string.
3231
#
3332
# @param [Hash] params default for the search
34-
def initialize(params = {}, adapter = :net_http)
33+
def initialize(params = {}, _adapter = :net_http)
3534
# set default read timeout
3635
@timeout = params[:timeout] || params['timeout'] || 120
3736
@timeout.freeze
@@ -128,30 +127,24 @@ def query(params)
128127
# @param [Boolean] symbolize_names if true, convert JSON keys to symbols
129128
# @return decoded payload as JSON / Hash or String
130129
def get(endpoint, decoder = :json, params = {}, symbolize_names = true)
131-
begin
132-
payload = @socket.get(endpoint) do |req|
133-
req.params = query(params)
134-
req.options.timeout = timeout
135-
end
136-
# read http response
137-
data = payload.body
138-
# decode payload using JSON native parser
139-
if decoder == :json
140-
data = JSON.parse(data, symbolize_names: symbolize_names)
141-
if data.class == Hash && data.key?('error')
142-
raise SerpApiException, "get failed with error: #{data['error']} from url: #{endpoint}, params: #{params}, decoder: #{decoder}, http status: #{payload.status} "
143-
end
144-
if payload.status != 200
145-
raise SerpApiException, "get failed with response status: #{payload.status} reponse: #{data} on get url: #{endpoint}, params: #{params}, decoder: #{decoder}"
146-
end
130+
payload = @socket.get(endpoint) do |req|
131+
req.params = query(params)
132+
req.options.timeout = timeout
133+
end
134+
# read http response
135+
data = payload.body
136+
# decode payload using JSON native parser
137+
if decoder == :json
138+
data = JSON.parse(data, symbolize_names: symbolize_names)
139+
if data.instance_of?(Hash) && data.key?('error')
140+
raise SerpApiException, "get failed with error: #{data['error']} from url: #{endpoint}, params: #{params}, decoder: #{decoder}, http status: #{payload.status} "
147141
end
148-
# return raw HTML
149-
return data
150-
rescue Faraday::Error => err
151-
raise SerpApiException, "fail: get url: #{endpoint} caused by #{err.class} : #{err.message} (params: #{params}, decoder: #{decoder})"
142+
raise SerpApiException, "get failed with response status: #{payload.status} reponse: #{data} on get url: #{endpoint}, params: #{params}, decoder: #{decoder}" if payload.status != 200
152143
end
144+
# return raw HTML
145+
data
146+
rescue Faraday::Error => e
147+
raise SerpApiException, "fail: get url: #{endpoint} caused by #{e.class} : #{e.message} (params: #{params}, decoder: #{decoder})"
153148
end
154-
155149
end
156-
157150
end

lib/serpapi/version.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module SerpApi
2-
32
# Current version of the gem
43
VERSION = '1.0.0'.freeze
5-
64
end

0 commit comments

Comments
 (0)