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.
10 11 12 13 14 15 16 |
# File 'lib/razor/cli/navigate.rb', line 10 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.
18 19 20 |
# File 'lib/razor/cli/navigate.rb', line 18 def doc_resource @doc_resource end |
Instance Method Details
#collections ⇒ Object
28 29 30 |
# File 'lib/razor/cli/navigate.rb', line 28 def collections entrypoint["collections"] end |
#command(name) ⇒ Object
44 45 46 |
# File 'lib/razor/cli/navigate.rb', line 44 def command(name) @command ||= commands.find { |coll| coll["name"] == name } end |
#command? ⇒ Boolean
48 49 50 |
# File 'lib/razor/cli/navigate.rb', line 48 def command? !! command(@segments.first) end |
#commands ⇒ Object
32 33 34 |
# File 'lib/razor/cli/navigate.rb', line 32 def commands entrypoint["commands"] end |
#entrypoint ⇒ Object
24 25 26 |
# File 'lib/razor/cli/navigate.rb', line 24 def entrypoint @entrypoint ||= json_get(@parse.api_url) end |
#get(url, headers = {}) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/razor/cli/navigate.rb', line 107 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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/razor/cli/navigate.rb', line 54 def get_document if @segments.empty? entrypoint elsif query? Razor::CLI::Query.new(@parse, self, collections, @segments).run elsif command? Razor::CLI::Command.new(@parse, self, commands, @segments).run else raise NavigationError.new(@doc_resource, @segments, @doc) end end |
#json_get(url, headers = {}, params = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/razor/cli/navigate.rb', line 114 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
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/razor/cli/navigate.rb', line 126 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
20 21 22 |
# File 'lib/razor/cli/navigate.rb', line 20 def last_url @doc_resource end |
#move_to(key, doc = @doc, params = {}) ⇒ Object
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 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/razor/cli/navigate.rb', line 66 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
40 41 42 |
# File 'lib/razor/cli/navigate.rb', line 40 def query? @query ||= collections.any? { |coll| coll["name"] == @segments.first } end |
#server_version ⇒ Object
36 37 38 |
# File 'lib/razor/cli/navigate.rb', line 36 def server_version entrypoint.has_key?('version') and entrypoint['version']['server'] or 'Unknown' end |