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
# 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



67
68
69
# File 'lib/zammad_api/resources/base.rb', line 67

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

.create(transport, data) ⇒ Object



86
87
88
89
90
# File 'lib/zammad_api/resources/base.rb', line 86

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

.destroy(transport, id) ⇒ Object



92
93
94
95
96
# File 'lib/zammad_api/resources/base.rb', line 92

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

.find(transport, id) ⇒ Object



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

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



59
60
61
# File 'lib/zammad_api/resources/base.rb', line 59

def self.get_url
  @url
end

.search(transport, parameter) ⇒ Object



71
72
73
# File 'lib/zammad_api/resources/base.rb', line 71

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

.url(value) ⇒ Object



63
64
65
# File 'lib/zammad_api/resources/base.rb', line 63

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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  @changes.present?
end

#destroyObject



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

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)


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

def new_record?
  @new_instance
end

#saveObject



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

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