Class: Razor::CLI::Navigate
- Inherits:
-
Object
- Object
- Razor::CLI::Navigate
- Defined in:
- lib/razor/cli/navigate.rb
Class Method Summary collapse
Instance Method Summary collapse
- #collections ⇒ Object
- #command(name) ⇒ Object
- #command? ⇒ Boolean
- #commands ⇒ Object
- #entrypoint ⇒ Object
- #extract_command ⇒ Object
- #get(url, headers = {}) ⇒ Object
- #get_document ⇒ Object
-
#initialize(parse, segments) ⇒ Navigate
constructor
A new instance of Navigate.
- #json_get(url, headers = {}) ⇒ Object
- #json_post(url, body) ⇒ Object
- #last_url ⇒ Object
- #move_to(key) ⇒ Object
- #query? ⇒ Boolean
Constructor Details
#initialize(parse, segments) ⇒ Navigate
Returns a new instance of Navigate.
8 9 10 11 12 13 |
# File 'lib/razor/cli/navigate.rb', line 8 def initialize(parse, segments) @parse = parse @segments = segments||[] @doc = entrypoint @doc_url = parse.api_url end |
Class Method Details
.annotations ⇒ Object
121 122 123 124 |
# File 'lib/razor/cli/navigate.rb', line 121 def self.annotations @@annotations ||= YAML::load_file(File::join(File::dirname(__FILE__), "navigate.yaml")) end |
.arg_type(cmd_name, arg_name) ⇒ Object
126 127 128 129 |
# File 'lib/razor/cli/navigate.rb', line 126 def self.arg_type(cmd_name, arg_name) cmd = annotations["commands"][cmd_name] cmd && cmd["args"][arg_name] end |
Instance Method Details
#collections ⇒ Object
23 24 25 |
# File 'lib/razor/cli/navigate.rb', line 23 def collections entrypoint["collections"] end |
#command(name) ⇒ Object
35 36 37 |
# File 'lib/razor/cli/navigate.rb', line 35 def command(name) commands.find { |coll| coll["name"] == name } end |
#command? ⇒ Boolean
39 40 41 |
# File 'lib/razor/cli/navigate.rb', line 39 def command? !! command(@segments.first) end |
#commands ⇒ Object
27 28 29 |
# File 'lib/razor/cli/navigate.rb', line 27 def commands entrypoint["commands"] end |
#entrypoint ⇒ Object
19 20 21 |
# File 'lib/razor/cli/navigate.rb', line 19 def entrypoint @entrypoint ||= json_get(@parse.api_url) end |
#extract_command ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/razor/cli/navigate.rb', line 62 def extract_command cmd = command(@segments.shift) body = {} until @segments.empty? if @segments.shift =~ /\A--([a-z-]+)(=(\S+))?\Z/ body[$1] = convert_arg(cmd["name"], $1, ($3 || @segments.shift)) end end body = JSON::parse(File::read(body["json"])) if body["json"] [cmd, body] end |
#get(url, headers = {}) ⇒ Object
99 100 101 102 103 |
# File 'lib/razor/cli/navigate.rb', line 99 def get(url, headers={}) response = RestClient.get url.to_s, headers puts "GET #{url.to_s}\n#{response.body}" if @parse.dump_response? response end |
#get_document ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/razor/cli/navigate.rb', line 43 def get_document if @segments.empty? entrypoint elsif query? @doc = collections while @segments.any? move_to @segments.shift end @doc elsif command? # @todo lutter 2013-08-16: None of this has any tests, and error # handling is heinous at best cmd, body = extract_command json_post(cmd["id"], body) else raise NavigationError.new(@doc_url, @segments, @doc) end end |
#json_get(url, headers = {}) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/razor/cli/navigate.rb', line 105 def json_get(url, headers = {}) response = get(url,headers.merge(:accept => :json)) unless response.headers[:content_type] =~ /application\/json/ raise "Received content type #{response.headers[:content_type]}" end JSON.parse(response.body) end |
#json_post(url, body) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/razor/cli/navigate.rb', line 113 def json_post(url, body) headers = { :accept=>:json, "Content-Type" => :json } response = RestClient.post url, body.to_json, headers puts "POST #{url.to_s}\n#{body}\n-->\n#{response.body}" if @parse.dump_response? JSON::parse(response.body) end |
#last_url ⇒ Object
15 16 17 |
# File 'lib/razor/cli/navigate.rb', line 15 def last_url @doc_url end |
#move_to(key) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/razor/cli/navigate.rb', line 75 def move_to(key) key = key.to_i if key.to_i.to_s == key if @doc.is_a? Array obj = @doc.find {|x| x.is_a?(Hash) and x["name"] == key } elsif @doc.is_a? Hash obj = @doc[key] end raise NavigationError.new(@doc_url, key, @doc) unless obj if obj.is_a?(Hash) && obj["id"] @doc = json_get(obj["id"]) # strip the wrapper around collections if @doc.is_a? Hash and @doc["items"].is_a? Array @doc = @doc["items"] end @doc_url = obj["id"] elsif obj.is_a? Hash @doc = obj else @doc = nil end end |
#query? ⇒ Boolean
31 32 33 |
# File 'lib/razor/cli/navigate.rb', line 31 def query? collections.any? { |coll| coll["name"] == @segments.first } end |