Module: Rmobio::Utils

Defined in:
lib/rmobio/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_domain(uri) ⇒ Object

Retrieve the domain string from a given uri



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rmobio/utils.rb', line 209

def self.get_domain(uri)
  uritokens = uri.split('/')
  uritokens.each_with_index  do |token,index|
    if(token == 'w')       
      @domain = uritokens[index + 1]
      RAILS_DEFAULT_LOGGER.debug 'Utils: Setting domain to ' +
        @domain + '...' unless not defined? RAILS_DEFAULT_LOGGER
    end
  end
  @domain
end

Instance Method Details

#backURL(key) ⇒ Object

#== backURL(key) This utility handles back url to GLP if page is cached. It expects params or a session key and renders the following xml data back to the caller:

<data xmlns=“”>

<burl>blah blah url</burl>

</data>

The returned instance data <burl> is determined by the following rules: #* params if parameter exists, the url in the session is also updated #* session if params is nil #* empty string if none of the above

To access the backurl from your xfroms, use the following pattern:

<xf:instance id="homepage" src="storeBackurl?burl=SOMEURL" />

Where the controller should provide a “storeBackurl” method that just call this backURL utility:

def storeBackurl
  backURL("recipe_burl")
end

#=== Parameter key => a unique session key to store the burl for the app



178
179
180
181
182
# File 'lib/rmobio/utils.rb', line 178

def backURL(key) 
  url = backurl_xml(key)
  headers['Content-Type']='application/xml' 
  render :text => "<data xmlns=\"\">" + url  + "</data>"
end

#backurl_xml(key) ⇒ Object

if you want to just return partial backurl without headers and no rendering



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rmobio/utils.rb', line 186

def backurl_xml(key)
  if params[:burl]
    url =  params[:burl].gsub(/&/,'&amp;')
    session[key] = url
  elsif session[key]
    url = session[key]
  else 
    # don't know what to do, just return an empty url
    url = ''
  end 
  logger.debug("back url #{key}: #{url}")
  "<burl>" + url  + "</burl>"
end

#cacheControl(cacheStr = 'priority=P3;max-age=604800') ⇒ Object

#== cachControl #=== Adding cache control to response header



30
31
32
33
# File 'lib/rmobio/utils.rb', line 30

def cacheControl(cacheStr='priority=P3;max-age=604800')  
  headers.delete("Cache-Control");
  headers["Cache-Control"] = cacheStr;   
end

#cacheimgObject

#== cacheimg #=== A utility method to add cache control to images #=== This utility is deprecated, use cacheloader instead To add a cache control for an image file in RAILS_ROOT/public/images, edit the configuration file “cachehints.txt with image name and header. Ex:

logo.png: priority=P2;max-age=1296000

To access the image in your xforms, use the following pattern: <icon>img?name=logo.png</icon> Where the controller should provide an img method that just call this cacheimg utility.

Sample cachehints.txt: Skins_176A2-a.png: priority=P2;max-age=1296000 logo.png: priority=P2;max-age=1296000



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
# File 'lib/rmobio/utils.rb', line 123

def cacheimg
  public_folder = File::join RAILS_ROOT, "public/images"
  filePath = File::join RAILS_ROOT, "public/images", params['name'] 
  file = File.new(filePath,"r")
  filecontents = file.read(File.size(filePath))
  file.close()


  headers["Content-Type"] = "image/png"
  headers.delete("Cache-Control")
  fileDir = File.dirname(filePath)
  fileName = File.basename(filePath)
  hints = File::join fileDir, "cachehints.txt"
  if File.file?(hints)
    file = File.new(hints,"r")
    hintsTxt = file.read(File.size(hints))
    file.close()
    hintsTxt.each_line do |line|
      if line.index(fileName) == 0
        line.scan(/(.*):(.*)/) do |garbage, ccheader|
          headers["Cache-Control"] = ccheader.strip
        end
        break
      end
    end
  end
  render :text => filecontents
end

#cacheloaderObject

#== cacheloader #=== A utility method to add cache control to images, styles and files To add a cache control, edit the configuration file ‘cachehints.txt’ with filename and header. Here is a ample configuration:

#logo.png: priority=P2;max-age=1296000

#base.rhtml: priority=P2;max-age=1296000

The utility assumes base directory for images files in RAILS_ROOT/public/images and other files in RAILS_ROOT/apps/views.

To access the image in your xform, use the following pattern: #<icon>recipe/loader?name=logo.png</icon> To access the style in your xform, use the following pattern: #<style xmlns="http://www.mobio.com/ext" src="recipe/loader?name=layouts/base.rhtml"/>

where the controller ‘recipe’ should provide a method ‘loader’ that calls this utility:

def loader
  cacheloader
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rmobio/utils.rb', line 62

def cacheloader 
  if (name=params[:name]).nil?
    # Can't do anything, just return empty content so we don't get 'no
    # template' error.
    render :text => ''
    return
  end

  # Default cache configuration file
  cache_hints = File::join RAILS_ROOT, 'public/cachehints.txt' 

  # Add cache header
  headers.delete("Cache-Control")  
  if File.file?(cache_hints)
    file = File.new(cache_hints,"r")
    hintsTxt = file.read(File.size(cache_hints))
    file.close()
    hintsTxt.each_line do |line|
      if line.index(name) == 0
        line.scan(/(.*):(.*)/) do |garbage, ccheader|
          headers["Cache-Control"] = ccheader.strip
        end
        break
      end
    end
  end

  if name=~ /.png$/
    # for now hardcode the image directory
    filePath = File::join RAILS_ROOT, 'public/images/', name 
    file = File.new(filePath,"rb")
    filecontents = file.read(File.size(filePath))
    file.close()

    headers["Content-Type"] = "image/png"

    render :text => filecontents
    return
  elsif name =~ /.xfs$/ or name =~ /.rxf/ 
    headers["Content-Type"] = "application/mform" 
  else 
    headers["Content-Type"] = "application/xml"
  end 

  render :template => name   
end

#logHeadersObject

end backurl_xml



200
201
202
203
204
205
206
# File 'lib/rmobio/utils.rb', line 200

def logHeaders
  logger.debug( "Logging Headers...")
  request.env.keys.each do |header|
    logger.debug( header.to_s + ': ' + request.env[header].to_s)
  end
  logger.debug("End of Headers...")
end