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



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/ruby-macrodroid.rb', line 393

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.



391
392
393
# File 'lib/ruby-macrodroid.rb', line 391

def macros
  @macros
end

Instance Method Details

#add(macro) ⇒ Object



420
421
422
# File 'lib/ruby-macrodroid.rb', line 420

def add(macro)
  @macros << macro
end

#build_hObject



424
425
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 424

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



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

def export_json()

  to_h.to_json

end

#import_json(s) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/ruby-macrodroid.rb', line 460

def import_json(s)

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

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

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

  end

  @h[:macro_list] = []
  
end

#import_xml(raws) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/ruby-macrodroid.rb', line 478

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



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/ruby-macrodroid.rb', line 494

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]}
    constraints = h[:c].map {|text| [:constraint, {}, text]}

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

  end

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

#to_hObject



521
522
523
524
525
# File 'lib/ruby-macrodroid.rb', line 521

def to_h()

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

end

#to_sObject



527
528
529
# File 'lib/ruby-macrodroid.rb', line 527

def to_s()
  @macros.map(&:to_s).join("\n\n")
end