Class: RemoteDroid::Model
- Inherits:
-
Object
- Object
- RemoteDroid::Model
- Includes:
- AppRoutes
- Defined in:
- lib/remotedroid.rb
Instance Method Summary collapse
- #build(raw_requests, root: @root) ⇒ Object
- #get_thing(h) ⇒ Object
-
#initialize(obj = nil, root: 'device1', debug: false) ⇒ Model
constructor
A new instance of Model.
-
#op ⇒ Object
Object Property (op) Helpful for accessing properites in dot notation e.g.
- #query(s) ⇒ Object
-
#request(s) ⇒ Object
request accepts a string in plain english e.g.
- #set_thing(h) ⇒ Object
- #to_sliml(level: 0) ⇒ Object
- #to_xml(options = nil) ⇒ Object (also: #xml)
Constructor Details
#initialize(obj = nil, root: 'device1', debug: false) ⇒ Model
Returns a new instance of Model.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/remotedroid.rb', line 101 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
138 139 140 141 142 143 |
# File 'lib/remotedroid.rb', line 138 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/remotedroid.rb', line 146 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 |
#op ⇒ Object
Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’
176 177 178 |
# File 'lib/remotedroid.rb', line 176 def op() @ed end |
#query(s) ⇒ Object
180 181 182 |
# File 'lib/remotedroid.rb', line 180 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’
187 188 189 190 191 192 193 194 195 |
# File 'lib/remotedroid.rb', line 187 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end |
#set_thing(h) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/remotedroid.rb', line 197 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
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 |
# File 'lib/remotedroid.rb', line 210 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
236 237 238 |
# File 'lib/remotedroid.rb', line 236 def to_xml(=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end |