Class: Razor::CLI::Navigate
- Inherits:
-
Object
- Object
- Razor::CLI::Navigate
- Extended by:
- Forwardable
- Defined in:
- lib/razor/cli/navigate.rb
Instance Attribute Summary collapse
-
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
Instance Method Summary collapse
- #collections ⇒ Object
- #command(name) ⇒ Object
- #command? ⇒ Boolean
- #commands ⇒ Object
- #entrypoint ⇒ Object
- #get(url, headers = {}) ⇒ Object
- #get_document ⇒ 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
Constructor Details
#initialize(parse, segments) ⇒ Navigate
Returns a new instance of Navigate.
46 47 48 49 50 51 52 |
# File 'lib/razor/cli/navigate.rb', line 46 def initialize(parse, segments) @parse = parse @segments = segments||[] @doc = entrypoint @username, @password = parse.api_url.userinfo.to_s.split(':') @doc_resource = create_resource parse.api_url, {:accept => :json} end |
Instance Attribute Details
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
54 55 56 |
# File 'lib/razor/cli/navigate.rb', line 54 def doc_resource @doc_resource end |
Instance Method Details
#collections ⇒ Object
64 65 66 |
# File 'lib/razor/cli/navigate.rb', line 64 def collections entrypoint["collections"] end |
#command(name) ⇒ Object
80 81 82 |
# File 'lib/razor/cli/navigate.rb', line 80 def command(name) @command ||= commands.find { |coll| coll["name"] == name } end |
#command? ⇒ Boolean
84 85 86 |
# File 'lib/razor/cli/navigate.rb', line 84 def command? !! command(@segments.first) end |
#commands ⇒ Object
68 69 70 |
# File 'lib/razor/cli/navigate.rb', line 68 def commands entrypoint["commands"] end |
#entrypoint ⇒ Object
60 61 62 |
# File 'lib/razor/cli/navigate.rb', line 60 def entrypoint @entrypoint ||= json_get(@parse.api_url) end |
#get(url, headers = {}) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/razor/cli/navigate.rb', line 151 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/razor/cli/navigate.rb', line 90 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 |
#json_get(url, headers = {}, params = {}) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/razor/cli/navigate.rb', line 158 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. response = get(url,headers.merge(:accept => :json)) unless response.headers[:content_type] =~ /application\/json/ raise "Received content type #{response.headers[:content_type]}" end MultiJson.load(response.body) end |
#json_post(url, body) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/razor/cli/navigate.rb', line 170 def json_post(url, body) headers = { :accept=>:json, "Content-Type" => :json } 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
56 57 58 |
# File 'lib/razor/cli/navigate.rb', line 56 def last_url @doc_resource end |
#move_to(key, doc = @doc, params = {}) ⇒ Object
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 |
# File 'lib/razor/cli/navigate.rb', line 110 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) unless obj 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) # No spec string; use parent's and remember extra navigation. 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']}) elsif obj.is_a?(Array) # No spec string; use parent's and remember extra navigation. if @doc['+spec'].is_a?(Array) # Something's already been added. @doc['+spec'] << key elsif @doc['+spec'].nil? || @doc['+spec'].is_a?(String) @doc['+spec'] = [@doc['spec'], key] end @doc = {'+spec' => @doc['+spec'], 'items' => obj} else @doc = nil end end |
#query? ⇒ Boolean
76 77 78 |
# File 'lib/razor/cli/navigate.rb', line 76 def query? @query ||= collections.any? { |coll| coll["name"] == @segments.first } end |
#server_version ⇒ Object
72 73 74 |
# File 'lib/razor/cli/navigate.rb', line 72 def server_version entrypoint.has_key?('version') and entrypoint['version']['server'] or 'Unknown' end |