Class: ZammadAPI::Resources::Base
- Inherits:
-
Object
- Object
- ZammadAPI::Resources::Base
show all
- Extended by:
- JsonHelper
- Includes:
- JsonHelper
- Defined in:
- lib/zammad_api/resources/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from JsonHelper
safe_json_parse
Constructor Details
#initialize(transport, attributes = {}) ⇒ Base
Returns a new instance of Base.
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/zammad_api/resources/base.rb', line 14
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
27
28
29
30
31
32
33
34
|
# File 'lib/zammad_api/resources/base.rb', line 27
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
#attributes ⇒ Object
Returns the value of attribute attributes.
11
12
13
|
# File 'lib/zammad_api/resources/base.rb', line 11
def attributes
@attributes
end
|
#changes ⇒ Object
Returns the value of attribute changes.
12
13
14
|
# File 'lib/zammad_api/resources/base.rb', line 12
def changes
@changes
end
|
#new_instance ⇒ Object
Returns the value of attribute new_instance.
11
12
13
|
# File 'lib/zammad_api/resources/base.rb', line 11
def new_instance
@new_instance
end
|
#url ⇒ Object
Returns the value of attribute url.
11
12
13
|
# File 'lib/zammad_api/resources/base.rb', line 11
def url
@url
end
|
Class Method Details
.all(transport, _) ⇒ Object
72
73
74
|
# File 'lib/zammad_api/resources/base.rb', line 72
def self.all(transport, _)
ZammadAPI::ListAll.new(self, transport, per_page: 100)
end
|
.create(transport, data) ⇒ Object
92
93
94
95
96
|
# File 'lib/zammad_api/resources/base.rb', line 92
def self.create(transport, data)
item = new(transport, data)
item.save
item
end
|
.destroy(transport, id) ⇒ Object
98
99
100
101
102
|
# File 'lib/zammad_api/resources/base.rb', line 98
def self.destroy(transport, id)
item = find(transport, id)
item.destroy
true
end
|
.find(transport, id) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/zammad_api/resources/base.rb', line 80
def self.find(transport, id)
response = transport.get(url: "#{@url}/#{id}?expand=true")
data = safe_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_url ⇒ Object
64
65
66
|
# File 'lib/zammad_api/resources/base.rb', line 64
def self.get_url
@url
end
|
.search(transport, parameter) ⇒ Object
76
77
78
|
# File 'lib/zammad_api/resources/base.rb', line 76
def self.search(transport, parameter)
ZammadAPI::ListSearch.new(self, transport, parameter)
end
|
.url(value) ⇒ Object
68
69
70
|
# File 'lib/zammad_api/resources/base.rb', line 68
def self.url(value)
@url = value
end
|
Instance Method Details
#changed? ⇒ Boolean
40
41
42
|
# File 'lib/zammad_api/resources/base.rb', line 40
def changed?
@changes.present?
end
|
#destroy ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/zammad_api/resources/base.rb', line 44
def destroy
response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
if response.body.to_s != '' && response.body.to_s != ' '
data = safe_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
36
37
38
|
# File 'lib/zammad_api/resources/base.rb', line 36
def new_record?
@new_instance
end
|
#save ⇒ Object
54
55
56
57
58
59
60
61
62
|
# File 'lib/zammad_api/resources/base.rb', line 54
def save
attributes = saved_attributes
symbolize_keys_deep!(attributes)
attributes.delete(:article)
@attributes = attributes
@new_instance = false
@changes = {}
true
end
|