class AmoCRM::Resources::Base
Constants
- LIMIT_ROWS
Attributes
client[R]
Public Class Methods
entity_class()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 95 def self.entity_class ActiveSupport::Inflector.constantize "AmoCRM::Entities::#{type.to_s}" end
indexed(*args)
click to toggle source
Возвращает этот-же ресурс только индексированный
@return [AmoCRM::Resources::Indexed]
# File lib/amo_crm/resources/base.rb, line 13 def self.indexed *args # AmoCRM::Resources::Indexed.new new(*args) new(*args) end
inherited(superclass)
click to toggle source
Calls superclass method
# File lib/amo_crm/resources/base.rb, line 5 def self.inherited superclass super AmoCRM::Resources.register_resource superclass end
new(client: nil)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 18 def initialize client: nil raise "Должен быть AmoCRM::Client" unless client.is_a? AmoCRM::Client @client = client end
resource_name()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 91 def self.resource_name ActiveSupport::Inflector.underscore ActiveSupport::Inflector.pluralize type end
type()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 87 def self.type ActiveSupport::Inflector.singularize name.split('::').last.to_sym end
Public Instance Methods
all()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 23 def all buffer = [] offset = 0 list = nil while offset==0 || list.present? list = client.get(list_path, limit_offset: offset, limit_rows: LIMIT_ROWS).contacts offset += LIMIT_ROWS buffer += list end rescue AmoCRM::Client::Error => err raise err unless err.state == 204 buffer end
create(model)
click to toggle source
Создаем запись
@param [AmoCRM::Entities::Base]
Возвращается созданная на сервере сущность
@return [AmoCRM::Entities::Base]
# File lib/amo_crm/resources/base.rb, line 75 def create model raise "Должна быть модель типа AmoCRM::Entities::Base" unless model.is_a? AmoCRM::Entities::Base client.post set_path, model_to_request(model, :add) end
delete(id)
click to toggle source
Удаляем запись по id
@param id
# File lib/amo_crm/resources/base.rb, line 83 def delete id client.delete get_path id end
get(id=nil)
click to toggle source
Забираем элемент по id
@return [AmoCRM::Entities::Base]
# File lib/amo_crm/resources/base.rb, line 56 def get id=nil parse client.get get_path, id: id end
list(params={})
click to toggle source
Возвращает список элементов как есть
@return [Array of AmoCRM::Entities::Base
]
# File lib/amo_crm/resources/base.rb, line 42 def list params={} parse_list client.get list_path, params end
page(params={})
click to toggle source
Возвращает страницу со списком элементов
@return [AmoCRM::Entities::Page]
# File lib/amo_crm/resources/base.rb, line 49 def page params={} parse_page client.get list_path, params end
update(model)
click to toggle source
Модифицируем элемент по id
@return [AmoCRM::Entities::Base]
# File lib/amo_crm/resources/base.rb, line 63 def update model raise "Должна быть модель типа AmoCRM::Entities::Base" unless model.is_a? AmoCRM::Entities::Base client.post set_path, model_to_request(model) end
Private Instance Methods
get_path()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 137 def get_path list_path end
list_path()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 145 def list_path raise 'not implemented' end
model_to_request(model, command=:update)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 103 def model_to_request(model, command=:update) { request: { response_key => { command => [model.attributes_for_update] } } } end
parse(data)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 113 def parse data self.class.entity_class.new data[response_key].first end
parse_content(content)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 123 def parse_content content content end
parse_list(content)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 117 def parse_list content content[response_key].map do |data| self.class.entity_class.new data end end
parse_page(content)
click to toggle source
# File lib/amo_crm/resources/base.rb, line 127 def parse_page content col = AmoCRM::Entities::Collection.parse parse_content content # TODO Парсится два раза. Оптимизировать. Например сделать динамические CollectionFeature # и парсить через них items = parse content AmoCRM::Entities::Page.new items, col.total, col.start, col.count end
set_path()
click to toggle source
# File lib/amo_crm/resources/base.rb, line 141 def set_path raise 'not implemented' end