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.
100 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 |
# File 'lib/remotedroid.rb', line 100 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
137 138 139 140 141 142 |
# File 'lib/remotedroid.rb', line 137 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/remotedroid.rb', line 145 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’
175 176 177 |
# File 'lib/remotedroid.rb', line 175 def op() @ed end |
#query(s) ⇒ Object
179 180 181 |
# File 'lib/remotedroid.rb', line 179 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’
186 187 188 189 190 191 192 193 194 |
# File 'lib/remotedroid.rb', line 186 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end |
#set_thing(h) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/remotedroid.rb', line 196 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
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 |
# File 'lib/remotedroid.rb', line 209 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
235 236 237 |
# File 'lib/remotedroid.rb', line 235 def to_xml(=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end |