Class: MacroDroid

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-macrodroid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, debug: false) ⇒ MacroDroid

Returns a new instance of MacroDroid.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/ruby-macrodroid.rb', line 325

def initialize(obj=nil, debug: false)

  @debug = debug    
  
  if obj then
    
    s, _ = RXFHelper.read(obj)    
    
    if s[0] == '{' then
      import_json(s) 
    elsif  s[0] == '<'
      import_xml(s)
      @h = build_h
    else
      import_xml(text_to_xml(s))
      @h = build_h
    end
    
  else
    
    @h = build_h()
    
    @macros = []
    
  end
end

Instance Attribute Details

#macrosObject (readonly)

Returns the value of attribute macros.



323
324
325
# File 'lib/ruby-macrodroid.rb', line 323

def macros
  @macros
end

Instance Method Details

#add(macro) ⇒ Object



352
353
354
# File 'lib/ruby-macrodroid.rb', line 352

def add(macro)
  @macros << macro
end

#build_hObject



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/ruby-macrodroid.rb', line 356

def build_h()
  
  puts 'inside Macro#build_h' if @debug
  {
    cell_tower_groups: [],
    cell_towers_ignore: [],
    drawer_configuration: {
      drawer_items: [],
      background_color: -1,
      header_color: 12692882,
      left_side: false,
      swipe_area_color: -7829368,
      swipe_area_height: 20,
      swipe_area_offset: 40,
      swipe_area_opacity: 80,
      swipe_area_width: 14,
      visible_swipe_area_width: 0
    },
    variables: [],
    user_icons: [],
    geofence_data: {
      geofence_map: {}
    },
    macro_list: []

  }    
end

#export_jsonObject Also known as: to_json



384
385
386
387
388
# File 'lib/ruby-macrodroid.rb', line 384

def export_json()

  to_h.to_json

end

#import_json(s) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/ruby-macrodroid.rb', line 392

def import_json(s)

  @h = JSON.parse(s, symbolize_names: true).to_snake_case
  puts ('@h: ' + @h.pretty_inspect).debug if @debug

  @macros = @h[:macro_list].map do |macro|

    puts ('macro: ' + macro.pretty_inspect).debug if @debug
    m = Macro.new(debug: @debug)
    m.import_h(macro)
    m

  end

  @h[:macro_list] = []
  
end

#import_xml(raws) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/ruby-macrodroid.rb', line 410

def import_xml(raws)
  
  s = RXFHelper.read(raws).first
  puts 's: ' + s.inspect if @debug
  doc = Rexle.new(s)
  puts 'after doc' if @debug
  
  @macros = doc.root.xpath('macro').map do |node|
        
    macro = Macro.new @title, debug: @debug
    macro.import_xml(node)
    macro
    
  end
end

#text_to_xml(s) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/ruby-macrodroid.rb', line 426

def text_to_xml(s)
  
  a = s.split(/.*(?=^m:)/); a.shift
  a.map!(&:chomp)

  macros = a.map do |x|

    lines = x.lines
    puts 'lines: ' + lines.inspect if @debug
    
    name = lines.shift[/^m: +(.*)/,1]
    h = {t: [], a: [], c: []}

    lines.each {|line| h[line[0].to_sym] << line[/^\w: +(.*)/,1] }
    triggers = h[:t].map {|text| [:trigger, {}, text]}
    actions = h[:a].map {|text| [:action, {}, text]}

    [:macro, {name: name},'', *triggers, *actions]

  end

  doc = Rexle.new([:macros, {}, '', *macros])
  doc.root.xml pretty: true    
  
end

#to_hObject



452
453
454
455
456
# File 'lib/ruby-macrodroid.rb', line 452

def to_h()

  @h.merge(macro_list:  @macros.map(&:to_h)).to_camel_case

end