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
- #accept_language ⇒ Object
- #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 @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.
18 19 20 |
# File 'lib/razor/cli/navigate.rb', line 18 def doc_resource @doc_resource end |
Instance Method Details
#accept_language ⇒ Object
111 112 113 |
# File 'lib/razor/cli/navigate.rb', line 111 def accept_language @accept_language ||= GettextSetup.candidate_locales end |
#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
115 116 117 118 119 120 |
# File 'lib/razor/cli/navigate.rb', line 115 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 65 66 67 68 69 70 71 72 |
# 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? 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/razor/cli/navigate.rb', line 122 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/razor/cli/navigate.rb', line 137 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
20 21 22 |
# File 'lib/razor/cli/navigate.rb', line 20 def last_url @doc_resource end |
#move_to(key, doc = @doc, params = {}) ⇒ Object
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 106 107 108 109 |
# File 'lib/razor/cli/navigate.rb', line 74 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
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 |