Class: Evoke::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/evoke_client/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Callback

Returns a new instance of Callback.



25
26
27
28
# File 'lib/evoke_client/base.rb', line 25

def initialize(data)
  @new_record = determine_if_new_record(data.delete(:new_record))
  @data = Callback.stringify_keys(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



46
47
48
# File 'lib/evoke_client/base.rb', line 46

def method_missing(method, *args, &block)
  @data.include?(method.to_s) ? @data[method.to_s] : super
end

Class Method Details

.create_or_update(data) ⇒ Object



18
19
20
21
22
23
# File 'lib/evoke_client/base.rb', line 18

def self.create_or_update(data)
  data = Callback.stringify_keys(data)
  callback = (find(data["guid"]) || new(data)).update_attributes(data)
  callback.save
  callback
end

.find(guid) ⇒ Object



13
14
15
16
# File 'lib/evoke_client/base.rb', line 13

def self.find(guid)
  callback = get("/callbacks/#{guid}")
  callback.empty? ? nil : new(callback.merge(:new_record => false))
end

Instance Method Details

#destroyObject



42
43
44
# File 'lib/evoke_client/base.rb', line 42

def destroy
  handle_response(self.class.delete("/callbacks/#{guid}")) { |response| nil }
end

#new_record?Boolean

Returns:

  • (Boolean)


30
# File 'lib/evoke_client/base.rb', line 30

def new_record?; @new_record; end

#saveObject



37
38
39
40
# File 'lib/evoke_client/base.rb', line 37

def save
  args = (new_record? ? [:post, "/callbacks"] : [:put, "/callbacks/#{guid}"]) + [{:query => @data}]
  handle_response(self.class.send(*args)) { |response| @data = response }
end

#update_attributes(new_data) ⇒ Object



32
33
34
35
# File 'lib/evoke_client/base.rb', line 32

def update_attributes(new_data)
  @data = @data.merge(Callback.stringify_keys(new_data))
  self
end