Class: Cdek::Resources::Locations

Inherits:
Base
  • Object
show all
Defined in:
lib/cdek/resources/locations.rb

Overview

Справочники локаций CDEK API v2.

Тонкая обёртка над эндпоинтами:

  • GET /location/cities — список городов

  • GET /location/regions — список регионов

  • GET /location/suggest/cities — подсказка городов по подстроке

Параметры передаются как есть в query-string; обязательной нормализации или валидации не делаем — структура и набор параметров полностью соответствуют официальной документации CDEK. Значения nil автоматически отбрасываются, чтобы не попадать в URL пустыми ключами.

Примеры:

Cdek.locations.cities(country_codes: "RU", city: "Москва", size: 5)
Cdek.locations.regions(country_codes: "RU", size: 10)
Cdek.locations.suggest_cities(name: "Моск", country_code: "RU")
Cdek.locations.find_city("Москва")

Constant Summary collapse

CITIES_PATH =
"/location/cities"
REGIONS_PATH =
"/location/regions"
SUGGEST_CITIES_PATH =
"/location/suggest/cities"

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Cdek::Resources::Base

Instance Method Details

#cities(params = {}) ⇒ Object



27
28
29
# File 'lib/cdek/resources/locations.rb', line 27

def cities(params = {})
  client.get(CITIES_PATH, params: params.compact)
end

#find_city(name, country_codes: "RU", **extra) ⇒ Object

Удобный шорткат: найти первый город по точному названию (и стране). Возвращает Hash города или nil, если ничего не нашлось.



41
42
43
44
# File 'lib/cdek/resources/locations.rb', line 41

def find_city(name, country_codes: "RU", **extra)
  list = cities({ city: name, country_codes: country_codes, size: 1 }.merge(extra))
  list.is_a?(Array) ? list.first : nil
end

#regions(params = {}) ⇒ Object



31
32
33
# File 'lib/cdek/resources/locations.rb', line 31

def regions(params = {})
  client.get(REGIONS_PATH, params: params.compact)
end

#suggest_cities(params = {}) ⇒ Object



35
36
37
# File 'lib/cdek/resources/locations.rb', line 35

def suggest_cities(params = {})
  client.get(SUGGEST_CITIES_PATH, params: params.compact)
end