Class: RemoteDroid::Model

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Model.



201
202
203
204
205
206
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
# File 'lib/remotedroid.rb', line 201

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



238
239
240
241
242
243
# File 'lib/remotedroid.rb', line 238

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



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
# File 'lib/remotedroid.rb', line 246

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’



276
277
278
# File 'lib/remotedroid.rb', line 276

def op()
  @ed
end

#query(s) ⇒ Object



280
281
282
# File 'lib/remotedroid.rb', line 280

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’



287
288
289
290
291
292
293
294
295
# File 'lib/remotedroid.rb', line 287

def request(s)

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

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

#set_thing(h) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/remotedroid.rb', line 297

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/remotedroid.rb', line 310

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



336
337
338
# File 'lib/remotedroid.rb', line 336

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