google_search_results 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c9cec2b3a527d7088744a0189c2d57bc324b3bec
4
- data.tar.gz: d9920122de8d07df6663ca0794eee0a8a4f33572
2
+ SHA256:
3
+ metadata.gz: 981326cdb315dc996ca78711369407131f295719296498613d6c8003c11afeb7
4
+ data.tar.gz: 0f9d30de883d5db7aff06274a098716103544696283ed1bc85e533a756b551c0
5
5
  SHA512:
6
- metadata.gz: 4096606c2a209617653af427213de99d88a419aac4e7aa6c3c5cd28ce0f43af6d716f552aa9b31b710f7a819090d74d53dfa2fc2a001160a976220ffeaaf1582
7
- data.tar.gz: 35c00cc9a3c8b2a2f9387afe73f54cf2b4859a9e7565409fbfdf81448485bdd8df2fbe8e4c05a3de31428ad02f976bbc1e411cb13a684fe0f059a4d62c104b22
6
+ metadata.gz: 7519258d49f11585846f80fc7f29909faf619e86b75d1680c60e6e63e15afec7f3ff7071d960c9954b97a67e1746da90ea1ea6cdd3ba9066524123605fd774c5
7
+ data.tar.gz: 8a156b74c5a054b07bd8f41a8ca757ce11a8246f837987da6dd1a6545739a214f22a1b6e6cdc76e28c2cf53cf720f92064a84bea60af34e2f0d51f3879380191
@@ -1,9 +1,8 @@
1
- # Google Search Result for Ruby
2
- # powered by SerpApi.com
1
+ # Google Search Result for Ruby powered by SerpAPI
3
2
  #
4
- # Usage
3
+ # Search API Usage
5
4
  # ```
6
- #query_params = {
5
+ # parameter = {
7
6
  # q: "query",
8
7
  # google_domain: "Google Domain",
9
8
  # location: "Location Requested",
@@ -13,25 +12,26 @@
13
12
  # safe: "Safe Search Flag",
14
13
  # num: "Number of Results",
15
14
  # start: "Pagination Offset",
15
+ # tbm: "to be matched field",
16
+ # tbs: "to be searched field",
16
17
  # serp_api_key: "Your SERP API Key"
17
18
  # }
18
19
  #
19
- # query = GoogleSearchResults.new(query_params)
20
- # query.params[:location] = "Portland"
20
+ # client = GoogleSearchResults.new(parameter)
21
+ # client.params[:location] = "Portland"
21
22
  #
22
- # html_results = query.get_html
23
- # hash_results = query.get_hash
24
- # json_results = query.get_json
23
+ # html_results = client.get_html
24
+ # hash_results = client.get_hash
25
+ # json_results = client.get_json
25
26
  # ```
26
27
  # encoding: UTF-8
27
28
 
28
29
  require 'open-uri'
29
30
  require 'json'
30
- require_relative 'google_search_results/hash'
31
31
 
32
32
  class GoogleSearchResults
33
33
 
34
- VERSION = "1.1.0"
34
+ VERSION = "1.2.0"
35
35
  BACKEND = "serpapi.com"
36
36
 
37
37
  @@serp_api_key = nil
@@ -47,8 +47,8 @@ class GoogleSearchResults
47
47
  # Usage
48
48
  # ---
49
49
  # ```require 'google_search_results'
50
- # query = GoogleSearchResults.new({q: "coffee", serp_api_key: "Your SERP API Key")
51
- # result = query.get_json```
50
+ # client = GoogleSearchResults.new({q: "coffee", serp_api_key: "Your SERP API Key")
51
+ # result = client.get_json```
52
52
  #
53
53
  def initialize(params = {})
54
54
  @params = params
@@ -59,36 +59,68 @@ class GoogleSearchResults
59
59
  # where keys are String
60
60
  def get_json
61
61
  @params[:output] = "json"
62
- get_results
62
+ get_results('/search')
63
63
  end
64
64
 
65
65
  # get_html
66
66
  # @return [String] raw html
67
67
  def get_html
68
68
  @params[:output] = "html"
