-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat models/services/bling/product.rb: #7
get products from bling api.
- Loading branch information
1 parent
5b2df76
commit 76626c6
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
module Services | ||
module Bling | ||
# Service::Bling::Product is used in the GoodJob service to get products from Bling API | ||
# Model Product.synchronize_bling uses it to create/update itself according to bling products. | ||
class Product < ApplicationService | ||
attr_accessor :product_command, :tenant, :options | ||
|
||
def initialize(product_command:, tenant:, options: {}) | ||
@product_command = product_command | ||
@tenant = tenant | ||
@options = options | ||
@max_pages = options[:max_pages] | ||
options.delete(:max_pages) | ||
end | ||
|
||
def call | ||
case product_command | ||
when 'find_products' | ||
find_products | ||
else | ||
raise 'Not a product command' | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_products | ||
token = bling_token | ||
base_url = 'https://www.bling.com.br/Api/v3/produtos' | ||
params = { | ||
limite: 100 | ||
} | ||
|
||
params.merge!(options) | ||
|
||
headers = { | ||
'Accept' => 'application/json', | ||
'Authorization' => "Bearer #{token}" | ||
} | ||
|
||
all_products = [] | ||
|
||
(1..@max_pages).each do |page| | ||
# we do not need to request all massive data. If the first page works, so does the remaining pages. | ||
break if (page.eql?(2) && Rails.env.eql?('test')) | ||
|
||
response = HTTParty.get(base_url, query: params.merge(pagina: page), headers:) | ||
if response['error'].present? | ||
sleep 10 | ||
response = HTTParty.get(base_url, query: params.merge(pagina: page), headers:) | ||
end | ||
|
||
raise(StandardError, response['error']['type']) if response['error'].present? | ||
|
||
data = JSON.parse(response.body) | ||
break if data['data'].blank? | ||
|
||
all_products.concat(data['data']) | ||
end | ||
|
||
{ 'data' => all_products } | ||
end | ||
|
||
def bling_token | ||
@bling_token ||= BlingDatum.find_by(account_id: @tenant).access_token | ||
end | ||
end | ||
end | ||
end |