Class: WitBot::WitModel::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wit_bot/models/wit/base.rb

Direct Known Subclasses

Entity, Intent

Constant Summary collapse

@@changeable_attrs =
[]
@@changeable_attrs_only =
{}
@@server_attrs =
[]
@@server_attrs_only =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, request = false, *args) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/wit_bot/models/wit/base.rb', line 11

def initialize(id, request=false, *args)
  @id = id

  @delete_listeners = []

  get if request
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/wit_bot/models/wit/base.rb', line 4

def id
  @id
end

Class Method Details

.allObject



20
21
22
23
24
25
26
# File 'lib/wit_bot/models/wit/base.rb', line 20

def all
  @all ||= request.get(all: true).map do |data|
    model = self.new data[:id]
    model.from_response_data data
    model
  end
end

.get(model: nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/wit_bot/models/wit/base.rb', line 28

def get(model: nil)
  if model.respond_to? :get
    model.get
  else
    self.class.new id, request: true
  end
end

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wit_bot/models/wit/base.rb', line 49

def create
  _response = request.create

  if self.respond_to? :process_create_response, true
    self.process_create_response(_response) do |response|
      from_response_data response
    end
  else
    from_response_data _response
  end
end

#deleteObject



61
62
63
64
65
# File 'lib/wit_bot/models/wit/base.rb', line 61

def delete
  request.delete

  @delete_listeners.all?{|listener| listener.call self}
end

#from_response_data(response, method = ) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/wit_bot/models/wit/base.rb', line 88

def from_response_data(response, method=caller[0])
  response = response.with_indifferent_access

  variables = (@@server_attrs_only[method] || []) + @@server_attrs + @@changeable_attrs

  variables.each do |attr|
    if (value = response[attr])
      processor = "process_#{attr}".to_sym

      value = self.send processor, value if self.respond_to? processor, true

      instance_variable_set "@#{attr}", value
    end
  end
end

#get(data: {}) ⇒ Object



67
68
69
# File 'lib/wit_bot/models/wit/base.rb', line 67

def get(data: {})
  from_response_data data
end

#on_delete(&listener) ⇒ Object



71
72
73
# File 'lib/wit_bot/models/wit/base.rb', line 71

def on_delete(&listener)
  @delete_listeners << listener
end

#to_h(include_id = true, method = ) ⇒ Object Also known as: to_hash, as_json



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

def to_h(include_id=true, method=caller[0])
  attrs = @@changeable_attrs + (@@changeable_attrs_only[method] || [])

  hash = {}
  hash[:id] = id if include_id

  attrs.inject(hash) do |h, v|
    h[v] = self.send v if self.send v
  end
end