Class: XriParser::AuthorityParser
- Inherits:
-
Object
- Object
- XriParser::AuthorityParser
- Defined in:
- lib/xri_parser.rb
Instance Attribute Summary collapse
-
#authority ⇒ Object
readonly
Returns the value of attribute authority.
-
#local ⇒ Object
readonly
Returns the value of attribute local.
-
#path ⇒ Object
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#qxri ⇒ Object
readonly
Returns the value of attribute qxri.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#subsegments ⇒ Object
readonly
Returns the value of attribute subsegments.
Class Method Summary collapse
Instance Method Summary collapse
- #community ⇒ Object
- #fully_qualified ⇒ Object
-
#initialize(xri, get_root = true) ⇒ AuthorityParser
constructor
A new instance of AuthorityParser.
- #is_inumber? ⇒ Boolean
- #local_iname ⇒ Object
- #local_inumber ⇒ Object
- #normalized ⇒ Object
- #xri_strip_scheme(xri) ⇒ Object
Constructor Details
#initialize(xri, get_root = true) ⇒ AuthorityParser
Returns a new instance of AuthorityParser.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 |
# File 'lib/xri_parser.rb', line 14 def initialize(xri, get_root=true) @root = String.new @subsegments = Array.new xri = xri.strip rescue nil xri = xri_strip_scheme(xri) rescue nil @qxri = xri raise 'No XRI Received' if xri.nil? or xri.empty? #get root if get_root gcs_chars = Regexp.escape("@=!+*()") gcs_connectors = Regexp.escape("@=!+*") dns_rgx = Regexp.new("\\A(\\$dns\\*[^#{gcs_chars}]+)([#{gcs_connectors}].*)\\z", true) ## equivalent to: /\A(\$dns\*[^@=!\+\*\(\)]+)([@=!\+\*].*)\z/i ip_rgx = Regexp.new("\\A(\\$ip\\*[^#{gcs_chars}]+)([#{gcs_connectors}].*)\\z", true) ## equivalent to: /\A(\$ip\*[^@=!\+\*\(\)]+)([@=!\+\*].*)\z/i simple_roots = [?=, ?@, ?!, ?+] if simple_roots.include? xri[0] @root = xri[0].chr xri = xri[1..-1] # xri is now the rest of xri after the root elsif xri[0,2] == '$(' paren = 0 pointer = 0 @root = '$' xri = xri[1..-1] xri.each_byte do |c| paren += 1 if c == ?( paren -= 1 if c == ?) pointer += 1 @root << c.chr break if paren.zero? end xri = xri[pointer..-1] # xri is now the rest of xri after the root elsif xri =~ dns_rgx @root = $1 xri = $2 elsif xri =~ ip_rgx @root = $1 xri = $2 else raise XRIRootMissing, 'No XRI root' end end if xri.length < 1 return true end if (@root == '!') and (xri[0] != ?!) raise 'XRIs in the ! registry must start with !' end subsegment_delimiters = [?@, ?=, ?!, ?+, ?*] #add implicit * after root if neccessary xri.insert(0,'*') unless subsegment_delimiters.include? xri[0] open_paren = 0 close_paren = 0 segment_buf = String.new # get subsegments xri.each_byte do |c| open_paren += 1 if c == ?( close_paren += 1 if c == ?) # if we're inside an xref keep adding to buffer if (open_paren > 0) and (open_paren != close_paren) segment_buf << c.chr next end # starting a new subsegment if subsegment_delimiters.include? c # flush the buffer @subsegments << segment_buf unless segment_buf.empty? # initialize a new buffer segment_buf = "" # add the delimiter to the buffer segment_buf << c.chr elsif (c == ?/) or (c == ??) or (c == ?#) #end of authority parsing break else # add the character to the buffer segment_buf << c.chr end end @subsegments << segment_buf # flush buffer at the end @local = xri[@subsegments.to_s.length..-1] # the rest of xri @path = @local.index('?') ? @local[/^(.*)\?/, 1] : @local # path is everything up to a question mark @query = @local.index('?') ? @local[/\?(.*)$/, 1] : '' # query is everything after a question mark @authority = self.normalized true end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def @authority end |
#local ⇒ Object (readonly)
Returns the value of attribute local.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def local @local end |
#path ⇒ Object
Returns the value of attribute path.
12 13 14 |
# File 'lib/xri_parser.rb', line 12 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def query @query end |
#qxri ⇒ Object (readonly)
Returns the value of attribute qxri.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def qxri @qxri end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def root @root end |
#subsegments ⇒ Object (readonly)
Returns the value of attribute subsegments.
11 12 13 |
# File 'lib/xri_parser.rb', line 11 def subsegments @subsegments end |
Class Method Details
.parse(xri, get_root = true) ⇒ Object
6 7 8 |
# File 'lib/xri_parser.rb', line 6 def parse(xri, get_root=true) AuthorityParser.new(xri, get_root) end |
Instance Method Details
#community ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xri_parser.rb', line 121 def community all_but_last_seg = @subsegments[0..-2].to_s # strip off leading * if i-name and root is = or @ if @root == '@' or @root == '=' all_but_last_seg = all_but_last_seg[1..-1] if all_but_last_seg[0] == ?* end unless all_but_last_seg.nil? or all_but_last_seg.empty? @root + all_but_last_seg else @root end end |
#fully_qualified ⇒ Object
148 149 150 |
# File 'lib/xri_parser.rb', line 148 def fully_qualified @root + @subsegments.to_s end |
#is_inumber? ⇒ Boolean
116 117 118 119 |
# File 'lib/xri_parser.rb', line 116 def is_inumber? @subsegments.each {|seg| return false if seg[0] != ?!} true end |
#local_iname ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/xri_parser.rb', line 134 def local_iname unless self.is_inumber? if @subsegments.last[0] == ?* # strip off leading * on local i-name @subsegments.last[1..-1] else @subsegments.last end end end |
#local_inumber ⇒ Object
144 145 146 |
# File 'lib/xri_parser.rb', line 144 def local_inumber @subsegments.last[0..-1] if self.is_inumber? end |
#normalized ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/xri_parser.rb', line 152 def normalized if @root == '@' or @root == '=' @root + ((@subsegments.to_s[0,1] == '!') ? @subsegments.to_s : @subsegments.to_s[1..-1]) else @root + @subsegments.to_s end end |
#xri_strip_scheme(xri) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/xri_parser.rb', line 106 def xri_strip_scheme(xri) if xri[0..5] =~ /xri:\/\//i xri[6..-1] elsif xri[0..3] =~ /xri:/i xri[4..-1] else xri end end |