Class: SadsXml::Sads

Inherits:
Object
  • Object
show all
Defined in:
lib/sads_xml/sads.rb

Constant Summary collapse

DEFAULTS =
{
  :navigation => {
    :type => 'nav'
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSads

Returns a new instance of Sads.



35
36
37
38
39
40
41
42
43
44
# File 'lib/sads_xml/sads.rb', line 35

def initialize
  @sanitize = false
  @title = ''
  @inputs = []
  @navigations = { :default => [] }
  @messages = { :top => '', :bottom => '' }
  @banner_targets = []
  @banner_position = :bottom
  @request_attributes = []
end

Instance Attribute Details

Returns the value of attribute banner_position.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def banner_position
  @banner_position
end

Returns the value of attribute banner_targets.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def banner_targets
  @banner_targets
end

#inputsObject

Returns the value of attribute inputs.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def inputs
  @inputs
end

#messagesObject

Returns the value of attribute messages.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def messages
  @messages
end

Returns the value of attribute navigations.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def navigations
  @navigations
end

#request_attributesObject

Returns the value of attribute request_attributes.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def request_attributes
  @request_attributes
end

#sanitize(text) ⇒ Object

Returns the value of attribute sanitize.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def sanitize
  @sanitize
end

#sms_messageObject

Returns the value of attribute sms_message.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def sms_message
  @sms_message
end

#submit_pageObject

Returns the value of attribute submit_page.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def submit_page
  @submit_page
end

#titleObject

Returns the value of attribute title.



26
27
28
# File 'lib/sads_xml/sads.rb', line 26

def title
  @title
end

Instance Method Details

#add_attribute(req_attribute) ⇒ Object



95
96
97
# File 'lib/sads_xml/sads.rb', line 95

def add_attribute(req_attribute)
  @request_attributes<< req_attribute
end

#add_input(input) ⇒ Object



103
104
105
# File 'lib/sads_xml/sads.rb', line 103

def add_input(input)
  @inputs<< input
end

#add_navigation(link, navigation_id = :default) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/sads_xml/sads.rb', line 86

def add_navigation(link, navigation_id = :default)
  @navigations[navigation_id] = [] if @navigations[navigation_id].nil?

  # workaround for SADS Absolute Path
  #link[:pageId].gsub!(/^\/yp_mobile\//, '') unless link[:pageId].match(/\.html$/)

  @navigations[navigation_id]<< DEFAULTS[:navigation].merge(link)
end

#message=(message) ⇒ Object



46
47
48
# File 'lib/sads_xml/sads.rb', line 46

def message=(message)
  self.set_message(:default, message)
end

#one_ussd_messageObject



78
79
80
# File 'lib/sads_xml/sads.rb', line 78

def one_ussd_message
  ussd_length <= 160
end

#one_ussd_message_characters_leftObject



82
83
84
# File 'lib/sads_xml/sads.rb', line 82

def one_ussd_message_characters_left
  160 - ussd_length
end

#set_message(id, message) ⇒ Object



99
100
101
# File 'lib/sads_xml/sads.rb', line 99

def set_message(id, message)
  @messages[id.to_sym] = message
end

#to_sadsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sads_xml/sads.rb', line 112

def to_sads
  xml = Builder::XmlMarkup.new
  xml.instruct!
  xml.page :version => "2.0", "xmlns:meta" => "http://whoisd.eyeline.com/sads/meta" do
    xml.title sanitize(@title), :id => ''

    @messages.each do |key, message|
      object_hash = {}
      object_hash[:id] = key.to_s unless key == :default

      xml.div object_hash do
        unless message.blank?
          xml.div do
            lines = sanitize(message).split("\n").reverse
            while line = lines.pop
              xml.text! line
              xml.br if lines.any?
            end
          end
        end
        if @banner_position.to_s == key.to_s and @banner_targets.any?
          xml.br
          xml.br unless message.blank?
          xml.meta :banner, :target => @banner_targets.join(',') 
        end
      end
    end

    xml.div sanitize(@sms_message), :type => 'sms' unless sms_message.blank?

    if @inputs.any?
      xml.div do
        @inputs.each do |input|
          xml.input input
        end
      end
    end

    navigations.each do |key, links|
      if links.any?
        object_hash = {}
        object_hash[:id] = key.to_s unless key == :default

        xml.navigation object_hash do
          links.each do |link| xml.link sanitize(link[:title].strip), link.except(:title) end
        end
      end # eo if links.any?

      if @inputs.any?
        xml.navigation :id => 'submit' do
          xml.link :accesskey => '1', :pageId => @submit_page
        end
      end
    end

    if @request_attributes.any?
      xml.attributes do
        @request_attributes.each do |attribute|
          xml.attribute attribute
        end
      end
    end
  end
end

#ussd_lengthObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sads_xml/sads.rb', line 56

def ussd_length
  counter = 0
  counter += @title.length + 2 unless @title.blank?

  @messages.each do |key, message|
    counter += message.length + 1 unless message.blank?
  end

  @navigations.each do |key, links|
    links.each do |link|
      counter += link[:title].length + link[:accesskey].length + 2
    end
  end

  @inputs.each do |input|
    counter += input[:title].length + 2
  end

  counter -= 1
  return counter
end