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.
- #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 localhost:8151/api via ‘HEAD` HTTP method - Use FOSS’ 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 19 |
# 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, {:accept => :json, :accept_language => accept_language} end |
Instance Attribute Details
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
51 52 53 |
# File 'lib/razor/cli/navigate.rb', line 51 def doc_resource @doc_resource end |
Instance Method Details
#accept_language ⇒ Object
144 145 146 |
# File 'lib/razor/cli/navigate.rb', line 144 def accept_language @accept_language ||= GettextSetup.candidate_locales end |
#collections ⇒ Object
61 62 63 |
# File 'lib/razor/cli/navigate.rb', line 61 def collections entrypoint["collections"] end |
#command(name) ⇒ Object
77 78 79 |
# File 'lib/razor/cli/navigate.rb', line 77 def command(name) @command ||= commands.find { |coll| coll["name"] == name } end |
#command? ⇒ Boolean
81 82 83 |
# File 'lib/razor/cli/navigate.rb', line 81 def command? !! command(@segments.first) end |
#commands ⇒ Object
65 66 67 |
# File 'lib/razor/cli/navigate.rb', line 65 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`.
25 26 27 28 29 30 31 |
# File 'lib/razor/cli/navigate.rb', line 25 def default_api if https_api_exists? [RAZOR_HTTPS_API, :https] else [RAZOR_HTTP_API, :http] end end |
#entrypoint ⇒ Object
57 58 59 |
# File 'lib/razor/cli/navigate.rb', line 57 def entrypoint @entrypoint ||= json_get(@parse.api_url) end |
#get(url, headers = {}) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/razor/cli/navigate.rb', line 155 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/razor/cli/navigate.rb', line 87 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, @segments, @doc) end end |
#head(url, headers = {}) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/razor/cli/navigate.rb', line 148 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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/razor/cli/navigate.rb', line 162 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(:accept => :json, :accept_language => accept_language)) 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
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/razor/cli/navigate.rb', line 177 def json_post(url, body) @username ||= url.user @password ||= url.password headers = { :accept=>:json, "Content-Type" => :json, :accept_language => accept_language} begin resource = create_resource(url, headers) response = resource.post MultiJson::dump(body) 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
53 54 55 |
# File 'lib/razor/cli/navigate.rb', line 53 def last_url @doc_resource end |
#move_to(key, doc = @doc, params = {}) ⇒ Object
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 |
# File 'lib/razor/cli/navigate.rb', line 107 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, 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
73 74 75 |
# File 'lib/razor/cli/navigate.rb', line 73 def query? @query ||= collections.any? { |coll| coll["name"] == @segments.first } end |
#server_version ⇒ Object
69 70 71 |
# File 'lib/razor/cli/navigate.rb', line 69 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 localhost:8151/api via ‘HEAD` HTTP method
-
Use FOSS’ localhost:8150/api
This receives an argument determining whether to be verbose about requests made.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/razor/cli/navigate.rb', line 40 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 |