Class: Mosaic::Lyris::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/mosaic/lyris/object.rb

Direct Known Subclasses

Demographic, Filter, List, Message, Partner, Record, Trigger, Upload

Constant Summary collapse

@@logger =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



19
20
21
22
23
# File 'lib/mosaic/lyris/object.rb', line 19

def initialize(attributes)
  attributes.each do |attribute,value|
    instance_variable_set "@#{attribute}", value unless value.nil?
  end
end

Class Method Details

.callback_urlObject



47
48
49
# File 'lib/mosaic/lyris/object.rb', line 47

def callback_url
  configuration['callback_url']
end

.configurationObject



30
31
32
# File 'lib/mosaic/lyris/object.rb', line 30

def configuration
  @@configuration ||= load_configuration
end

.configuration=(value) ⇒ Object



34
35
36
# File 'lib/mosaic/lyris/object.rb', line 34

def configuration=(value)
  @@configuration = value
end

.default_list_idObject



51
52
53
# File 'lib/mosaic/lyris/object.rb', line 51

def default_list_id
  configuration['list_id']
end

.default_trigger_idObject



55
56
57
# File 'lib/mosaic/lyris/object.rb', line 55

def default_trigger_id
  configuration['trigger_id']
end

.get_array_data(record, type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mosaic/lyris/object.rb', line 59

def get_array_data(record, type)
  if record.at("DATA[@type='#{type}']")
    record.search("DATA[@type='#{type}']").collect do |data|
      if block_given?
        yield data
      else
        data.inner_html
      end
    end
  end
end

.get_boolean_data(record, type, value, attribute = nil, conditions = {}) ⇒ Object



71
72
73
74
75
# File 'lib/mosaic/lyris/object.rb', line 71

def get_boolean_data(record, type, value, attribute = nil, conditions = {})
  if data = get_data(record, type, attribute, conditions)
    data == value
  end
end

.get_data(record, type, attribute = nil, conditions = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/mosaic/lyris/object.rb', line 77

def get_data(record, type, attribute = nil, conditions = {})
  xpath = "DATA[@type='#{type}']"
  xpath << conditions.collect { |a,v| "[@#{a}='#{v}']" }.join
  if element = record.at(xpath)
    data = attribute ? element[attribute] : element.inner_html
    # TODO: fix encoding if necessary... seems like we should need to convert to UTF-8 here, but this works for now
    HTMLEntities.new.decode(data.gsub(/&#(\d+);/) { Integer($1).chr })
  end
end

.get_date_data(record, type, attribute = nil, conditions = {}) ⇒ Object



87
88
89
90
# File 'lib/mosaic/lyris/object.rb', line 87

def get_date_data(record, type, attribute = nil, conditions = {})
  data = get_data(record, type, attribute, conditions)
  Date.parse(data) unless data.blank?
end

.get_demographic_data(record) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mosaic/lyris/object.rb', line 92

def get_demographic_data(record)
  if data = get_array_data(record, 'demographic') { |d| [ d[:id].to_i, d.inner_html ] }
    data.inject({}) do |h,(k,v)|
      case h[k]
      when NilClass
        h[k] = v
      when Array
        h[k] << v
      else
        h[k] = Array(h[k])
        h[k] << v
      end
      h
    end
  end
end

.get_element(record, element) ⇒ Object



109
110
111
112
113
# File 'lib/mosaic/lyris/object.rb', line 109

def get_element(record, element)
  if data = record.at("/#{element}")
    data.inner_html
  end
end

.get_integer_data(record, type, attribute = nil, conditions = {}) ⇒ Object



115
116
117
118
119
# File 'lib/mosaic/lyris/object.rb', line 115

def get_integer_data(record, type, attribute = nil, conditions = {})
  if data = get_data(record, type, attribute, conditions)
    data.gsub(/,/,'').to_i
  end
end

.get_integer_element(record, element) ⇒ Object



121
122
123
# File 'lib/mosaic/lyris/object.rb', line 121

def get_integer_element(record, element)
  get_element(record, element).to_i
end

.get_time_data(record, type, attribute = nil, conditions = {}) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/mosaic/lyris/object.rb', line 125

def get_time_data(record, type, attribute = nil, conditions = {})
  if data = get_data(record, type, attribute, conditions)
    Time.use_zone('Pacific Time (US & Canada)') do
      Time.zone.parse(data)
    end
  end
end

.get_time_element(record, element) ⇒ Object



133
134
135
136
137
# File 'lib/mosaic/lyris/object.rb', line 133

def get_time_element(record, element)
  if data = get_element(record, element)
    Time.parse(data) + (Time.zone.utc_offset - Time.zone_offset('PST'))
  end
end

.get_time_offset_data(record, type, attribute = nil, conditions = {}) ⇒ Object



139
140
141
142
143
# File 'lib/mosaic/lyris/object.rb', line 139

def get_time_offset_data(record, type, attribute = nil, conditions = {})
  if offset = get_integer_data(record, type, attribute, conditions)
    offset + (Time.zone.utc_offset - Time.zone_offset('PST'))
  end
end

.get_xml_time_data(record, type, attribute = nil, conditions = {}) ⇒ Object



145
146
147
148
149
# File 'lib/mosaic/lyris/object.rb', line 145

def get_xml_time_data(record, type, attribute = nil, conditions = {})
  if data = get_data(record, type, attribute, conditions)
    Time.xmlschema(data)
  end
end

.load_configurationObject



38
39
40
41
42
# File 'lib/mosaic/lyris/object.rb', line 38

def load_configuration
  configuration = YAML.load_file(File.join(::Rails.root.to_s,'config','lyris.yml')) rescue {}
  configuration = configuration[::Rails.env] if configuration.include?(::Rails.env)
  configuration
end

.loggerObject



151
152
153
# File 'lib/mosaic/lyris/object.rb', line 151

def logger
  @@logger ||= nil
end

.logger=(logger) ⇒ Object



155
156
157
# File 'lib/mosaic/lyris/object.rb', line 155

def logger=(logger)
  @@logger = logger
end

.passwordObject



159
160
161
# File 'lib/mosaic/lyris/object.rb', line 159

def password
  configuration['password']
end

.post(type, activity, &block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mosaic/lyris/object.rb', line 163

def post(type, activity, &block)
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct!
  xml.DATASET do
    xml.SITE_ID site_id
    put_extra_data(xml, 'password', password)
    block.call(xml) if block
  end
  input = xml.target!

  request = Net::HTTP::Post.new("/API/mailing_list.html")
  logger.debug ">>>>> REQUEST:\ntype=#{type}\nactivity=#{activity}\ninput=#{input}\n>>>>>" if logger
  request.set_form_data('type' => type, 'activity' => activity, 'input' => input)

  conn = Net::HTTP.new(server, 443)
  conn.use_ssl = true
  conn.verify_mode = OpenSSL::SSL::VERIFY_NONE

  conn.start do |http|
    # TODO: parse encoding from declaration? update declaration after conversion?
    reply = http.request(request).body
    logger.debug ">>>>> REPLY:\n#{reply}\n>>>>>" if logger
    document = Nokogiri.XML(reply)
    raise Error, (document % '/DATASET/DATA').inner_html unless document % "/DATASET/TYPE[.='success']"
    document
  end
end

.put_array_data(request, type, values) ⇒ Object



191
192
193
194
195
# File 'lib/mosaic/lyris/object.rb', line 191

def put_array_data(request, type, values)
  Array(values).each do |value|
    put_data(request, type, value)
  end
end

.put_data(request, type, value, attributes = {}) ⇒ Object



197
198
199
# File 'lib/mosaic/lyris/object.rb', line 197

def put_data(request, type, value, attributes = {})
  request.DATA value, {:type => type}.merge(attributes) unless value.nil?
end

.put_demographic_data(request, demographics) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/mosaic/lyris/object.rb', line 201

def put_demographic_data(request, demographics)
  Array(demographics).each do |id, value|
    Array(value).each do |v|
      put_data(request, 'demographic', v, :id => id)
    end
  end
end

.put_extra_data(request, id, value) ⇒ Object



209
210
211
# File 'lib/mosaic/lyris/object.rb', line 209

def put_extra_data(request, id, value)
  put_data(request, 'extra', value, :id => id)
end

.serverObject



213
214
215
# File 'lib/mosaic/lyris/object.rb', line 213

def server
  configuration['server']
end

.site_idObject



217
218
219
# File 'lib/mosaic/lyris/object.rb', line 217

def site_id
  configuration['site_id']
end

.triggersObject



221
222
223
# File 'lib/mosaic/lyris/object.rb', line 221

def triggers
  configuration['triggers']
end

Instance Method Details

#to_paramObject



25
26
27
# File 'lib/mosaic/lyris/object.rb', line 25

def to_param
  id && id.to_s
end