Class: NOMS::Command::Application
- Defined in:
- lib/noms/command/application.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Should user-agent actually be here?.
-
#document ⇒ Object
Should user-agent actually be here?.
-
#options ⇒ Object
Should user-agent actually be here?.
-
#type ⇒ Object
Should user-agent actually be here?.
-
#useragent ⇒ Object
Should user-agent actually be here?.
-
#window ⇒ Object
Should user-agent actually be here?.
Attributes inherited from Base
Instance Method Summary collapse
-
#_sanitize(thing) ⇒ Object
Get rid of V8 stuff.
- #abbrev(s, limit = 10) ⇒ Object
- #display ⇒ Object
- #exitcode ⇒ Object
- #fetch! ⇒ Object
-
#initialize(origin, argv, attrs = {}) ⇒ Application
constructor
A new instance of Application.
- #render! ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(origin, argv, attrs = {}) ⇒ Application
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/noms/command/application.rb', line 25 def initialize(origin, argv, attrs={}) @document = nil @origin = NOMS::Command::URInion.parse(origin) if @origin.scheme == 'file' and @origin.host.nil? @origin.host = 'localhost' end @argv = argv @options = { } @type = nil @log = attrs[:logger] || default_logger @window = NOMS::Command::Window.new($0, @origin, :logger => @log) @log.debug "Application #{argv[0]} has origin: #{origin}" @useragent = NOMS::Command::UserAgent.new(@origin, :logger => @log, :specified_identities => (attrs[:specified_identities] || []), :cache => (attrs.has_key?(:cache) ? attrs[:cache] : true), :plaintext_identity => (attrs.has_key?(:plaintext_identity) ? attrs[:plaintext_identity] : false)) end |
Instance Attribute Details
#body ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def body @body end |
#document ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def document @document end |
#options ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def @options end |
#type ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def type @type end |
#useragent ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def useragent @useragent end |
#window ⇒ Object
Should user-agent actually be here?
21 22 23 |
# File 'lib/noms/command/application.rb', line 21 def window @window end |
Instance Method Details
#_sanitize(thing) ⇒ Object
Get rid of V8 stuff
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/noms/command/application.rb', line 195 def _sanitize(thing) if thing.kind_of? V8::Array or thing.respond_to? :to_ary thing.map do |item| _sanitize item end elsif thing.respond_to? :keys Hash[ thing.keys.map do |key| [key, _sanitize(thing[key])] end] else thing end end |
#abbrev(s, limit = 10) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/noms/command/application.rb', line 166 def abbrev(s, limit=10) if s.length > (limit - 3) s[0 .. (limit - 3)] + '...' else s end end |
#display ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/noms/command/application.rb', line 174 def display case @type when 'noms-v2' body = _sanitize(@document.body) NOMS::Command::Formatter.new(body, :logger => @log).render when 'noms-raw' @body.to_yaml when /^text(\/|$)/ @body else if @window.isatty # Should this be here? @log.warn "Unknown data of type '#{@type}' not sent to terminal" [] else @body end end end |
#exitcode ⇒ Object
94 95 96 |
# File 'lib/noms/command/application.rb', line 94 def exitcode @document ? @document.exitcode : 0 end |
#fetch! ⇒ Object
47 48 49 50 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/noms/command/application.rb', line 47 def fetch! # Get content and build object, set @type case @origin.scheme when 'file' @type = (MIME::Types.of(@origin.path).first || MIME::Types['text/plain'].first).content_type @body = File.open(@origin.path, 'r') { |fh| fh.read } when 'data' @type = @origin.mime_type raise NOMS::Command::Error.new("data URLs must contain application/json") unless @type == 'application/json' @body = @origin.data when /^http/ response, landing_url = @useragent.get(@origin) new_url = landing_url @origin = new_url @useragent.origin = new_url @window.origin = new_url if response.success? # Unlike typical ReST data sources, this # should very rarely fail unless there is # a legitimate communication issue. @type = response.content_type || 'text/plain' @body = response.body else raise NOMS::Command::Error.new("Failed to request #{@origin}: #{response.statusText}") end else raise NOMS::Command::Error.new("noms command #{@argv[0].inspect} not found: not a URL or bookmark") end case @type when /^(application|text)\/(x-|)json/ begin @body = JSON.parse(@body) rescue JSON::ParserError => e raise NOMS::Command::Error.new("JSON error in #{@origin}: #{e.}") end if @body.respond_to? :has_key? and @body.has_key? '$doctype' @type = @body['$doctype'] @document = NOMS::Command::Document.new @body @document.argv = @argv @document.exitcode = 0 else @type = 'noms-raw' end end end |
#render! ⇒ Object
98 99 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/noms/command/application.rb', line 98 def render! if @document and @document.script # Crashes when using @window as global object @v8 = V8::Context.new # Set up same-origin context and stuff--need # Ruby objects to do XHR and limit local I/O @window.document = @document @v8[:window] = @window @v8[:document] = @document @v8.eval 'var alert = function (s) { window.alert(s); };' @v8.eval 'var prompt = function (s, echo) { window.prompt(s, echo); };' @v8.eval 'var location = window.location;' @v8.eval 'var console = window.console;' NOMS::Command::XMLHttpRequest.origin = @origin NOMS::Command::XMLHttpRequest.useragent = @useragent @v8[:XMLHttpRequest] = NOMS::Command::XMLHttpRequest script_index = 0 @document.script.each do |script| if script.respond_to? :has_key? and script.has_key? '$source' # Parse relative URL and load request_error = nil begin response, landing_url = @useragent.get(script['$source']) rescue StandardError => e @log.debug "Setting request_error (#{e.class}) to #{e.})" request_error = e end # Don't need landing_url script_name = File.basename(@useragent.absolute_url(script['$source']).path) script_ref = "#{script_index},#{script_name}" if request_error.nil? and response.success? case response.content_type when /^(application|text)\/(x-|)javascript/ begin @v8.eval response.body rescue StandardError => e @log.warn "Javascript[#{script_ref}] error: #{e.}" @log.debug { e.backtrace.join("\n") } end else @log.warn "Unsupported script type '#{response.content_type.inspect}' " + "for script from #{script['$source'].inspect}" end else if request_error @log.warn "Couldn't load script from #{script['$source'].inspect} " + "(#{request_error.class}): #{request_error.})" @log.debug { request_error.backtrace.join("\n") } else @log.warn "Couldn't load script from #{script['$source'].inspect}: " + "#{response.statusText}" end end else # It's javascript text script_ref = "#{script_index},\"#{abbrev(script)}\"" begin @v8.eval script rescue StandardError => e @log.warn "Javascript[#{script_ref}] error: #{e.}" @log.debug { e.backtrace.join("\n") } end end script_index += 1 end end end |