Class: ZammadAPI::Resources::Base
- Inherits:
-
Object
- Object
- ZammadAPI::Resources::Base
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
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)
if method.to_s[-1, 1] == '='
method = method.to_s[0, method.length - 1].to_sym
@changes[method] = [@attributes[method], args[0]]
@attributes[method] = args[0]
end
@attributes[method]
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8
9
10
|
# File 'lib/zammad_api/resources/base.rb', line 8
def attributes
@attributes
end
|
#changes ⇒ Object
Returns the value of attribute changes.
9
10
11
|
# File 'lib/zammad_api/resources/base.rb', line 9
def changes
@changes
end
|
#new_instance ⇒ Object
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
|
#url ⇒ Object
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
87
88
89
|
# File 'lib/zammad_api/resources/base.rb', line 87
def self.all(transport, _)
ZammadAPI::ListAll.new(self, transport, per_page: 100)
end
|
.create(transport, data) ⇒ Object
106
107
108
109
110
|
# File 'lib/zammad_api/resources/base.rb', line 106
def self.create(transport, data)
item = new(transport, data)
item.save
item
end
|
.destroy(transport, id) ⇒ Object
112
113
114
115
116
|
# File 'lib/zammad_api/resources/base.rb', line 112
def self.destroy(transport, id)
item = find(transport, id)
item.destroy
true
end
|
.find(transport, id) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/zammad_api/resources/base.rb', line 95
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_url ⇒ Object
79
80
81
|
# File 'lib/zammad_api/resources/base.rb', line 79
def self.get_url
@url
end
|
.search(transport, parameter) ⇒ Object
91
92
93
|
# File 'lib/zammad_api/resources/base.rb', line 91
def self.search(transport, parameter)
ZammadAPI::ListSearch.new(self, transport, parameter)
end
|
.url(value) ⇒ Object
83
84
85
|
# File 'lib/zammad_api/resources/base.rb', line 83
def self.url(value)
@url = value
end
|
Instance Method Details
#changed? ⇒ Boolean
37
38
39
40
|
# File 'lib/zammad_api/resources/base.rb', line 37
def changed?
return false if @changes.empty?
true
end
|
#destroy ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/zammad_api/resources/base.rb', line 42
def destroy
response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
if response.body.to_s != '' && response.body.to_s != ' '
data = JSON.parse(response.body)
end
if response.status != 200
raise "Can't destroy object (#{self.class.name}): #{data['error']}"
end
true
end
|
#new_record? ⇒ Boolean
33
34
35
|
# File 'lib/zammad_api/resources/base.rb', line 33
def new_record?
@new_instance
end
|
#save ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/zammad_api/resources/base.rb', line 53
def save
if @new_instance
response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
attributes = JSON.parse(response.body)
if response.status != 201
raise "Can't create new object (#{self.class.name}): #{attributes['error']}"
end
else
attributes_to_post = {}
@changes.each { |name, values|
attributes_to_post[name] = values[1]
}
response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
attributes = JSON.parse(response.body)
if response.status != 200
raise "Can't update new object (#{self.class.name}): #{attributes['error']}"
end
end
symbolize_keys_deep!(attributes)
attributes.delete(:article)
@attributes = attributes
@new_instance = false
@changes = {}
true
end
|