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.



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/ruby-macrodroid.rb', line 765

def initialize(obj=nil, debug: false)

  @debug = debug    
  
  @geofences = {}
  
  if obj then
    
    raw_s, _ = RXFHelper.read(obj)    
    
    s = raw_s.strip
    
    if s[0] == '{' then
      
      import_json(s) 
      
    elsif  s[0] == '<'
      
      import_xml(s)
      @h = build_h
      
    else

      puts 's: ' + s.inspect if @debug
      
      if s =~ /m(?:acro)?:\s/ then
        
        puts 'before RowX.new' if @debug

        s2 = s.gsub(/^g:/,'geofence:').gsub(/^m:/,'macro:')\
            .gsub(/^t:/,'trigger:').gsub(/^a:/,'action:')\
            .gsub(/^c:/,'constraint:').gsub(/^#.*/,'')
        
        raw_macros, raw_geofences = s2.split(/(?=^macro:)/,2).reverse
        
        if raw_geofences then
          
          geoxml = RowX.new(raw_geofences).to_xml
          
          geodoc = Rexle.new(geoxml)  
          geofences = geodoc.root.xpath('item/geofence')        
          @geofences = fetch_geofences(geofences) if geofences.any?          
          
        end
        
        xml = RowX.new(raw_macros).to_xml
        import_rowxml(xml)
        
      elsif s =~ /^# / 
        xml = pc_to_xml(s)
        import_xml(xml)
      else
        raise MacroDroidError, 'invalid input'
      end
      
      @h = build_h
      
    end
    
  else
    
    @h = build_h()
    
    @macros = []
    
  end
end

Instance Attribute Details

#geofencesObject (readonly)

Returns the value of attribute geofences.



763
764
765
# File 'lib/ruby-macrodroid.rb', line 763

def geofences
  @geofences
end

#macrosObject (readonly)

Returns the value of attribute macros.



763
764
765
# File 'lib/ruby-macrodroid.rb', line 763

def macros
  @macros
end

#yamlObject (readonly)

Returns the value of attribute yaml.



763
764
765
# File 'lib/ruby-macrodroid.rb', line 763

def yaml
  @yaml
end

Instance Method Details

#add(macro) ⇒ Object



833
834
835
# File 'lib/ruby-macrodroid.rb', line 833

def add(macro)
  @macros << macro
end

#build_hObject



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'lib/ruby-macrodroid.rb', line 837

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



865
866
867
868
869
# File 'lib/ruby-macrodroid.rb', line 865

def export_json()

  to_h.to_json

end

#to_hObject



874
875
876
877
878
879
880
881
882
883
884
# File 'lib/ruby-macrodroid.rb', line 874

def to_h()

  h = {
    geofence_data: {
      geofence_map: @geofences.map {|key, value| [key, value.to_h] }.to_h
    },
    macro_list:  @macros.map(&:to_h)
  }
  @h.merge(h).to_camel_case

end

#to_pcObject

returns pseudocode



888
889
890
# File 'lib/ruby-macrodroid.rb', line 888

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

#to_sObject



892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/ruby-macrodroid.rb', line 892

def to_s()
  
  lines = []
  
  if @geofences.any? then
    lines << @geofences.map {|_, value| 'g: ' + value.to_s}.join("\n\n") + "\n"
  end
  
  lines << @macros.map(&:to_s).join("\n")
  lines.join("\n")
  
end

#to_summaryObject



905
906
907
# File 'lib/ruby-macrodroid.rb', line 905

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