Class: OpenWebPageAction

Inherits:
ApplicationAction show all
Defined in:
lib/ruby-macrodroid/actions.rb

Instance Attribute Summary

Attributes inherited from Action

#constraints

Attributes inherited from MacroObject

#options, #siguid, #type

Instance Method Summary collapse

Methods included from ObjectX

#action_to_object, #object_create, #varify

Methods inherited from MacroObject

#to_h

Constructor Details

#initialize(obj = {}, macro = nil) ⇒ OpenWebPageAction

Returns a new instance of OpenWebPageAction.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ruby-macrodroid/actions.rb', line 207

def initialize(obj={}, macro=nil)

  $debug = true    
  puts ('obj: ' + obj.inspect).debug if $debug
  
  h = if obj.is_a? Hash then
  
    obj.merge({macro: macro}) #unless obj and obj[:macro]
    
  elsif obj.is_a? Array
    
    puts ('obj: ' + obj.inspect).debug if $debug
    e, macro, h3 = obj
    
    a = e.xpath('item/*')

    h2 = if a.any? then
    
      a.map do |node|
        
        if node.name == 'description' and node.text.to_s =~ /: / then
          node.text.to_s.split(/: +/,2).map(&:strip)
        else
          [node.name.to_sym, node.text.to_s.strip]
        end
        
      end.to_h
      
    else
      txt = e.text('item/description')
      {url: (txt || e.text)}
    end      
    
    h2.merge(macro: macro).merge(h3)

  end

  puts ('h:' + h.inspect).debug if $debug
  
  #h[:url_to_open] = h[:url] if h[:url] and h[:url].length > 1

  options = {
    #variable_to_save_response: {:string_value=>"", :name=>"coords", 
    #decimal_value: 0.0, isLocal: true, m_boolean_value: false, 
    #excludeFromLog: false, int_value: 0, type: 2},
    url_to_open: '',
    http_get: true,
    disable_url_encode: false,
    block_next_action: false
  }
  
  return super(options.merge h) if h[:url_to_open]
  
  if h[:macro].remote_url.nil? and (h[:url].nil? or h[:url].empty?) then
    raise OpenWebPageActionError, 'remote_url not found'
  end
  
  url = if h[:url] and h[:url].length > 1 then
  
    h[:url]      

  elsif h2 and h[:macro].remote_url and h[:identifier]
    
    "%s/%s" % [h[:macro].remote_url.sub(/\/$/,''), h[:identifier]]
    
  elsif (h[:identifier].nil? or h[:identifier].empty?)         
    
    h[:url_to_open] = h[:macro].remote_url.sub(/\/$/,'') + '/' + 
        h[:macro].title.downcase.gsub(/ +/,'-')            
    
  end        
  puts 'url: ' + url.inspect if $debug
  
  if h2 then
    
    h2.delete :identifier
    h2.delete :url
    
    if h2.any? then
      url += '?' + \
          URI.escape(h2.map {|key,value| "%s=%s" % [key, value]}.join('&'))
    end
    
  end
  
  h[:url_to_open] = url    
  super(options.merge h)

end

Instance Method Details

#invokeObject



297
298
299
# File 'lib/ruby-macrodroid/actions.rb', line 297

def invoke()
  super(url: @h[:url_to_open])
end

#to_s(colour: false, indent: 0) ⇒ Object



301
302
303
304
305
# File 'lib/ruby-macrodroid/actions.rb', line 301

def to_s(colour: false, indent: 0)
  s = @h[:http_get] ? '' : 'Open Website / '
  @s = s + "HTTP GET\nurl: " + @h[:url_to_open]
  super()
end