Module: Yclients::Api::ServiceCategories

Included in:
Client
Defined in:
lib/yclients/api/service_categories.rb

Constant Summary collapse

URL1 =
'https://api.yclients.com/api/v1/service_category'
URL2 =
'https://api.yclients.com/api/v1/service_categories'

Instance Method Summary collapse

Instance Method Details

#service_categories(company_id, args = {}) ⇒ Object

id (Number, 1234) ID категории услуг (для работы с конкретной категорией) staff_id (Number, 1234) ID сотрудника (для получения категорий, привязанных к сотруднику)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yclients/api/service_categories.rb', line 10

def service_categories(company_id, args={})
  id = query_param(:id, args[:id], :numeric)[:id] if args.key?(:id)
  uri = URI(id.nil? ? "#{URL2}/#{company_id}" : "#{URL2}/#{company_id}/#{id}")
  params = {}
  params.merge!(query_param(:staff_id, args[:staff_id], :numeric)) if args.key?(:staff_id)
  uri.query = URI.encode_www_form(params)

  req = Net::HTTP::Get.new(uri, headers({ auth: false }))

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  json = JSON.parse(res.body)
  if json.kind_of?(Array)
    json
  else
    raise ServiceCategoriesAccessError, json.to_s
  end
end

#service_category(company_id, id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yclients/api/service_categories.rb', line 31

def service_category(company_id, id)
  uri = URI("#{URL1}/#{company_id}/#{id}")
  req = Net::HTTP::Get.new(uri, headers({ auth: false }))

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  json = JSON.parse(res.body)
  if json.kind_of?(Hash) && json.key?('id')
    json
  else
    raise ServiceCategoriesAccessError, json.to_s
  end
end