Class: StreamSend::Resource
- Inherits:
-
Object
- Object
- StreamSend::Resource
show all
- Defined in:
- lib/streamsend.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data, from_api = nil) ⇒ Resource
Returns a new instance of Resource.
15
16
17
18
|
# File 'lib/streamsend.rb', line 15
def initialize(data, from_api=nil)
@data = data
@new_record = !from_api
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/streamsend.rb', line 24
def method_missing(method, *args, &block)
if method.to_s.match(/^(.*)=$/)
@data[$1] = *args
elsif @data.has_key?(method.to_s)
@data[method.to_s]
else
super
end
end
|
Class Method Details
.find(options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/streamsend.rb', line 58
def self.find(options={})
response = StreamSend.get(resource_path(options))
case response.code
when 200
if resource = response[resource_name]
self.new(resource, true)
elsif resources = response['people']
results = []
resources.each { |r| results << self.new(r, true) }
results.count == 1 ? results.first : results
else
nil
end
else
raise "Could not find the #{resource_name}. Make sure the ID is correct. (#{response.code})"
end
end
|
.resource_name ⇒ Object
89
90
91
92
|
# File 'lib/streamsend.rb', line 89
def self.resource_name
self.name.downcase.split('::').last
end
|
.resource_path(options = {}) ⇒ Object
94
95
96
|
# File 'lib/streamsend.rb', line 94
def self.resource_path(options={})
""
end
|
.to_xml(input_hash = self.attributes) ⇒ Object
54
55
56
|
# File 'lib/streamsend.rb', line 54
def self.to_xml(input_hash=self.attributes)
XmlSimple.xml_out({:person => input_hash.reject { |_,v| v.nil? || v.blank? }}, :keep_root => true)
end
|
Instance Method Details
#attributes ⇒ Object
42
43
44
|
# File 'lib/streamsend.rb', line 42
def attributes
@data
end
|
#errors ⇒ Object
38
39
40
|
# File 'lib/streamsend.rb', line 38
def errors
@errors
end
|
#errors=(vals = []) ⇒ Object
34
35
36
|
# File 'lib/streamsend.rb', line 34
def errors=(vals = [])
@errors = vals
end
|
#id ⇒ Object
46
47
48
|
# File 'lib/streamsend.rb', line 46
def id
@data["id"]
end
|
#new_record? ⇒ Boolean
20
21
22
|
# File 'lib/streamsend.rb', line 20
def new_record?
@new_record
end
|
#save ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/streamsend.rb', line 76
def save
if self.new_record?
response = StreamSend.post(self.class.resource_path("audience_id" => self.audience_id),
:body => self.to_xml, :headers => { "Content-Type" => "application/xml" })
self.id = attributes["id"]
else
response = StreamSend.put(self.class.resource_path({ "id" => self.id,
"audience_id" => self.audience_id,
}), :body => self.to_xml, :headers => { "Content-Type" => "application/xml" })
end
handle_response(response)
end
|
#to_xml ⇒ Object
50
51
52
|
# File 'lib/streamsend.rb', line 50
def to_xml
XmlSimple.xml_out({:person => self.attributes.reject { |_,v| v.nil? || v.blank? }}, :keep_root => true)
end
|