Class: BlingApi::Client
- Inherits:
-
Ac::Base
- Object
- Ac::Base
- BlingApi::Client
- Defined in:
- lib/bling_api/client.rb
Constant Summary collapse
- BASE_URL =
"https://www.bling.com.br/Api/v3"- SAVE_RESPONSES =
true
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
- #create_contact(body) ⇒ Object
- #create_order(body) ⇒ Object
- #find_contact_by_document_number(document_number) ⇒ Object
- #find_product_by_sku(sku) ⇒ Object
- #get_contact(contact_id) ⇒ Object
- #get_order(order_id) ⇒ Object
- #get_seller(seller_id) ⇒ Object
- #get_sellers(page, limit, situation) ⇒ Object
-
#initialize(access_token = nil) ⇒ Client
constructor
A new instance of Client.
- #refresh_token(client_id:, client_secret:, refresh_token:) ⇒ Object
- #update_contact(contact_id, body) ⇒ Object
- #validate_response(response, required_response_key) ⇒ Object
Constructor Details
#initialize(access_token = nil) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 |
# File 'lib/bling_api/client.rb', line 7 def initialize(access_token = nil) @headers = { "Content-Type": "application/json" } super(access_token) end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/bling_api/client.rb', line 6 def access_token @access_token end |
Instance Method Details
#create_contact(body) ⇒ Object
48 49 50 51 |
# File 'lib/bling_api/client.rb', line 48 def create_contact(body) response = post("/contatos", body: body, headers: @headers) { |response| validate_response(response, "data") } response.json["data"] end |
#create_order(body) ⇒ Object
53 54 55 56 |
# File 'lib/bling_api/client.rb', line 53 def create_order(body) response = post("/pedidos/vendas", body: body, headers: @headers) { |response| validate_response(response, "data") } response.json["data"] end |
#find_contact_by_document_number(document_number) ⇒ Object
38 39 40 41 |
# File 'lib/bling_api/client.rb', line 38 def find_contact_by_document_number(document_number) response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") { |response| validate_response(response, "data") } response.json["data"].first end |
#find_product_by_sku(sku) ⇒ Object
43 44 45 46 |
# File 'lib/bling_api/client.rb', line 43 def find_product_by_sku(sku) response = get("/produtos?codigo=#{sku}") { |response| validate_response(response, "data") } response.json["data"].first end |
#get_contact(contact_id) ⇒ Object
23 24 25 26 |
# File 'lib/bling_api/client.rb', line 23 def get_contact(contact_id) response = get("/contatos/#{contact_id}") { |response| validate_response(response, "data") } response.json["data"] end |
#get_order(order_id) ⇒ Object
18 19 20 21 |
# File 'lib/bling_api/client.rb', line 18 def get_order(order_id) response = get("/pedidos/vendas/#{order_id}") { |response| validate_response(response, "data") } response.json["data"] end |
#get_seller(seller_id) ⇒ Object
28 29 30 31 |
# File 'lib/bling_api/client.rb', line 28 def get_seller(seller_id) response = get("/vendedores/#{seller_id}") { |response| validate_response(response, "data") } response.json["data"] end |
#get_sellers(page, limit, situation) ⇒ Object
33 34 35 36 |
# File 'lib/bling_api/client.rb', line 33 def get_sellers(page, limit, situation) response = get("/vendedores?pagina=#{page}&limite=#{limit}&situacaoContato=#{situation}") { |response| validate_response(response, "data") } response.json["data"] end |
#refresh_token(client_id:, client_secret:, refresh_token:) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/bling_api/client.rb', line 58 def refresh_token(client_id:, client_secret:, refresh_token:) headers = { "Content-Type": "application/x-www-form-urlencoded", Authorization: "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}" } body = "grant_type=refresh_token&refresh_token=#{refresh_token}" response = post("/oauth/token", body: body, headers: headers) { |response| validate_response(response, "refresh_token") } response.json end |
#update_contact(contact_id, body) ⇒ Object
14 15 16 |
# File 'lib/bling_api/client.rb', line 14 def update_contact(contact_id, body) put("/contatos/#{contact_id}", body: body, headers: @headers) { |response| validate_response(response, "") } end |
#validate_response(response, required_response_key) ⇒ Object
68 69 70 71 |
# File 'lib/bling_api/client.rb', line 68 def validate_response(response, required_response_key) puts response.json unless response.success? ![500, 429].include?(response.code) || response.json.dig(*required_response_key) end |