Class: ZammadAPI::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/zammad_api/resources/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, attributes = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zammad_api/resources/base.rb', line 11

def initialize(transport, attributes = {})
  @new_instance = true
  @transport    = transport
  @changes      = {}
  @url          = self.class.get_url

  if attributes.nil?
    attributes = {}
  end
  @attributes = attributes
  symbolize_keys_deep!(@attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/zammad_api/resources/base.rb', line 24

def method_missing(method, *args)
  return @attributes[method] if !method.to_s.end_with?('=')

  method              = method.to_s[0, method.length - 1].to_sym
  @changes[method]    = [@attributes[method], args[0]]
  @attributes[method] = args[0]
  nil
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/zammad_api/resources/base.rb', line 8

def attributes
  @attributes
end

#changesObject (readonly)

Returns the value of attribute changes.



9
10
11
# File 'lib/zammad_api/resources/base.rb', line 9

def changes
  @changes
end

#new_instanceObject

Returns the value of attribute new_instance.



8
9
10
# File 'lib/zammad_api/resources/base.rb', line 8

def new_instance
  @new_instance
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/zammad_api/resources/base.rb', line 8

def url
  @url
end

Class Method Details

.all(transport, _) ⇒ Object



69
70
71
# File 'lib/zammad_api/resources/base.rb', line 69

def self.all(transport, _)
  ZammadAPI::ListAll.new(self, transport, per_page: 100)
end

.create(transport, data) ⇒ Object



89
90
91
92
93
# File 'lib/zammad_api/resources/base.rb', line 89

def self.create(transport, data)
  item = new(transport, data)
  item.save
  item
end

.destroy(transport, id) ⇒ Object



95
96
97
98
99
# File 'lib/zammad_api/resources/base.rb', line 95

def self.destroy(transport, id)
  item = find(transport, id)
  item.destroy
  true
end

.find(transport, id) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zammad_api/resources/base.rb', line 77

def self.find(transport, id)
  response = transport.get(url: "#{@url}/#{id}?expand=true")
  data = JSON.parse(response.body)
  if response.status != 200
    raise "Can't find object (#{self.class.name}): #{data['error']}"
  end

  item = new(transport, data)
  item.new_instance = false
  item
end

.get_urlObject



61
62
63
# File 'lib/zammad_api/resources/base.rb', line 61

def self.get_url
  @url
end

.search(transport, parameter) ⇒ Object



73
74
75
# File 'lib/zammad_api/resources/base.rb', line 73

def self.search(transport, parameter)
  ZammadAPI::ListSearch.new(self, transport, parameter)
end

.url(value) ⇒ Object



65
66
67
# File 'lib/zammad_api/resources/base.rb', line 65

def self.url(value)
  @url = value
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/zammad_api/resources/base.rb', line 37

def changed?
  @changes.present?
end

#destroyObject



41
42
43
44
45
46
47
48
49
# File 'lib/zammad_api/resources/base.rb', line 41

def destroy
  response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
  if response.body.to_s != '' && response.body.to_s != ' '
    data = JSON.parse(response.body)
  end
  return true if response.status == 200

  raise "Can't destroy object (#{self.class.name}): #{data['error']}"
end

#new_record?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zammad_api/resources/base.rb', line 33

def new_record?
  @new_instance
end

#saveObject



51
52
53
54
55
56
57
58
59
# File 'lib/zammad_api/resources/base.rb', line 51

def save
  attributes = saved_attributes
  symbolize_keys_deep!(attributes)
  attributes.delete(:article)
  @attributes   = attributes
  @new_instance = false
  @changes      = {}
  true
end