Skip to content

Commit 2f6dcbb

Browse files
committed
improve coding style based on feedback from ocvit
#5
1 parent 9087af1 commit 2f6dcbb

32 files changed

+182
-110
lines changed

lib/serpapi.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module SerpApi
99
require 'json'
1010

1111
# implementation
12+
require_relative 'serpapi/version'
1213
require_relative 'serpapi/error'
13-
require_relative 'serpapi/serpapi'
14+
require_relative 'serpapi/client'
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# Client implementation for SerpApi.com
22
#
33
module SerpApi
4+
45
# Client for SerpApi.com
56
#
67
class Client
7-
VERSION = '1.0.0'.freeze
8-
BACKEND = 'serpapi.com'.freeze
8+
include Errors
99

10-
# HTTP timeout in seconds (default: 120)
11-
attr_accessor :timeout
10+
BACKEND = 'serpapi.com'.freeze
1211

13-
# Default parameters provided in the constructor (hash)
14-
attr_accessor :params
12+
# HTTP timeout requests
13+
attr_reader :timeout,
14+
# Query parameters
15+
:params
1516

1617
# Constructor
1718
#
@@ -24,7 +25,7 @@ class Client
2425
# The params hash should contains the following optional field:
2526
# api_key [String] user secret API key
2627
# engine [String] search enginge selected
27-
# timeout [Integer] HTTP read max timeout in seconds (default: 60s)
28+
# timeout [Integer] HTTP read max timeout in seconds (default: 120s)
2829
#
2930
# key can be either a symbol or a string.
3031
#
@@ -113,10 +114,10 @@ def build_url(endpoint, params)
113114
query = (@params || {}).merge(params || {})
114115

115116
# set ruby client
116-
query[:source] = 'serpapi-ruby:' << VERSION
117+
query[:source] = 'serpapi-ruby:' << SerpApi::VERSION
117118

118119
# delete empty key/value
119-
query.delete_if { |_, value| value.nil? }
120+
query.compact!
120121

121122
# HTTP params encoding
122123
encoded_query = URI.encode_www_form(query)
@@ -133,15 +134,17 @@ def build_url(endpoint, params)
133134
# @return decoded payload as JSON / Hash or String
134135
def get(endpoint, decoder = :json, params = {})
135136
url = build_url(endpoint, params)
136-
payload = URI(url).open(read_timeout: @read_timeout).read
137+
payload = URI(url).open(read_timeout: timeout).read
137138
decode(payload, decoder)
138-
rescue OpenURI::HTTPError => e
139-
data = JSON.parse(e.io.read)
140-
raise SerpApiException, "error: #{data['error']} from url: #{url}" if data.key?('error')
141-
142-
raise SerpApiException, "fail: get url: #{url} response: #{data}"
143-
rescue => e
144-
raise SerpApiException, "fail: get url: #{url} caused by: #{e}"
139+
rescue OpenURI::HTTPError => err
140+
data = JSON.parse(err.io.read)
141+
if data.key?('error')
142+
raise SerpApiException, "error: #{data['error']} from url: #{url}"
143+
end
144+
raise SerpApiException, "fail: get url: #{url} response: #{data}"
145+
rescue => err
146+
raise SerpApiException, "fail: get url: #{url} caused by: #{err}"
147+
end
145148
end
146149

147150
# decode HTTP payload either as :json or :html
@@ -160,5 +163,4 @@ def decode(payload, decoder)
160163
raise SerpApiException, msg
161164
end
162165
end
163-
end
164166
end

lib/serpapi/error.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Module includes SerpApi exception
22
#
33
module SerpApi
4-
# SerpApiException wraps anything related to the SerpApi client
4+
# SerpApiException wraps anything related to the SerpApi client errors.
55
#
6-
class SerpApiException < StandardError
6+
module Errors
7+
class SerpApiException < StandardError
8+
end
79
end
810
end

lib/serpapi/version.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module SerpApi
2+
3+
VERSION = '1.0.0'.freeze
4+
5+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
1+
require 'spec_helper'
22

33
describe 'account API' do
44
it 'fetch user account information' do

