Class: RestTree
Class Method Summary collapse
-
.extract_root_verb(path) ⇒ Object
Pull out root.
-
.extract_verb(line) ⇒ Object
Pull out verb.
-
.handles?(list) ⇒ Boolean
Tell Launcher whether we’re in a rest tree.
- .launch(options = {}) ⇒ Object
- .launch_inner(path, children) ⇒ Object
- .remove_before_root(list) ⇒ Object
- .request(verb, url, body = nil) ⇒ Object
Class Method Details
.extract_root_verb(path) ⇒ Object
Pull out root
56 57 58 59 |
# File 'lib/xiki/rest_tree.rb', line 56 def self.extract_root_verb path path.first.sub! /^ *(GET|PUT|POST|DELETE) /, "\\2" $1 end |
.extract_verb(line) ⇒ Object
Pull out verb
62 63 64 65 |
# File 'lib/xiki/rest_tree.rb', line 62 def self.extract_verb line line.sub! /^ *(GET|PUT|POST|DELETE) ?(.*)/, "\\2" $1 end |
.handles?(list) ⇒ Boolean
Tell Launcher whether we’re in a rest tree
51 52 53 |
# File 'lib/xiki/rest_tree.rb', line 51 def self.handles? list list.index{|i| i =~ /^(GET|PUT|POST|DELETE)/} end |
.launch(options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/xiki/rest_tree.rb', line 31 def self.launch ={} Tree.plus_to_minus_maybe verb, url, body = self.launch_inner [:path], Tree.children url = "http://#{url}" unless url =~ %r"http://" result = self.request verb, url, body result = "#{result}\n" unless result =~ /\n\z/ result.gsub! "\cm", '' # Quote unless begins with "|" result.gsub! /^/, "| " result.gsub! /^\| ( *[-+] )/, "\\1" Tree.<< result, :escape=>'', :no_slash=>1 nil end |
.launch_inner(path, children) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/xiki/rest_tree.rb', line 3 def self.launch_inner path, children self.remove_before_root path # Extract verbs from root and current line root_verb = self.extract_root_verb path verb = self.extract_verb path.last body = nil # If line had verb if verb if ! path.last.empty? # If line isn't blank, use as body body = path.last path.pop else # Remove quoted children children = children ? children.select{|e| e !~ /^\|/} : nil if children and children.any? # If line blank and has children, use them as body body = children.map{|i| "#{i.sub(/^\s+/, '')}\n"}.join('') end end end verb ||= root_verb [verb || root_verb, path.join(''), body] end |
.remove_before_root(list) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/xiki/rest_tree.rb', line 67 def self.remove_before_root list # Delete from beginning until root is found while list.first !~ /^ *(GET|PUT|POST|DELETE) / break unless list.any? list.shift end list end |
.request(verb, url, body = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/xiki/rest_tree.rb', line 76 def self.request verb, url, body=nil begin net_http_class = Net::HTTP.const_get(verb.capitalize) url.gsub!('"', '%22') uri = URI.parse(url) req = net_http_class.new(uri.request_uri) req.body = body res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req) } (res.code == '200' ? '' : "#{res.code} ") + res.body rescue Exception=>e e. end end |