google_search_results 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/google_search_results.rb +61 -26
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c9cec2b3a527d7088744a0189c2d57bc324b3bec
|
|
4
|
+
data.tar.gz: d9920122de8d07df6663ca0794eee0a8a4f33572
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4096606c2a209617653af427213de99d88a419aac4e7aa6c3c5cd28ce0f43af6d716f552aa9b31b710f7a819090d74d53dfa2fc2a001160a976220ffeaaf1582
|
|
7
|
+
data.tar.gz: 35c00cc9a3c8b2a2f9387afe73f54cf2b4859a9e7565409fbfdf81448485bdd8df2fbe8e4c05a3de31428ad02f976bbc1e411cb13a684fe0f059a4d62c104b22
|
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
# Google Search Result for Ruby
|
|
2
|
+
# powered by SerpApi.com
|
|
3
|
+
#
|
|
4
|
+
# Usage
|
|
5
|
+
# ```
|
|
6
|
+
#query_params = {
|
|
7
|
+
# q: "query",
|
|
8
|
+
# google_domain: "Google Domain",
|
|
9
|
+
# location: "Location Requested",
|
|
10
|
+
# device: device,
|
|
11
|
+
# hl: "Google UI Language",
|
|
12
|
+
# gl: "Google Country",
|
|
13
|
+
# safe: "Safe Search Flag",
|
|
14
|
+
# num: "Number of Results",
|
|
15
|
+
# start: "Pagination Offset",
|
|
16
|
+
# serp_api_key: "Your SERP API Key"
|
|
17
|
+
# }
|
|
18
|
+
#
|
|
19
|
+
# query = GoogleSearchResults.new(query_params)
|
|
20
|
+
# query.params[:location] = "Portland"
|
|
21
|
+
#
|
|
22
|
+
# html_results = query.get_html
|
|
23
|
+
# hash_results = query.get_hash
|
|
24
|
+
# json_results = query.get_json
|
|
25
|
+
# ```
|
|
1
26
|
# encoding: UTF-8
|
|
2
27
|
|
|
3
28
|
require 'open-uri'
|
|
@@ -6,7 +31,7 @@ require_relative 'google_search_results/hash'
|
|
|
6
31
|
|
|
7
32
|
class GoogleSearchResults
|
|
8
33
|
|
|
9
|
-
VERSION = "1.0
|
|
34
|
+
VERSION = "1.1.0"
|
|
10
35
|
BACKEND = "serpapi.com"
|
|
11
36
|
|
|
12
37
|
@@serp_api_key = nil
|
|
@@ -17,10 +42,40 @@ class GoogleSearchResults
|
|
|
17
42
|
|
|
18
43
|
attr_accessor :params
|
|
19
44
|
|
|
20
|
-
|
|
45
|
+
# constructor
|
|
46
|
+
#
|
|
47
|
+
# Usage
|
|
48
|
+
# ---
|
|
49
|
+
# ```require 'google_search_results'
|
|
50
|
+
# query = GoogleSearchResults.new({q: "coffee", serp_api_key: "Your SERP API Key")
|
|
51
|
+
# result = query.get_json```
|
|
52
|
+
#
|
|
53
|
+
def initialize(params = {})
|
|
21
54
|
@params = params
|
|
22
55
|
end
|
|
23
56
|
|
|
57
|
+
# get_json
|
|
58
|
+
# @return [Hash] search result "json like"
|
|
59
|
+
# where keys are String
|
|
60
|
+
def get_json
|
|
61
|
+
@params[:output] = "json"
|
|
62
|
+
get_results
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# get_html
|
|
66
|
+
# @return [String] raw html
|
|
67
|
+
def get_html
|
|
68
|
+
@params[:output] = "html"
|
|
69
|
+
get_results
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# get_html
|
|
73
|
+
# @return [Hash] search result Ruby hash
|
|
74
|
+
# where keys are Symbol
|
|
75
|
+
def get_hash
|
|
76
|
+
JSON.load(get_json).symbolize_all_keys
|
|
77
|
+
end
|
|
78
|
+
|
|
24
79
|
def construct_url
|
|
25
80
|
@params[:source] = "ruby"
|
|
26
81
|
if @params[:serp_api_key].nil? and GoogleSearchResults.serp_api_key
|
|
@@ -29,11 +84,13 @@ class GoogleSearchResults
|
|
|
29
84
|
URI::HTTPS.build(host: BACKEND, path: '/search', query: URI.encode_www_form(@params))
|
|
30
85
|
end
|
|
31
86
|
|
|
87
|
+
private
|
|
88
|
+
|
|
32
89
|
def get_results
|
|
33
90
|
begin
|
|
34
91
|
open(construct_url, read_timeout: 600).read
|
|
35
92
|
rescue OpenURI::HTTPError => e
|
|
36
|
-
if error = JSON.load(e.io.read)["error"]
|
|
93
|
+
if error = JSON.load(e.io.read)["error"]
|
|
37
94
|
raise error
|
|
38
95
|
else
|
|
39
96
|
raise e
|
|
@@ -41,27 +98,5 @@ class GoogleSearchResults
|
|
|
41
98
|
end
|
|
42
99
|
end
|
|
43
100
|
|
|
44
|
-
|
|
45
|
-
@params[:output] = "html"
|
|
46
|
-
get_results
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def get_json
|
|
50
|
-
@params[:output] = "json"
|
|
51
|
-
get_results
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def get_json_with_images
|
|
55
|
-
@params[:output] = "json_with_images"
|
|
56
|
-
get_results
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def get_hash
|
|
60
|
-
JSON.load(get_json).symbolize_all_keys
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def get_hash_with_images
|
|
64
|
-
JSON.load(get_json_with_images).symbolize_all_keys
|
|
65
|
-
end
|
|
101
|
+
end
|
|
66
102
|
|
|
67
|
-
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: google_search_results
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- hartator
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2019-02-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -50,7 +50,8 @@ files:
|
|
|
50
50
|
homepage: https://github.com/serpapi/google-search-results-ruby
|
|
51
51
|
licenses:
|
|
52
52
|
- MIT
|
|
53
|
-
metadata:
|
|
53
|
+
metadata:
|
|
54
|
+
yard.run: yri
|
|
54
55
|
post_install_message:
|
|
55
56
|
rdoc_options: []
|
|
56
57
|
require_paths:
|