Class: MoyskladIntegration::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/moysklad_integration/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



7
8
9
# File 'lib/moysklad_integration/service.rb', line 7

def initialize
  @connection = MoyskladIntegration::Connector.new.connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/moysklad_integration/service.rb', line 5

def connection
  @connection
end

Instance Method Details

#create(object_name, params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/moysklad_integration/service.rb', line 11

def create(object_name, params)
  initialize_entity(object_name, params)
  response = connection.post(entity_path, entity_instance.to_json)
  if response.status == 200
    Entity.new(JSON.parse(response.body))
  else
    JSON.parse(response.body)['errors'].each do |error|
      MoyskladIntegration.logger.warn(error['error'])
    end
  end
end

#create_positions(object, positions_params) ⇒ Object



56
57
58
59
# File 'lib/moysklad_integration/service.rb', line 56

def create_positions(object, positions_params)
  @entity_path = "entity/#{object.meta['type']}/#{object.id}/positions"
  response = connection.post(entity_path, positions_params.to_json)
end

#delete_positions(object, positions) ⇒ Object



71
72
73
74
75
76
# File 'lib/moysklad_integration/service.rb', line 71

def delete_positions(object, positions)
  positions.each do |position|
    @entity_path = "entity/#{object.meta['type']}/#{object.id}/positions/#{position[:id]}"
    response = connection.delete(entity_path)
  end
end

#filter(object_name, params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/moysklad_integration/service.rb', line 23

def filter(object_name, params)
  initialize_entity(object_name, params)
  params_for_filter = params.each_with_object('') { |(k, v), str| str << "#{k}=#{v};" }
  response = connection.get("#{entity_path}?filter=#{params_for_filter}")
  if response.status == 200
    JSON.parse(response.body)['rows'].each_with_object([]) do |entity_params, arr|
      arr << Entity.new(entity_params)
    end
  end
end

#get(object_name, id = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/moysklad_integration/service.rb', line 34

def get(object_name, id = nil)
  @entity_path = id ? "entity/#{object_name.downcase}/#{id}" : "entity/#{object_name.downcase}"
  response = connection.get(entity_path)
  if response.status == 200
    if id
      Entity.new(JSON.parse(response.body))
    else
      JSON.parse(response.body)['rows'].each_with_object([]) do |entity_params, arr|
        arr << Entity.new(entity_params)
      end
    end
  end
end

#get_positions(object) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/moysklad_integration/service.rb', line 61

def get_positions(object)
  @entity_path = "entity/#{object.meta['type']}/#{object.id}/positions"
  response = connection.get(entity_path)
  if response.status == 200
    JSON.parse(response.body)['rows'].each_with_object([]) do |entity_params, arr|
      arr << Entity.new(entity_params)
    end
  end
end

#update(object, params) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/moysklad_integration/service.rb', line 48

def update(object, params)
  params.delete_if do |k, v|
    object.__send__(k) == (v.is_a?(String) ? v : v.collect { |key, val| [key.to_s, val] }.to_h)
  end
  @entity_path = "entity/#{object.meta['type']}/#{object.id}"
  response = connection.put(entity_path.to_s, params.to_json)
end