Module: Rho::RhoSupport

Defined in:
lib/framework/rho/rhosupport.rb,
lib/framework/autocomplete/Rho.rb

Defined Under Namespace

Classes: UrlEncodedPairParser

Constant Summary collapse

USE_BBSQLITE =
System.get_property('platform') == 'Blackberry' && System.get_property('os_version')[0].to_i() >= 5

Class Method Summary collapse

Class Method Details

._url_decode(str, regex) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/framework/rho/rhosupport.rb', line 73

def _url_decode(str, regex)
  return str if str.nil? || str.length() == 0
  
  isEncoded = false
  res = str.tr('+',' ').gsub(regex) { 
    isEncoded = true
    #[$&[1, 2].hex].pack('C') 
    [$1.delete('%')].pack('H*')
  }
  if isEncoded
      res.force_encoding("ASCII-8BIT") #need for BB since Java string is UTF16
      res.force_encoding('UTF-8')
  end
  
  res    
end

.binary_decode(str) ⇒ Object



62
63
64
65
66
# File 'lib/framework/rho/rhosupport.rb', line 62

def binary_decode(str)
  return str unless USE_BBSQLITE
  
  str.binary_decode
end

.binary_encode(str) ⇒ Object

def url_decode(s)

s.tr('+',' ').gsub(/((?:%[0-9a-fA-f]{2})+)/n) do
  [$1.delete('%')].pack('H*')  
end

end



56
57
58
59
60
# File 'lib/framework/rho/rhosupport.rb', line 56

def binary_encode(str)
  return str unless USE_BBSQLITE
  
  str.binary_encode
end

.form_decode(str) ⇒ Object

def unescape_form(str)

_unescape(str.gsub(/\+/, " "), ESCAPED)

end



95
96
97
# File 'lib/framework/rho/rhosupport.rb', line 95

def form_decode(str)
  _url_decode(str, /%([0-9a-fA-F]{2})/)
end

.parse_query_parameters(query_string) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/framework/rho/rhosupport.rb', line 99

def parse_query_parameters(query_string)
  return {} if query_string.nil?

  pairs = query_string.split('&').collect do |chunk|
    next if !chunk || chunk.empty?
    key, value = chunk.split('=', 2)
    next if !key || key.empty?
    value = value.nil? ? nil : form_decode(value)
    [ form_decode(key), value ]
  end.compact

  UrlEncodedPairParser.new(pairs).result
end

.query_params(req) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/framework/rho/rhosupport.rb', line 113

def query_params(req)
  params = {}
  unless req['id'].nil?
    params['id'] = req['id']
  end
  unless req['request-query'].nil? or req['request-query'].length == 0
    params.merge!(parse_query_parameters(req['request-query']))				
  end
  unless req['headers'].nil? or req['headers']['Content-Type'].nil?
    unless req['headers']['Content-Type'].index('application/x-www-form-urlencoded').nil?
      params.merge!(parse_query_parameters(req['request-body']))
    end	
  end
  
  skip_params = Rho::RhoConfig.log_skip_post
  if skip_params.to_s != 'true' && skip_params.to_s != '1'
      puts "Params: " + params.to_s unless params.empty?
  end
      
  params
end

.rhobundle_download(download_url, download_callback) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/framework/rho/rhosupport.rb', line 234

def self.rhobundle_download(download_url, download_callback)

    file_name = rhobundle_getfilename()
    dir_name = File.dirname(file_name)
    if Dir.exists?(dir_name) && System.delete_folder(dir_name) != 0
        return false
    end
    
    Dir.mkdir(dir_name) unless Dir.exists?(dir_name)
    
    Rho::AsyncHttp.download_file(
             :url => download_url,
             :filename => file_name,
             :headers => {},
             :callback => download_callback ) if download_url
             
    return true
end

.rhobundle_getfilenameObject



230
231
232
# File 'lib/framework/rho/rhosupport.rb', line 230

def self.rhobundle_getfilename()
    File.join( __rhoGetUserDir(), '/RhoBundle/upgrade_bundle.zip')
end

.url_decode(str) ⇒ Object

def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end



69
70
71
# File 'lib/framework/rho/rhosupport.rb', line 69

def url_decode(str)
  _url_decode(str, /((?:%[0-9a-fA-f]{2})+)/n)
end

.url_encode(s) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/framework/rho/rhosupport.rb', line 36

def url_encode(s)
#        s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
#          sprintf("%%%02X", $&.unpack("C")[0])
#        }
  s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n) do
      us = $&
      tmp = ''
      us.each_byte do |uc|
        tmp << sprintf('%%%02X', uc)
      end
      tmp
  end.force_encoding("ASCII-8BIT")

end