Class: Phoneburner::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/phoneburner/model.rb

Direct Known Subclasses

Call, Contact, Content, Dialsession, Folder, Member, Settings, Voicemail

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attrs = {}) ⇒ Model

Returns a new instance of Model.



4
5
6
7
# File 'lib/phoneburner/model.rb', line 4

def initialize(client, attrs={})    
  @client = client  
  attributes(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/phoneburner/model.rb', line 44

def method_missing(symbol,*args)
  @attrs ||= {}
  if symbol.to_s.end_with?("=")        
    @attrs[symbol.to_s.gsub!(/=$/,'')] = args.first
  else
    @attrs[symbol.to_s]
  end
end

Class Method Details

.extract_inner_results(results, request) ⇒ Object



32
33
34
# File 'lib/phoneburner/model.rb', line 32

def self.extract_inner_results(results, request)
  results[self.secondary_json_type(request)]
end

.extract_results(results, request) ⇒ Object



28
29
30
# File 'lib/phoneburner/model.rb', line 28

def self.extract_results(results, request)
  results[self.json_type]
end

.secondary_json_type(request) ⇒ Object



24
25
26
# File 'lib/phoneburner/model.rb', line 24

def self.secondary_json_type(request)
  self.json_type
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
# File 'lib/phoneburner/model.rb', line 20

def <=>(other)
  self.id <=> other.id
end

#attribute_namesObject



40
41
42
# File 'lib/phoneburner/model.rb', line 40

def attribute_names
  @attrs.keys
end

#attributes(attrs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/phoneburner/model.rb', line 9

def attributes(attrs)
  @attrs = Hash[attrs.map do |k, v|         
    if v.is_a?(Hash)
      [k.to_s, Phoneburner::Model.new(@client,v)]
    else
      [k.to_s, v]
    end
  end]
  nil
end

#deleteObject



69
70
71
72
73
74
# File 'lib/phoneburner/model.rb', line 69

def delete
  unless is_new?
    Phoneburner::Request.new(@client,self.class).delete(self.id)
    #FIXME What should we do here
  end
end

#is_new?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/phoneburner/model.rb', line 57

def is_new?
  !!self.id
end

#pathObject



36
37
38
# File 'lib/phoneburner/model.rb', line 36

def path
  self.class.path
end

#refreshObject



61
62
63
64
65
66
67
# File 'lib/phoneburner/model.rb', line 61

def refresh
  unless is_new?
    Phoneburner::Request.new(@client,self.class).find(self.id).raw.result do |r|
      attributes(r)
    end
  end
end

#saveObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/phoneburner/model.rb', line 76

def save
  if is_new?
    Phoneburner::Request.new(@client,self.class).create(self).raw.result do |r|
      update_attributes(r)
    end
  else
    Phoneburner::Request.new(@client,self.class).update(self.id,self).raw.result do |r|
      update_attributes(r)
    end
  end
end

#to_json(a = nil) ⇒ Object



53
54
55
# File 'lib/phoneburner/model.rb', line 53

def to_json(a=nil)
  @attrs.to_json(a)
end