Class: Razor::CLI::Navigate
- Inherits:
-
Object
- Object
- Razor::CLI::Navigate
- Extended by:
- Forwardable
- Defined in:
- lib/razor/cli/navigate.rb
Constant Summary collapse
- RAZOR_HTTPS_API =
"https://localhost:8151/api"
- RAZOR_HTTP_API =
"http://localhost:8150/api"
Instance Attribute Summary collapse
-
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
Instance Method Summary collapse
- #accept_language ⇒ Object
- #collections ⇒ Object
- #command(name) ⇒ Object
- #command? ⇒ Boolean
- #commands ⇒ Object
-
#default_api ⇒ Object
This returns an array of two elements: - The URL that, if neither the
-u
argument nor the RAZOR_API environment variable are set, will be used. - #default_headers ⇒ Object
- #entrypoint ⇒ Object
- #get(url, headers = {}) ⇒ Object
- #get_document ⇒ Object
- #head(url, headers = {}) ⇒ Object
-
#initialize(parse, segments) ⇒ Navigate
constructor
A new instance of Navigate.
- #json_get(url, headers = {}, params = {}) ⇒ Object
- #json_post(url, body) ⇒ Object
- #last_url ⇒ Object
- #move_to(key, doc = @doc, params = {}) ⇒ Object
- #query? ⇒ Boolean
- #server_version ⇒ Object
-
#set_api_url!(parse) ⇒ Object
The order of API selection works as follows: - Use
-u
argument if defined (done elsewhere) - Use "RAZOR_API" environment variable if defined - Check PE's https://localhost:8151/api viaHEAD
HTTP method - Use FOSS' http://localhost:8150/api This receives an argument determining whether to be verbose about requests made.
Constructor Details
#initialize(parse, segments) ⇒ Navigate
Returns a new instance of Navigate.
12 13 14 15 16 17 18 |
# File 'lib/razor/cli/navigate.rb', line 12 def initialize(parse, segments) @parse = parse @segments = segments||[] set_api_url!(parse) @doc = entrypoint @doc_resource = create_resource parse.api_url, default_headers end |
Instance Attribute Details
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
50 51 52 |
# File 'lib/razor/cli/navigate.rb', line 50 def doc_resource @doc_resource end |
Instance Method Details
#accept_language ⇒ Object
143 144 145 |
# File 'lib/razor/cli/navigate.rb', line 143 def accept_language @accept_language ||= GettextSetup.candidate_locales end |
#collections ⇒ Object
60 61 62 |
# File 'lib/razor/cli/navigate.rb', line 60 def collections entrypoint["collections"] end |
#command(name) ⇒ Object
76 77 78 |
# File 'lib/razor/cli/navigate.rb', line 76 def command(name) @command ||= commands.find { |coll| coll["name"] == name } end |
#command? ⇒ Boolean
80 81 82 |
# File 'lib/razor/cli/navigate.rb', line 80 def command? !! command(@segments.first) end |
#commands ⇒ Object
64 65 66 |
# File 'lib/razor/cli/navigate.rb', line 64 def commands entrypoint["commands"] end |
#default_api ⇒ Object
This returns an array of two elements:
- The URL that, if neither the
-u
argument nor the RAZOR_API environment variable are set, will be used. - The source from which the URL was found,
:https
or:http
.
24 25 26 27 28 29 30 |
# File 'lib/razor/cli/navigate.rb', line 24 def default_api if https_api_exists? [RAZOR_HTTPS_API, :https] else [RAZOR_HTTP_API, :http] end end |
#default_headers ⇒ Object
147 148 149 150 |
# File 'lib/razor/cli/navigate.rb', line 147 def default_headers {'accept' => 'application/json', 'accept_language' => accept_language} end |
#entrypoint ⇒ Object
56 57 58 |
# File 'lib/razor/cli/navigate.rb', line 56 def entrypoint @entrypoint ||= json_get(@parse.api_url) end |
#get(url, headers = {}) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/razor/cli/navigate.rb', line 159 def get(url, headers={}) resource = create_resource(url, headers) response = resource.get print "GET #{url.to_s}\n#{response.body}\n\n" if @parse.dump_response? response end |
#get_document ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/razor/cli/navigate.rb', line 86 def get_document if @segments.empty? entrypoint elsif query? Razor::CLI::Query.new(@parse, self, collections, @segments).run elsif command? cmd = @segments.shift command = commands.find { |coll| coll["name"] == cmd } cmd_url = URI.parse(command['id']) # Ensure that we copy authentication data from our previous URL. if @doc_resource cmd_url = URI.parse(cmd_url.to_s) end command = json_get(cmd_url) Razor::CLI::Command.new(@parse, self, command, @segments, cmd_url).run else raise NavigationError.new(@doc_resource.build_url, @segments, @doc) end end |
#head(url, headers = {}) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/razor/cli/navigate.rb', line 152 def head(url, headers={}) resource = create_resource(url, headers) response = resource.head print "HEAD #{url.to_s}\n#{response.body}\n\n" if @parse.dump_response? response end |
#json_get(url, headers = {}, params = {}) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/razor/cli/navigate.rb', line 166 def json_get(url, headers = {}, params = {}) # Add extra parameters to URL. url.query = URI.encode_www_form(params) url.query = nil if url.query.empty? # Remove dangling '?' from URL. @username ||= url.user @password ||= url.password response = get(url, headers.merge(default_headers)) if response.status == 401 raise UnauthorizedError.new(url) end unless response.headers[:content_type] =~ /application\/json/ raise _('Received content type %{content_type}') % {content_type: response.headers[:content_type]} end MultiJson.load(response.body) end |
#json_post(url, body) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/razor/cli/navigate.rb', line 183 def json_post(url, body) @username ||= url.user @password ||= url.password headers = {'Content-Type' => 'application/json'}.merge(default_headers) begin resource = create_resource(url, headers) # `nil` here because the URL above is absolute, not relative. response = resource.post nil, MultiJson::dump(body) if response.status == 401 raise UnauthorizedError.new(url) end ensure if @parse.dump_response? print "POST #{url.to_s}\n#{body}\n-->\n" puts (response ? response.body : _("ERROR")) end end MultiJson::load(response.body) end |
#last_url ⇒ Object
52 53 54 |
# File 'lib/razor/cli/navigate.rb', line 52 def last_url @doc_resource.build_url end |
#move_to(key, doc = @doc, params = {}) ⇒ Object
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 |
# File 'lib/razor/cli/navigate.rb', line 106 def move_to(key, doc = @doc, params = {}) @doc = doc if @doc.is_a? Array obj = @doc.find {|x| x.is_a?(Hash) and x["name"] == key } elsif @doc.is_a?(Hash) && @doc['items'].is_a?(Array) obj = @doc['items'].find {|x| x.is_a?(Hash) and x["name"] == key } elsif @doc.is_a?(Hash) obj = @doc[key] end raise NavigationError.new(@doc_resource.build_url, key, @doc) if obj.nil? if obj.is_a?(Hash) && obj["id"] url = URI.parse(obj["id"]) @doc = json_get(url, {}, params) elsif obj.is_a?(Hash) && obj['spec'] @doc = obj elsif obj.is_a?(Hash) || obj.is_a?(Array) # We have reached a data structure that doesn't have a spec string! # This means we should use the parent's string and keep track of which # extra navigation is needed, so we can still format the data # accordingly. if @doc['+spec'].is_a?(Array) # Something's been added. @doc['+spec'] << key elsif @doc['+spec'].nil? || @doc['+spec'].is_a?(String) @doc['+spec'] = [@doc['spec'], key] end @doc = obj.merge({'+spec' => @doc['+spec']}) if obj.is_a?(Hash) @doc = {'+spec' => @doc['+spec'], 'items' => obj} if obj.is_a?(Array) @doc else @doc = obj end end |
#query? ⇒ Boolean
72 73 74 |
# File 'lib/razor/cli/navigate.rb', line 72 def query? @query ||= collections.any? { |coll| coll["name"] == @segments.first } end |
#server_version ⇒ Object
68 69 70 |
# File 'lib/razor/cli/navigate.rb', line 68 def server_version entrypoint.has_key?('version') and entrypoint['version']['server'] or _('Unknown') end |
#set_api_url!(parse) ⇒ Object
The order of API selection works as follows:
- Use
-u
argument if defined (done elsewhere) - Use "RAZOR_API" environment variable if defined
- Check PE's https://localhost:8151/api via
HEAD
HTTP method - Use FOSS' http://localhost:8150/api This receives an argument determining whether to be verbose about requests made.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/razor/cli/navigate.rb', line 39 def set_api_url!(parse) if !!parse.api_url parse.api_url.to_s elsif ENV["RAZOR_API"] parse.parse_and_set_api_url(ENV['RAZOR_API'], :env) else url, source = default_api parse.parse_and_set_api_url(url, source) end end |