-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathREADME.md.erb
More file actions
584 lines (454 loc) · 22.6 KB
/
README.md.erb
File metadata and controls
584 lines (454 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<%-
def snippet(format, path, demo = false)
lines = File.new(path).readlines
if demo
# skip comment
buf = lines.reject { |line| line =~ /^\s*#|require 'serpapi'/ }[2..-1].join
else
# select test from line start to stop
start = 9
stop = lines.find_index { |line| line =~ /pp results/}
slice = File.new(path).readlines[start..stop+1]
slice.reject! { |l| l =~ /expect\(|^\s*describe|^\s+it|^\s*end\s*$/ }
slice.map! { |l| l.gsub(/(^\s\s\s\s)/, '')}
buf = slice.join
buf.gsub!('# pp ', 'pp ')
buf.gsub!('api_key)', "ENV['SERPAPI_KEY'])")
end
%Q(```#{format}\nrequire 'serpapi'\n#{buf}```\n\n * source code: [#{path}](https://github.com/serpapi/serpapi-ruby/blob/master/#{path}))
end
-%>
# SerpApi Ruby Library
[](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml) [](https://badge.fury.io/rb/serpapi)
Integrate search data into your AI workflow, RAG / fine-tuning, or Ruby application using this official wrapper for [SerpApi](https://serpapi.com).
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and [more](https://serpapi.com).
Query a vast range of data at scale, including web search results, flight schedules, stock market data, news headlines, and [more](https://serpapi.com).
## Features
* `persistent` → Keep socket connection open to save on SSL handshake / reconnection (2x faster). [Search at scale](#Search-At-Scale)
* `async` → Support non-blocking job submission. [Search Asynchronous](#Search-Asynchronous)
* extensive documentation → easy to follow
* real world examples → included throughout
## Installation
Ruby 2.7 and higher are supported. To achieve an optimal performance, the latest version is recommended. Check 2.7.8 vs 3.4.4 [performance comparison](#Performance-Comparison).
Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with [legacy SerpApi library](https://github.com/serpapi/google-search-results-ruby), which is still supported. To upgrade to the latest library, check our [migration guide](#Migration-quick-guide).
### Bundler
```ruby
gem 'serpapi', '~> 1.0', '>= 1.0.3'
```
### Gem
```bash
$ gem install serpapi
```
[Ruby Gem page](https://rubygems.org/gems/serpapi/)
## Simple Usage
```ruby
require 'serpapi'
client = SerpApi::Client.new(engine: "google", api_key: "<SERPAPI_KEY>")
results = client.search(q: "coffee")
pp results
```
This example runs a search for "coffee" on Google. It then returns the results as a regular Ruby Hash.
See the [playground](https://serpapi.com/playground) to generate your own code.
The SerpApi key can be obtained from [serpapi.com/signup](https://serpapi.com/users/sign_up?plan=free).
Environment variables are a secure, safe, and easy way to manage secrets.
Set `export SERPAPI_KEY=<secret_serpapi_key>` in your shell.
Ruby accesses these variables from `ENV['SERPAPI_KEY']`.
## Search API advanced usage with Google search engine
This example dives into all the available parameters for the Google search engine.
The list of parameters depends on the chosen search engine.
```ruby
# load gem
require 'serpapi'
# serpapi client created with default parameters
client = SerpApi::Client.new(
engine: 'google',
api_key: ENV['SERPAPI_KEY'],
# HTTP client configuration
async: false, # non-blocking HTTP request see: Search Asynchronous (default: false)
persistent: true, # leave socket connection open for faster response time see: Search at scale (default: true)
timeout: 5, # HTTP timeout in seconds on the client side only. (default: 120s)
symbolize_names: true # turn on/off JSON keys to symbols (default: on, more efficient)
)
# search query overview (more fields available depending on search engine)
params = {
# overview of parameter for Google search engine which is one of many search engine supported.
# select the search engine (full list: https://serpapi.com/)
engine: "google",
# actual search query
q: "Coffee",
# then adds search engine specific options.
# for example: google specific parameters: https://serpapi.com/search-api
google_domain: "Google Domain",
# example: Portland,Oregon,United States [ * doc: Location API](#Location-API)
location: "Location Requested",
device: "desktop|mobile|tablet",
hl: "Google UI Language",
gl: "Google Country",
safe: "Safe Search Flag",
start: "Pagination Offset",
tbm: "nws|isch|shop",
tbs: "custom to be client criteria",
}
# search results as a symbolized Hash (per performance)
results = client.search(params)
# search results as a raw HTML string
raw_html = client.html(params)
```
→ [SerpApi documentation](https://serpapi.com/search-api).
#### Documentations
This library is well documented, and you can find the following resources:
* [Full documentation on SerpApi.com](https://serpapi.com)
* [Library Github page](https://github.com/serpapi/serpapi-ruby)
* [Library GEM page](https://rubygems.org/gems/serpapi/)
* [Library API documentation](https://rubydoc.info/github/serpapi/serpapi-ruby/master)
* [API health status](https://serpapi.com/status)
## Advanced search API usage
### Search Asynchronous
Search API features non-blocking search using the option: `async=true`.
- Non-blocking - async=true - a single parent process can handle unlimited concurrent searches.
- Blocking - async=false - many processes must be forked and synchronized to handle concurrent searches. This strategy is I/O usage because each client would hold a network connection.
Search API enables `async` search.
- Non-blocking (`async=true`) : the development is more complex, but this allows handling many simultaneous connections.
- Blocking (`async=false`) : it is easy to write the code but more compute-intensive when the parent process needs to hold many connections.
Here is an example of asynchronous searches using Ruby
```ruby
require 'serpapi'
company_list = %w[meta amazon apple netflix google]
client = SerpApi::Client.new(engine: 'google', async: true, persistent: true, api_key: ENV['SERPAPI_KEY'])
schedule_search = Queue.new
result = nil
company_list.each do |company|
result = client.search(q: company)
puts "#{company}: search results found in cache for: #{company}" if result[:search_metadata][:status] =~ /Cached/
schedule_search.push(result[:search_metadata][:id])
end
puts "Last search submited at: #{result[:search_metadata][:created_at]}"
puts 'wait 10s for all requests to be completed '
sleep(10)
puts 'wait until all searches are cached or success'
until schedule_search.empty?
search_id = schedule_search.pop
search_archived = client.search_archive(search_id)
company = search_archived[:search_parameters][:q]
if search_archived[:search_metadata][:status] =~ /Cached|Success/
puts "#{search_archived[:search_parameters][:q]}: search results found in archive for: #{company}"
next
end
schedule_search.push(search_id)
end
schedule_search.close
puts 'done'
```
* source code: [demo/demo_async.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_async.rb)
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)). Each search may take up to few seconds to complete. By the time the first element pops out of the queue, the search results might already be available in the archive. If not, the `search_archive` method blocks until the search results are available.
### Search at scale
The provided code snippet is a Ruby spec test case that demonstrates the use of thread pools to execute multiple HTTP requests concurrently.
```ruby
require 'serpapi'
require 'connection_pool'
# create a thread pool of 4 threads with a persistent connection to serpapi.com
pool = ConnectionPool.new(size: n, timeout: 5) do
SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'], timeout: 30, persistent: true)
end
# run user thread to search for your favorites coffee type
threads = %w(latte espresso cappuccino americano mocha macchiato frappuccino cold_brew).map do |query|
Thread.new do
pool.with { |socket| socket.search({q: query }).to_s }
end
end
responses = threads.map(&:value)
```
The code aims to demonstrate how thread pools can be used to
improve performance by executing multiple tasks concurrently. In
this case, it makes multiple HTTP requests to an API endpoint using
a thread pool of persistent connections.
Note: `gem install connection_pool` to run this example.
**Benefits:**
* Improved performance by avoiding the overhead of creating and destroying connections for each request.
* Efficient use of resources by sharing connections among multiple threads.
* Concurrency and parallelism, allowing multiple requests to be processed simultaneously.
benchmark: (demo/demo_thread_pool.rb)
** Benchmark Ruby 3.4.8 vs Ruby 4.0.0 **
benchmark: (demo/demo_thread_pool.rb)
| ruby | runtime (s) | thread | time/thread (s) |
|------|------------:|-------:|----------------:|
| 3.4.8 | 0.018644 | 4 | 0.004661 |
| 4.0.0 | 0.017302 | 4 | 0.004326 |
Ruby 4.0.0 shows a slight improvement over Ruby 3.4.8, but the difference is not significant using thread.
Ractor could be considered for a more efficient use of resources but it's still in the experimental stage.
Note: in this benchmark, `thread == HTTP connections`.
### Real world search without persistency
```ruby
require 'serpapi'
require 'pp'
client = SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])
params = {
q: 'coffee'
}
results = client.search(params)
unless results[:organic_results]
puts 'no organic results found'
exit 1
end
pp results[:organic_results]
puts 'done'
exit 0
```
* source code: [demo/demo.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo.rb)
## APIs supported
### Location API
```ruby
require 'serpapi'
client = SerpApi::Client.new
location_list = client.location(q: "Austin", limit: 3)
puts "number of location: #{location_list.size}"
pp location_list
```
it prints the first 3 locations matching Austin (Texas, Texas, Rochester)
```ruby
[{
:id=>"585069bdee19ad271e9bc072",
:google_id=>200635,
:google_parent_id=>21176,
:name=>"Austin, TX",
:canonical_name=>"Austin,TX,Texas,United States",
:country_code=>"US",
:target_type=>"DMA Region",
:reach=>5560000,
:gps=>[-97.7430608, 30.267153],
:keys=>["austin", "tx", "texas", "united", "states"]
}
# ...
]
```
NOTE: api_key is not required for this endpoint.
### Search Archive API
This API allows retrieving previous search results.
To fetch earlier results from the search_id.
First, you need to run a search and save the search ID.
```ruby
require 'serpapi'
client = SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'])
results = client.search(q: "Coffee", location: "Portland")
search_id = results[:search_metadata][:id]
```
Now we can retrieve the previous search results from the archive using the search ID (free of charge).
```ruby
require 'serpapi'
client = SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])
results = client.search_archive(search_id)
pp results
```
This code prints the search results from the archive. :)
### Account API
```ruby
require 'serpapi'
client = SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])
pp client.account
```
It prints your account information as:
```ruby
{
account_id: "1234567890",
api_key: "your_secret_key",
account_email: "email@company.com",
account_status: "Active",
plan_id: "free",
plan_name: "Free Plan",
plan_monthly_price: 0.0,
searches_per_month: 250,
plan_searches_left: 250,
extra_credits: 0,
total_searches_left: 250,
this_month_usage: 0,
this_hour_searches: 0,
last_hour_searches: 0,
account_rate_limit_per_hour: 250
}
```
## Basic example per search engine
### Search google
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_spec.rb') %>
see: [https://serpapi.com/search-api](https://serpapi.com/search-api)
### Search google light
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_light_spec.rb') %>
see: [https://serpapi.com/google-light-api](https://serpapi.com/google-light-api)
### Search google scholar
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_scholar_spec.rb') %>
see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api)
### Search google autocomplete
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_autocomplete_spec.rb') %>
see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api)
### Search google product
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_product_spec.rb') %>
see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api)
### Search google reverse image
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_reverse_image_spec.rb') %>
see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image)
### Search google events
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_events_spec.rb') %>
see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api)
### Search google local services
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_local_services_spec.rb') %>
see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api)
### Search google maps
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_maps_spec.rb') %>
see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)
### Search google jobs
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_jobs_spec.rb') %>
see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
### Search google play
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_play_spec.rb') %>
see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)
### Search google images
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_spec.rb') %>
see: [https://serpapi.com/images-results](https://serpapi.com/images-results)
### Search google lens
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_lens_spec.rb') %>
see: [https://serpapi.com/google-lens-api](https://serpapi.com/google-lens-api)
### Search google images light
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_light_spec.rb') %>
see: [https://serpapi.com/google-images-light-api](https://serpapi.com/google-images-light-api)
### Search google hotels
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_hotels_spec.rb') %>
see: [https://serpapi.com/google-hotels-api](https://serpapi.com/google-hotels-api)
### Search google flights
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_flights_spec.rb') %>
see: [https://serpapi.com/google-flights-api](https://serpapi.com/google-flights-api)
### Search google finance
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_finance_spec.rb') %>
see: [https://serpapi.com/google-finance-api](https://serpapi.com/google-finance-api)
### Search google ai overview
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_ai_overview_spec.rb') %>
see: [https://serpapi.com/google-ai-overview-api](https://serpapi.com/google-ai-overview-api)
### Search google news
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_spec.rb') %>
see: [https://serpapi.com/google-news-api](https://serpapi.com/google-news-api)
### Search google news light
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_light_spec.rb') %>
see: [https://serpapi.com/google-news-light-api](https://serpapi.com/google-news-light-api)
### Search google patents
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_patents_spec.rb') %>
see: [https://serpapi.com/google-patents-api](https://serpapi.com/google-patents-api)
### Search google trends
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_trends_spec.rb') %>
see: [https://serpapi.com/google-trends-api](https://serpapi.com/google-trends-api)
### Search google shopping
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_shopping_spec.rb') %>
see: [https://serpapi.com/google-shopping-api](https://serpapi.com/google-shopping-api)
### Search google immersive product
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_immersive_product_spec.rb') %>
see: [https://serpapi.com/google-immersive-product-api](https://serpapi.com/google-immersive-product-api)
### Search google videos
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_videos_spec.rb') %>
see: [https://serpapi.com/google-videos-api](https://serpapi.com/google-videos-api)
### Search amazon
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_amazon_spec.rb') %>
see: [https://serpapi.com/amazon-search-api](https://serpapi.com/amazon-search-api)
### Search baidu
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_baidu_spec.rb') %>
see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api)
### Search yahoo
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yahoo_spec.rb') %>
see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api)
### Search youtube
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_youtube_spec.rb') %>
see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api)
### Search walmart
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_walmart_spec.rb') %>
see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api)
### Search ebay
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_ebay_spec.rb') %>
see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)
### Search naver
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_naver_spec.rb') %>
see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api)
### Search home depot
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_home_depot_spec.rb') %>
see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api)
### Search apple app store
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_apple_app_store_spec.rb') %>
see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)
### Search duckduckgo
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_duckduckgo_spec.rb') %>
see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api)
### Search yandex
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yandex_spec.rb') %>
see: [https://serpapi.com/yandex-search-api](https://serpapi.com/yandex-search-api)
### Search yelp
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yelp_spec.rb') %>
see: [https://serpapi.com/yelp-search-api](https://serpapi.com/yelp-search-api)
## Performance Comparison
### Ruby 4.0.0 vs 3.4.4 vs Ruby 2.7.8 Performance
| Metric | Ruby 2.7.8 | Ruby 3.4.4 | Ruby 4.0.0 | Improvement (3.4.4 vs 2.7.8) | Improvement (4.0.0 vs 3.4.4) |
|--------|------------|------------|------------|------------------------------|------------------------------|
| **SerpApi Non-Persistent** | 100.93 req/s | 114.97 req/s | 120.09 req/s | **+13.9%** | **+4.5%** |
| **SerpApi Persistent** | 226.82 req/s | 255.07 req/s | 296.05 req/s | **+12.4%** | **+16.1%** |
| **HTTP.rb Non-Persistent** | 270.62 req/s | 294.01 req/s | 319.81 req/s | **+8.6%** | **+8.8%** |
| **HTTP.rb Persistent** | 347.04 req/s | 570.95 req/s | 456.93 req/s | **+64.5%** | **-20.0%** |
### Key Takeaways
1. **Upgrade to Ruby 3.4.4**: Clear performance benefits across all scenarios
2. **Use Persistent Connections**: 2x+ performance improvement in most cases
3. **HTTP.rb Performance**: Particularly benefits from Ruby 3.4.4 with persistent connections
4. **SerpApi Optimization**: Shows consistent ~2.2x improvement with persistent connections regardless of Ruby version
5. **Ruby 4.0.0 Performance**: Shows mixed results with some regressions compared to 3.4.4, particularly for HTTP.rb persistent connections. Ruby 4.0.0 was just released for Christmas 2025, and HTTP.rb has not been optimized for it yet.
The older library (google-search-results-ruby) was performing at 55 req/s on Ruby 2.7.8, which is 2x slower than the current version (serpapi-ruby) on Ruby 3.4.4 or 4.0.0.
**Context** This benchmark was performed on warmup search results using a MacBook Pro 2025 connected via Wi-Fi 6.0 home network on AT&T fiber from Austin, TX (no network optimization).
## Migration quick guide
If you were already using [google-search-results-ruby gem](https://github.com/serpapi/google-search-results-ruby), here are the changes.
```
# load library
# old way
require 'google_search_results'
# new way
require 'serpapi'
# define a search
# old way to describe the search
search = GoogleSearch.new(search_params)
# new way
default_parameter = {api_key: "secret_key", engine: "google"}
client = SerpApi::Client.new(default_parameter)
# an instance of the serpapi client is created
# where the default parameters are stored in the client.
# like api_key, engine
# then each subsequent API call can be made with additional parameters.
# override an existing parameter
# old way
search.params[:location] = "Portland,Oregon,United States"
# new way
# just provided the search call with the parameters.
results = client.search({location: "Portland,Oregon,United States", q: "Coffee"})
# search format return as raw html
# old way
html_results = search.get_html
# new way
raw_html = client.html(params)
# where params is Hash containing additional key / value
# search format returns a Hash
# old way
hash_results = search.get_hash
# new way
results = client.search(params)
# where params is the search parameters (override the default search parameters in the constructor).
# search as raw JSON format
# old way
json_results = search.get_json
# new way
results = client.search(params)
# The prefix get_ is removed from all other methods.
# Because it's evident that a method returns something.
# old -> new way
search.get_search_archive -> client.search_archive
search.get_account -> client.account
search.get_location -> client.location
```
Most notable improvements:
- Removing parameters check on the client side. (most of the bugs)
- Reduce logic complexity in our implementation. (faster performance)
- Better documentation.
## Supported Ruby versions
Ruby 2.7 and higher is supported.
## Change logs
* [2025-11-17] 1.0.2 Implement `inspect` functions for client
* [2025-07-18] 1.0.1 Add support for old Ruby versions (2.7, 3.0)
* [2025-07-01] 1.0.0 Full API support