Class: ZammadAPI::Resources::Base

Inherits:
Object
  • Object
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

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/zammad_api/resources/base.rb', line 11

def attributes
  @attributes
end

#changesObject (readonly)

Returns the value of attribute changes.



12
13
14
# File 'lib/zammad_api/resources/base.rb', line 12

def changes
  @changes
end

#new_instanceObject

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

#urlObject

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



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")
  if response.status != 200
    raise ResponseError.from(response, operation: 'find object', resource_class: self)
  end

  data = safe_json_parse(response.body)
  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)


40
41
42
# File 'lib/zammad_api/resources/base.rb', line 40

def changed?
  !@changes.to_h.empty?
end

#destroyObject



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

def destroy
  response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
  return true if response.status == 200

  raise ResponseError.from(response, operation: 'destroy object', resource_class: self.class)
end

#new_record?Boolean

Returns:

  • (Boolean)


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

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