Class: TinyErpApi::Client

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

Constant Summary collapse

BASE_URL =
"https://api.tiny.com.br/api2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(access_token = nil)
  @access_token = access_token || TinyErpApi.configuration.access_token
  @query_params = "token=#{@access_token}&formato=JSON"
  super()
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



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

def query_params
  @query_params
end

Instance Method Details

#create_contact(contact) ⇒ Object



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

def create_contact(contact)
  params = "#{@query_params}&contato=#{URI.encode_www_form_component(contact.to_json)}"
  response = post("/contato.incluir.php?#{params}")
  response.json.dig("retorno","registros",0,"registro")
end

#create_order(order) ⇒ Object



70
71
72
73
74
# File 'lib/tiny_erp_api/client.rb', line 70

def create_order(order)
  params = "#{@query_params}&pedido=#{URI.encode_www_form_component(JSON.dump(order))}"
  response = post("/pedido.incluir.php?#{params}")
  response.json.dig("retorno", "registros", "registro")
end

#get_contact_by_document(document_number) ⇒ Object

Contacts



13
14
15
16
17
# File 'lib/tiny_erp_api/client.rb', line 13

def get_contact_by_document(document_number)
  params = "#{@query_params}&cnpj_cpf=#{document_number}"
  response = get("/contatos.pesquisa.php?#{params}")
  response.json.dig("retorno","contatos",0,"contato")
end

#get_contact_by_id(contact_id) ⇒ Object



19
20
21
22
23
# File 'lib/tiny_erp_api/client.rb', line 19

def get_contact_by_id(contact_id)
  params = "#{@query_params}&id=#{contact_id}"
  response = get("/contato.obter.php?#{params}")
  response.json.dig("retorno","contato")
end

#get_order_by_id(order_id) ⇒ Object

Orders



64
65
66
67
68
# File 'lib/tiny_erp_api/client.rb', line 64

def get_order_by_id(order_id)
  params = "#{@query_params}&id=#{order_id}"
  response = post("/pedido.obter.php?#{params}")
  response.json.dig("retorno", "pedido")
end

#get_product_by_id(product_id) ⇒ Object



57
58
59
60
61
# File 'lib/tiny_erp_api/client.rb', line 57

def get_product_by_id(product_id)
  params = "#{@query_params}&id=#{product_id}"
  response = post("/produto.obter.php?#{params}")
  response.json.dig("retorno", "produto")
end

#get_product_by_sku(sku) ⇒ Object

Products



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

def get_product_by_sku(sku)
  params = "#{@query_params}&pesquisa=#{URI.encode_www_form_component(sku)}"
  response = post("/produtos.pesquisa.php?#{params}")
  response.json.dig("retorno","produtos",0, "produto")
end

#get_seller_by_name(seller_name) ⇒ Object



44
45
46
47
48
# File 'lib/tiny_erp_api/client.rb', line 44

def get_seller_by_name(seller_name)
  params = "#{@query_params}&pesquisa=#{URI.encode_www_form_component(seller_name)}"
  response = post("/vendedores.pesquisa.php?#{params}")
  response.json.dig("retorno","vendedores",0,"vendedor")
end

#get_sellers(page = 1) ⇒ Object

Sellers



38
39
40
41
42
# File 'lib/tiny_erp_api/client.rb', line 38

def get_sellers(page = 1)
  params = "#{@query_params}&pagina=#{page}pesquisa="
  response = post("/vendedores.pesquisa.php?#{params}")
  response.json.dig("retorno")
end

#update_contact(contact_id) ⇒ Object



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

def update_contact(contact_id)
  params = "#{@query_params}&contato=#{contact_id}"
  response = post("/contato.alterar.php?#{params}")
  response.json.dig("retorno","registros",0,"registro")
end