69
- get_results
69
+ get_results('/search')
70
70
  end
71
71
 
72
72
  # get_html
73
73
  # @return [Hash] search result Ruby hash
74
74
  # where keys are Symbol
75
75
  def get_hash
76
- JSON.load(get_json).symbolize_all_keys
76
+ JSON.parse(get_json, {symbolize_names: true})
77
77
  end
78
78
 
79
- def construct_url
79
+ # alias for get_hash
80
+ # @deprecated
81
+ def get_hash_with_images
82
+ get_hash
83
+ end
84
+
85
+ # alias for get_json
86
+ # @deprecated
87
+ def get_json_with_images
88
+ get_json
89
+ end
90
+
91
+ # Get location using Location API
92
+ def get_location
93
+ JSON.parse(get_results('/locations.json'), {symbolize_names: true})
94
+ end
95
+
96
+ # Retrieve search result from the Search Archive API
97
+ def get_search_archive(search_id, format = 'json')
98
+ raise 'format must be json or html' if format !~ /^(html|json)$/
99
+ JSON.parse(get_results("/searches/#{search_id}.#{format}"), {symbolize_names: true})
100
+ end
101
+
102
+ # Get account information using Account API
103
+ def get_account
104
+ JSON.parse(get_results('/account'), {symbolize_names: true})
105
+ end
106
+
107
+ def construct_url(path)
80
108
  @params[:source] = "ruby"
81
- if @params[:serp_api_key].nil? and GoogleSearchResults.serp_api_key
82
- @params[:serp_api_key] = GoogleSearchResults.serp_api_key
109
+ if GoogleSearchResults.serp_api_key
110
+ @params[:serp_api_key] ||= GoogleSearchResults.serp_api_key
111
+ end
112
+ if @params[:serp_api_key].nil?
113
+ @params.delete(:serp_api_key)
83
114
  end
84
- URI::HTTPS.build(host: BACKEND, path: '/search', query: URI.encode_www_form(@params))
115
+
116
+ URI::HTTPS.build(host: BACKEND, path: path, query: URI.encode_www_form(@params))
85
117
  end
86
118
 
87
119
  private
88
120
 
89
- def get_results
121
+ def get_results(path)
90
122
  begin
91
- open(construct_url, read_timeout: 600).read
123
+ open(construct_url(path), read_timeout: 600).read
92
124
  rescue OpenURI::HTTPError => e
93
125
  if error = JSON.load(e.io.read)["error"]
94
126
  raise error
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hartator
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -25,28 +25,26 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '10.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: minitest
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: '3.8'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
41
- description: Get Google Search Results via SERP API. Hash, JSON, and HTML outputs
42
- supported.
40
+ version: '3.8'
41
+ description: Get Google Search Results via SERP API. Hash, JSON, and HTML format supported.
43
42
  email: hartator@gmail.com
44
43
  executables: []
45
44
  extensions: []
46
45
  extra_rdoc_files: []
47
46
  files:
48
47
  - lib/google_search_results.rb
49
- - lib/google_search_results/hash.rb
50
48
  homepage: https://github.com/serpapi/google-search-results-ruby
51
49
  licenses:
52
50
  - MIT
@@ -67,8 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
65
  - !ruby/object:Gem::Version
68
66
  version: '0'
69
67
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.6.14
68
+ rubygems_version: 3.0.3
72
69
  signing_key:
73
70
  specification_version: 4
74
71
  summary: Google Search Results via SERP API.
@@ -1,18 +0,0 @@
1
- class Hash
2
- def symbolize_all_keys(hash=self)
3
- symbolized_hash = {}
4
- return hash if hash.is_a?(String)
5
- hash.each do |k, v|
6
- if v.is_a?(Array)
7
- symbolized_hash[k.to_sym] = v.map do |e|
8
- symbolize_all_keys(e)
9
- end
10
- elsif v.is_a?(Hash)
11
- symbolized_hash[k.to_sym] = symbolize_all_keys(v)
12
- else
13
- symbolized_hash[k.to_sym] = v
14
- end
15
- end
16
- symbolized_hash
17
- end
18
- end