Class: RemoteDroid::Model

Inherits:
Object
  • Object
show all
Includes:
AppRoutes
Defined in:
lib/remotedroid/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, root: 'device1', debug: false) ⇒ Model

Returns a new instance of Model.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/remotedroid/model.rb', line 6

def initialize(obj=nil, root: 'device1', debug: false)

  super()
  @root, @debug = root, debug
  @location = nil
  
  if obj then
    
    s = obj.strip
    
    puts 's: ' + s.inspect if @debug
    
    if s[0] == '<' or s.lines[1][0..1] == '  ' then
      
      puts 'before easydom' if @debug
      
      s2 = if s.lines[1][0..1] == '  ' then
      
        lines = s.lines.map do |line|
          line.sub(/(\w+) +is +(\w+)$/) {|x| "#{$1} {switch: #{$2}}" }
        end
        
        lines.join
        
      else
        s
      end
      
      @ed = EasyDom.new(s2)
    else
      build(s, root: root) 
    end

  end

end

Instance Method Details

#build(raw_requests, root: @root) ⇒ Object



43
44
45
46
47
48
# File 'lib/remotedroid/model.rb', line 43

def build(raw_requests, root: @root)

  @ed = EasyDom.new(debug: false, root: root)
  raw_requests.lines.each {|line| request(line) }

end

#get_thing(h) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/remotedroid/model.rb', line 51

def get_thing(h)
  
  h[:thing].gsub!(/ /,'_')
  
  if not h.has_key? :location then
    location = false
    h[:location] = find_path(h[:thing]) 
  else
    location = true
  end
  
  puts 'h: ' + h.inspect if @debug
  
  a = []
  a += h[:location].split(/ /)
  a << h[:thing]
  status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action])
  
  if location then
    "The %s %s is %s." % [h[:location], h[:thing], status]
  else
    "%s is %s." % [h[:thing].capitalize, status]
  end
  
end

#opObject

Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’



81
82
83
# File 'lib/remotedroid/model.rb', line 81

def op()
  @ed
end

#query(s) ⇒ Object



85
86
87
# File 'lib/remotedroid/model.rb', line 85

def query(s)
  @ed.e.element(s)
end

#request(s) ⇒ Object

request accepts a string in plain english e.g. request ‘switch the livingroom light on’



92
93
94
95
96
97
98
99
100
# File 'lib/remotedroid/model.rb', line 92

def request(s)

  params = {request: s}
  requests(params)
  h = find_request(s)

  method(h.first[-1]).call(h).gsub(/_/,' ')
  
end

#set_thing(h) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/remotedroid/model.rb', line 102

def set_thing(h)

  h[:thing].gsub!(/ /,'_')
  h[:location] = find_path(h[:thing]) unless h.has_key? :location
  
  a = []
  a += h[:location].split(/ /)
  a << h[:thing]
  
  a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value])
  
end

#to_sliml(level: 0) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/remotedroid/model.rb', line 115

def to_sliml(level: 0)
  
  s = @ed.to_sliml

  return s if level.to_i > 0
  
  lines = s.lines.map do |line|
    
    line.sub(/\{[^\}]+\}/) do |x|
      
      a = x.scan(/\w+: +[^ ]+/)
      if a.length == 1 and x[/switch:/] then

        val = x[/(?<=switch: ) *["']([^"']+)/,1]
        'is ' + val
      else
        x
      end

    end
  end
  
  lines.join
  
end

#to_xml(options = nil) ⇒ Object Also known as: xml



141
142
143
# File 'lib/remotedroid/model.rb', line 141

def to_xml(options=nil)
  @ed.xml(pretty: true).gsub(' style=\'\'','')
end