goldfinger 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee7cd56211208bae7616ba9a6b6535bcd358f676
4
- data.tar.gz: d4bcfc1bd907072f3ef3062efd9e7644128a8779
3
+ metadata.gz: d87e2270742fca18ac73785d5099685f761a98e6
4
+ data.tar.gz: 437cc11a12334014e50d521dab4175d68e7d1fa9
5
5
  SHA512:
6
- metadata.gz: 6787387b7f7130e0862a7205f8b277b9b017d8687cf6f2cb861c21fe0045c9db775107d7ce9f3dd5ade94d1b74955de0ec9911ddbae1cff23517a62175b58600
7
- data.tar.gz: 2c7f7975273404131e0b4d2055ff2efd2f87a04f906a6cd065ef2b8af4b818d6413bbeeb7c0e4077b690fc61dd8312ce2c2eb554e3fabc1d3af8d48cf7eeb27c
6
+ metadata.gz: 1778395975ca01e8b288468035c3cbd6ccc1bc99c978f89a339399630695d085db6c0047b27a8e24f59deec0ae143867083f2d5cb2b8a46b8f22fe351528c891
7
+ data.tar.gz: c059dc6cac298573be5e7895dbe41adc6415a1afa80b006f6347970a16b80c83c9c6a8c23e2bb65102f23d6aa1167c236aafcb12ec4d7dba0e1971ab5a1581d3
data/README.md CHANGED
@@ -14,9 +14,16 @@ A Webfinger client for Ruby. Supports `application/xrd+xml` and `application/jrd
14
14
  ## Usage
15
15
 
16
16
  data = Goldfinger.finger('acct:gargron@quitter.no')
17
- data.link('http://schemas.google.com/g/2010#updates-from')[:href]
17
+
18
+ data.link('http://schemas.google.com/g/2010#updates-from').href
18
19
  # => "https://quitter.no/api/statuses/user_timeline/7477.atom"
19
20
 
21
+ data.aliases
22
+ # => ["https://quitter.no/user/7477", "https://quitter.no/gargron"]
23
+
24
+ data.subject
25
+ # => "acct:gargron@quitter.no"
26
+
20
27
  ## RFC support
21
28
 
22
- The gem only parses link data. It does not currently parse aliases, properties, or more complex structures.
29
+ The official Webfinger RFC is [7033](https://tools.ietf.org/html/rfc7033).
@@ -1,4 +1,5 @@
1
1
  require 'goldfinger/request'
2
+ require 'goldfinger/link'
2
3
  require 'goldfinger/result'
3
4
  require 'goldfinger/utils'
4
5
  require 'goldfinger/client'
@@ -13,6 +14,12 @@ module Goldfinger
13
14
  class SSLError < Error
14
15
  end
15
16
 
17
+ # Returns result for the Webfinger query
18
+ #
19
+ # @raise [Goldfinger::NotFoundError] Error raised when the Webfinger resource could not be retrieved
20
+ # @raise [Goldfinger::SSLError] Error raised when there was a SSL error when fetching the resource
21
+ # @param uri [String] A full resource identifier in the format acct:user@example.com
22
+ # @return [Goldfinger::Result]
16
23
  def self.finger(uri)
17
24
  Goldfinger::Client.new(uri).finger
18
25
  end
@@ -39,7 +39,7 @@ module Goldfinger
39
39
 
40
40
  def url_from_template(template)
41
41
  xml = Nokogiri::XML(template)
42
- links = xml.xpath('//xmlns:Link[@rel="lrdd"]', xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0')
42
+ links = xml.xpath('//xmlns:Link[@rel="lrdd"]')
43
43
 
44
44
  raise Goldfinger::NotFoundError if links.empty?
45
45
 
@@ -0,0 +1,53 @@
1
+ module Goldfinger
2
+ # @!attribute [r] href
3
+ # @return [String] The href the link points to
4
+ # @!attribute [r] type
5
+ # @return [String] The mime type of the link
6
+ # @!attribute [r] rel
7
+ # @return [String] The relation descriptor of the link
8
+ class Link
9
+ attr_reader :href, :type, :rel
10
+
11
+ def initialize(a)
12
+ @href = a[:href]
13
+ @type = a[:type]
14
+ @rel = a[:rel]
15
+ @titles = a[:titles]
16
+ @properties = a[:properties]
17
+ end
18
+
19
+ # The "titles" object comprises zero or more name/value pairs whose
20
+ # names are a language tag or the string "und". The string is
21
+ # human-readable and describes the link relation.
22
+ # @see #title
23
+ # @return [Array] Array form of the hash
24
+ def titles
25
+ @titles.to_a
26
+ end
27
+
28
+ # The "properties" object within the link relation object comprises
29
+ # zero or more name/value pairs whose names are URIs (referred to as
30
+ # "property identifiers") and whose values are strings or nil.
31
+ # Properties are used to convey additional information about the link
32
+ # relation.
33
+ # @see #property
34
+ # @return [Array] Array form of the hash
35
+ def properties
36
+ @properties.to_a
37
+ end
38
+
39
+ # Returns a title for a language
40
+ # @param lang [String]
41
+ # @return [String]
42
+ def title(lang)
43
+ @titles[lang]
44
+ end
45
+
46
+ # Returns a property for a key
47
+ # @param key [String]
48
+ # @return [String]
49
+ def property(key)
50
+ @properties[key]
51
+ end
52
+ end
53
+ end
@@ -1,17 +1,57 @@
1
1
  module Goldfinger
2
2
  class Result
3
3
  def initialize(headers, body)
4
- @mime_type = headers.get(HTTP::Headers::CONTENT_TYPE).first
5
- @body = body
6
- @links = {}
4
+ @mime_type = headers.get(HTTP::Headers::CONTENT_TYPE).first
5
+ @body = body
6
+ @subject = nil
7
+ @aliases = []
8
+ @links = {}
9
+ @properties = {}
7
10
 
8
11
  parse
9
12
  end
10
13
 
14
+ # The value of the "subject" member is a URI that identifies the entity
15
+ # that the JRD describes.
16
+ # @return [String]
17
+ def subject
18
+ @subject
19
+ end
20
+
21
+ # The "aliases" array is an array of zero or more URI strings that
22
+ # identify the same entity as the "subject" URI.
23
+ # @return [Array]
24
+ def aliases
25
+ @aliases
26
+ end
27
+
28
+ # The "properties" object comprises zero or more name/value pairs whose
29
+ # names are URIs (referred to as "property identifiers") and whose
30
+ # values are strings or nil.
31
+ # @see #property
32
+ # @return [Array] Array form of the hash
33
+ def properties
34
+ @properties.to_a
35
+ end
36
+
37
+ # Returns a property for a key
38
+ # @param key [String]
39
+ # @return [String]
40
+ def property(key)
41
+ @properties[key]
42
+ end
43
+
44
+ # The "links" array has any number of member objects, each of which
45
+ # represents a link.
46
+ # @see #link
47
+ # @return [Array] Array form of the hash
11
48
  def links
12
49
  @links.to_a
13
50
  end
14
51
 
52
+ # Returns a key for a relation
53
+ # @param key [String]
54
+ # @return [Goldfinger::Link]
15
55
  def link(rel)
16
56
  @links[rel]
17
57
  end
@@ -29,13 +69,38 @@ module Goldfinger
29
69
 
30
70
  def parse_json
31
71
  json = JSON.parse(@body)
32
- json['links'].each { |link| @links[link['rel']] = Hash[link.keys.map { |key| [key.to_sym, link[key]] }] }
72
+
73
+ @subject = json['subject']
74
+ @aliases = json['aliases'] || []
75
+ @properties = json['properties'] || {}
76
+
77
+ json['links'].each do |link|
78
+ tmp = Hash[link.keys.map { |key| [key.to_sym, link[key]] }]
79
+ @links[link['rel']] = Goldfinger::Link.new(tmp)
80
+ end
33
81
  end
34
82
 
35
83
  def parse_xml
36
84
  xml = Nokogiri::XML(@body)
37
- links = xml.xpath('//xmlns:Link', xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0')
38
- links.each { |link| @links[link.attribute('rel').value] = Hash[link.attributes.keys.map { |key| [key.to_sym, link.attribute(key).value] }] }
85
+
86
+ @subject = xml.at_xpath('//xmlns:Subject').content
87
+ @aliases = xml.xpath('//xmlns:Alias').map { |a| a.content }
88
+
89
+ properties = xml.xpath('/xmlns:XRD/xmlns:Property')
90
+ properties.each { |prop| @properties[prop.attribute('type').value] = prop.attribute('nil') ? nil : prop.content }
91
+
92
+ xml.xpath('//xmlns:Link').each do |link|
93
+ rel = link.attribute('rel').value
94
+ tmp = Hash[link.attributes.keys.map { |key| [key.to_sym, link.attribute(key).value] }]
95
+
96
+ tmp[:titles] = {}
97
+ tmp[:properties] = {}
98
+
99
+ link.xpath('.//xmlns:Title').each { |title| tmp[:titles][title.attribute('lang').value] = title.content }
100
+ link.xpath('.//xmlns:Property').each { |prop| tmp[:properties][prop.attribute('type').value] = prop.attribute('nil') ? nil : prop.content }
101
+
102
+ @links[rel] = Goldfinger::Link.new(tmp)
103
+ end
39
104
  end
40
105
  end
41
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goldfinger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugen Rochko
@@ -76,6 +76,7 @@ files:
76
76
  - README.md
77
77
  - lib/goldfinger.rb
78
78
  - lib/goldfinger/client.rb
79
+ - lib/goldfinger/link.rb
79
80
  - lib/goldfinger/request.rb
80
81
  - lib/goldfinger/result.rb
81
82
  - lib/goldfinger/utils.rb