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



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

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

#create_order(body) ⇒ Object



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

def create_order(body)
  response = post("/pedidos/vendas", body: body, headers: @headers) {_1.json["data"]}
  response.json["data"]
end

#find_contact_by_document_number(document_number) ⇒ Object



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

def find_contact_by_document_number(document_number)
  response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") {_1.json["data"]}
  response.json["data"].first
end

#find_product_by_sku(sku) ⇒ Object



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

def find_product_by_sku(sku)
  response = get("/produtos?codigo=#{sku}") {_1.json["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}") {_1.json["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}") {_1.json["data"]}
  response.json["data"]
end

#refresh_token(client_id:, client_secret:, refresh_token:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/bling_api/client.rb', line 46

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) {_1.json["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)
  response = put("/contatos/#{contact_id}", body: body, headers: @headers){_1.code==204}
end