spec/serpapi/client/client_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe 'client full coverage' do
4+
before(:all) do
5+
pp Module.constants.select { |c| c.to_s =~ /SerpApi/ }
6+
@client = SerpApi::Client.new(engine: 'google', api_key: ENV['API_KEY'], timeout: 30)
7+
end
8+
9+
it 'search for coffee in Austin, TX and receive json results' do
10+
data = @client.search(q: 'Coffee', location: 'Austin, TX')
11+
expect(data.size).to be > 5
12+
expect(data.class).to be Hash
13+
expect(data.keys.size).to be > 5
14+
end
15+
16+
it 'search fir coffee in Austin, TX and receive raw HTML' do
17+
data = @client.html(q: 'Coffee', location: 'Austin, TX')
18+
expect(data).to match(/coffee/i)
19+
end
20+
21+
it 'missing query' do
22+
begin
23+
@client.search({})
24+
rescue SerpApi::Errors::SerpApiException => e
25+
expect(e.message).to include('Missing query')
26+
rescue => e
27+
raise("wrong exception: #{e}")
28+
end
29+
end
30+
31+
it 'get params' do
32+
expect(@client.params[:api_key]).to eq(ENV['API_KEY'])
33+
end
34+
35+
it 'api_key' do
36+
expect(@client.api_key).to eq(ENV['API_KEY'])
37+
end
38+
39+
it 'engine' do
40+
expect(@client.engine).to eq('google')
41+
end
42+
43+
it 'timeout' do
44+
expect(@client.timeout).to eq(30)
45+
end
46+
47+
it 'get bad decoder' do
48+
begin
49+
@client.send(:get, '/search', :bad, {q: 'hello'})
50+
rescue SerpApi::Errors::SerpApiException => e
51+
expect(e.message).to include('not supported decoder')
52+
rescue => e
53+
raise("wrong exception: #{e}")
54+
end
55+
end
56+
57+
it 'fail: get url' do
58+
allow(JSON).to receive(:parse) { {} }
59+
begin
60+
@client.search({})
61+
rescue SerpApi::Errors::SerpApiException => e
62+
expect(e.message).to include('fail: get url')
63+
rescue => e
64+
raise("wrong exception: #{e}")
65+
end
66+
end
67+
end

spec/serpapi/example_search_apple_app_store_spec.rb renamed to spec/serpapi/client/example/example_search_apple_app_store_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'spec_helper'
2+
13
describe 'example: apple_app_store search' do
24
it 'prints organic_results' do
35
client = SerpApi::Client.new(api_key: ENV['API_KEY'], engine: 'apple_app_store')
@@ -8,4 +10,4 @@
810
# pp results[:organic_results]
911
# ENV['API_KEY'] captures the secret user API available from http://serpapi.com
1012
end
11-
end
13+
end

spec/serpapi/example_search_baidu_spec.rb renamed to spec/serpapi/client/example/example_search_baidu_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'spec_helper'
2+
13
describe 'example: baidu search' do
24
it 'prints organic_results' do
35
client = SerpApi::Client.new(api_key: ENV['API_KEY'], engine: 'baidu')
@@ -8,4 +10,4 @@
810
# pp results[:organic_results]
911
# ENV['API_KEY'] captures the secret user API available from http://serpapi.com
1012
end
11-
end
13+
end

spec/serpapi/example_search_bing_spec.rb renamed to spec/serpapi/client/example/example_search_bing_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'spec_helper'
2+
13
describe 'example: bing search' do
24
it 'prints organic_results' do
35
client = SerpApi::Client.new(api_key: ENV['API_KEY'], engine: 'bing')
@@ -8,4 +10,4 @@
810
# pp results[:organic_results]
911
# ENV['API_KEY'] captures the secret user API available from http://serpapi.com
1012
end
11-
end
13+
end

spec/serpapi/example_search_duckduckgo_spec.rb renamed to spec/serpapi/client/example/example_search_duckduckgo_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'spec_helper'
2+
13
describe 'example: duckduckgo search' do
24
it 'prints organic_results' do
35
client = SerpApi::Client.new(api_key: ENV['API_KEY'], engine: 'duckduckgo')
@@ -8,4 +10,4 @@
810
# pp results[:organic_results]
911
# ENV['API_KEY'] captures the secret user API available from http://serpapi.com
1012
end
11-
end
13+
end

0 commit comments

Comments
 (0)