Class: ShopsavvyDataApi::Client
- Inherits:
-
Object
- Object
- ShopsavvyDataApi::Client
- Defined in:
- lib/shopsavvy_data_api/client.rb
Overview
Official Ruby client for ShopSavvy Data API
Provides access to product data, pricing information, and price history across thousands of retailers and millions of products.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#batch_lookup(identifiers, include: nil) ⇒ Object
Look up multiple products at once (sync for <=20, async for >20).
- #create_webhook(url, events:) ⇒ Object
- #delete_webhook(webhook_id) ⇒ Object
-
#get_batch_status(batch_id) ⇒ Object
Poll for async batch job results.
-
#get_current_offers(identifier, retailer: nil, format: nil) ⇒ APIResponse<Array<ProductWithOffers>>
Get current offers for a product.
-
#get_current_offers_batch(identifiers, retailer: nil, format: nil) ⇒ APIResponse<Array<ProductWithOffers>>
Get current offers for multiple products.
-
#get_deals(sort: "hot", limit: 25, offset: 0, **options) ⇒ Hash
Browse current shopping deals.
-
#get_price_history(identifier, start_date, end_date, retailer: nil, format: nil) ⇒ APIResponse<Array<OfferWithHistory>>
Get price history for a product.
-
#get_product_details(identifier, format: nil) ⇒ APIResponse<Array<ProductDetails>>
Look up product details by identifier.
-
#get_product_details_batch(identifiers, format: nil) ⇒ APIResponse<Array<ProductDetails>>
Look up details for multiple products.
-
#get_product_review(identifier) ⇒ Hash
Get TLDR review for a product (pros, cons, scores).
-
#get_scheduled_products ⇒ APIResponse<Array<ScheduledProduct>>
Get all scheduled products.
-
#get_usage ⇒ APIResponse<UsageInfo>
Get API usage information.
-
#initialize(config = nil, api_key: nil, base_url: nil, timeout: nil) ⇒ Client
constructor
Initialize a new client.
- #list_webhooks ⇒ Object
-
#remove_product_from_schedule(identifier) ⇒ APIResponse<Hash>
Remove product from monitoring schedule.
-
#remove_products_from_schedule(identifiers) ⇒ APIResponse<Array<Hash>>
Remove multiple products from monitoring schedule.
-
#schedule_product_monitoring(identifier, frequency, retailer: nil) ⇒ APIResponse<Hash>
Schedule product monitoring.
-
#schedule_product_monitoring_batch(identifiers, frequency, retailer: nil) ⇒ APIResponse<Array<Hash>>
Schedule monitoring for multiple products.
-
#search_products(query, limit: nil, offset: nil) ⇒ ProductSearchResult
Search for products by keyword.
- #test_webhook(webhook_id) ⇒ Object
Constructor Details
#initialize(config = nil, api_key: nil, base_url: nil, timeout: nil) ⇒ Client
Initialize a new client
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/shopsavvy_data_api/client.rb', line 33 def initialize(config = nil, api_key: nil, base_url: nil, timeout: nil) @config = if config.is_a?(Configuration) config elsif config.is_a?(Hash) Configuration.new(**config) elsif api_key Configuration.new( api_key: api_key, base_url: base_url || "https://api.shopsavvy.com/v1", timeout: timeout || 30 ) else raise ConfigurationError, "Either config or api_key must be provided" end @connection = build_connection end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/shopsavvy_data_api/client.rb', line 25 def config @config end |
Instance Method Details
#batch_lookup(identifiers, include: nil) ⇒ Object
Look up multiple products at once (sync for <=20, async for >20)
277 278 279 280 281 |
# File 'lib/shopsavvy_data_api/client.rb', line 277 def batch_lookup(identifiers, include: nil) body = { identifiers: identifiers } body[:include] = include if include make_request(:post, "products/batch", body: body) end |
#create_webhook(url, events:) ⇒ Object
288 289 290 |
# File 'lib/shopsavvy_data_api/client.rb', line 288 def create_webhook(url, events:) make_request(:post, "webhooks", body: { url: url, events: events }) end |
#delete_webhook(webhook_id) ⇒ Object
300 301 302 |
# File 'lib/shopsavvy_data_api/client.rb', line 300 def delete_webhook(webhook_id) make_request(:delete, "webhooks/#{webhook_id}") end |
#get_batch_status(batch_id) ⇒ Object
Poll for async batch job results
284 285 286 |
# File 'lib/shopsavvy_data_api/client.rb', line 284 def get_batch_status(batch_id) make_request(:get, "batch/#{batch_id}") end |
#get_current_offers(identifier, retailer: nil, format: nil) ⇒ APIResponse<Array<ProductWithOffers>>
Get current offers for a product
117 118 119 120 121 122 123 124 |
# File 'lib/shopsavvy_data_api/client.rb', line 117 def get_current_offers(identifier, retailer: nil, format: nil) params = { ids: identifier } params[:retailer] = retailer if retailer params[:format] = format if format response = make_request(:get, "products/offers", params: params) APIResponse.new(response, data_class: ProductWithOffers) end |
#get_current_offers_batch(identifiers, retailer: nil, format: nil) ⇒ APIResponse<Array<ProductWithOffers>>
Get current offers for multiple products
132 133 134 135 136 137 138 139 |
# File 'lib/shopsavvy_data_api/client.rb', line 132 def get_current_offers_batch(identifiers, retailer: nil, format: nil) params = { ids: identifiers.join(",") } params[:retailer] = retailer if retailer params[:format] = format if format response = make_request(:get, "products/offers", params: params) APIResponse.new(response, data_class: ProductWithOffers) end |
#get_deals(sort: "hot", limit: 25, offset: 0, **options) ⇒ Hash
Browse current shopping deals
262 263 264 265 |
# File 'lib/shopsavvy_data_api/client.rb', line 262 def get_deals(sort: "hot", limit: 25, offset: 0, **) params = { sort: sort, limit: limit, offset: offset }.merge().compact make_request(:get, "deals", params: params) end |
#get_price_history(identifier, start_date, end_date, retailer: nil, format: nil) ⇒ APIResponse<Array<OfferWithHistory>>
Get price history for a product
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/shopsavvy_data_api/client.rb', line 155 def get_price_history(identifier, start_date, end_date, retailer: nil, format: nil) params = { ids: identifier, start_date: start_date, end_date: end_date } params[:retailer] = retailer if retailer params[:format] = format if format response = make_request(:get, "products/offers/history", params: params) APIResponse.new(response, data_class: OfferWithHistory) end |
#get_product_details(identifier, format: nil) ⇒ APIResponse<Array<ProductDetails>>
Look up product details by identifier
79 80 81 82 83 84 85 |
# File 'lib/shopsavvy_data_api/client.rb', line 79 def get_product_details(identifier, format: nil) params = { ids: identifier } params[:format] = format if format response = make_request(:get, "products", params: params) APIResponse.new(response, data_class: ProductDetails) end |
#get_product_details_batch(identifiers, format: nil) ⇒ APIResponse<Array<ProductDetails>>
Look up details for multiple products
96 97 98 99 100 101 102 |
# File 'lib/shopsavvy_data_api/client.rb', line 96 def get_product_details_batch(identifiers, format: nil) params = { ids: identifiers.join(",") } params[:format] = format if format response = make_request(:get, "products", params: params) APIResponse.new(response, data_class: ProductDetails) end |
#get_product_review(identifier) ⇒ Hash
Get TLDR review for a product (pros, cons, scores)
270 271 272 |
# File 'lib/shopsavvy_data_api/client.rb', line 270 def get_product_review(identifier) make_request(:get, "products/reviews", params: { id: identifier }) end |
#get_scheduled_products ⇒ APIResponse<Array<ScheduledProduct>>
Get all scheduled products
213 214 215 216 |
# File 'lib/shopsavvy_data_api/client.rb', line 213 def get_scheduled_products response = make_request(:get, "products/scheduled") APIResponse.new(response, data_class: ScheduledProduct) end |
#get_usage ⇒ APIResponse<UsageInfo>
Get API usage information
251 252 253 254 |
# File 'lib/shopsavvy_data_api/client.rb', line 251 def get_usage response = make_request(:get, "usage") APIResponse.new(response, data_class: UsageInfo) end |
#list_webhooks ⇒ Object
292 293 294 |
# File 'lib/shopsavvy_data_api/client.rb', line 292 def list_webhooks make_request(:get, "webhooks") end |
#remove_product_from_schedule(identifier) ⇒ APIResponse<Hash>
Remove product from monitoring schedule
226 227 228 229 230 231 |
# File 'lib/shopsavvy_data_api/client.rb', line 226 def remove_product_from_schedule(identifier) body = { identifier: identifier } response = make_request(:delete, "products/schedule", body: body) APIResponse.new(response) end |
#remove_products_from_schedule(identifiers) ⇒ APIResponse<Array<Hash>>
Remove multiple products from monitoring schedule
237 238 239 240 241 242 |
# File 'lib/shopsavvy_data_api/client.rb', line 237 def remove_products_from_schedule(identifiers) body = { identifiers: identifiers.join(",") } response = make_request(:delete, "products/schedule", body: body) APIResponse.new(response) end |
#schedule_product_monitoring(identifier, frequency, retailer: nil) ⇒ APIResponse<Hash>
Schedule product monitoring
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/shopsavvy_data_api/client.rb', line 178 def schedule_product_monitoring(identifier, frequency, retailer: nil) body = { identifier: identifier, frequency: frequency } body[:retailer] = retailer if retailer response = make_request(:post, "products/schedule", body: body) APIResponse.new(response) end |
#schedule_product_monitoring_batch(identifiers, frequency, retailer: nil) ⇒ APIResponse<Array<Hash>>
Schedule monitoring for multiple products
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/shopsavvy_data_api/client.rb', line 195 def schedule_product_monitoring_batch(identifiers, frequency, retailer: nil) body = { identifiers: identifiers.join(","), frequency: frequency } body[:retailer] = retailer if retailer response = make_request(:post, "products/schedule", body: body) APIResponse.new(response) end |
#search_products(query, limit: nil, offset: nil) ⇒ ProductSearchResult
Search for products by keyword
61 62 63 64 65 66 67 68 |
# File 'lib/shopsavvy_data_api/client.rb', line 61 def search_products(query, limit: nil, offset: nil) params = { q: query } params[:limit] = limit if limit params[:offset] = offset if offset response = make_request(:get, "products/search", params: params) ProductSearchResult.new(response) end |
#test_webhook(webhook_id) ⇒ Object
296 297 298 |
# File 'lib/shopsavvy_data_api/client.rb', line 296 def test_webhook(webhook_id) make_request(:post, "webhooks/#{webhook_id}/test") end |