Class: BlingApi::Client

Inherits:
Ac::Base
  • Object
show all
Defined in:
lib/bling_api/client.rb

Constant Summary collapse

BASE_URL =
"https://www.bling.com.br/Api/v3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/bling_api/client.rb', line 5

def initialize(access_token = nil)
  @headers = {
    "Content-Type": "application/json"
  }
  super(access_token)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/bling_api/client.rb', line 4

def access_token
  @access_token
end

Instance Method Details

#create_contact(body) ⇒ Object



46
47
48
49
# File 'lib/bling_api/client.rb', line 46

def create_contact(body)
  response = post("/contatos", body: body, headers: @headers) { |response| validate_response(response, "data") }
  response.json["data"]
end

#create_order(body) ⇒ Object



51
52
53
54
# File 'lib/bling_api/client.rb', line 51

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



36
37
38
39
# File 'lib/bling_api/client.rb', line 36

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



41
42
43
44
# File 'lib/bling_api/client.rb', line 41

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



21
22
23
24
# File 'lib/bling_api/client.rb', line 21

def get_contact(contact_id)
  response = get("/contatos/#{contact_id}") { |response| validate_response(response, "data") }
  response.json["data"]
end

#get_order(order_id) ⇒ Object



16
17
18
19
# File 'lib/bling_api/client.rb', line 16

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



26
27
28
29
# File 'lib/bling_api/client.rb', line 26

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



31
32
33
34
# File 'lib/bling_api/client.rb', line 31

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



56
57
58
59
60
61
62
63
64
# File 'lib/bling_api/client.rb', line 56

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



12
13
14
# File 'lib/bling_api/client.rb', line 12

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



66
67
68
69
# File 'lib/bling_api/client.rb', line 66

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