goldfinger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41d3880f49cb4f5e7847384420cca6cca7155daf
4
+ data.tar.gz: 7d2c2223eae18798ff18d1575128a934f47470d8
5
+ SHA512:
6
+ metadata.gz: d08628684d7ca1c7b90b71a1fce7ada6b2ec1877a066b187c1c73eea10f63226361b3dd339d78008c39a8e98ad97b798edb0bf40021fbdf77fad5beb1efd5174
7
+ data.tar.gz: 844fd60a49aa651cad0995eaec7a844dc11ce31b754e44dc7525e6adbb97813c928f711fffc789ff5e4ef3fa7971583c14f48de7f540c7c1a37a27bc305390f1
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Eugen Rochko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Goldfinger, a Webfinger client for Ruby
2
+ =======================================
3
+
4
+ A Webfinger client for Ruby. Supports `application/xrd+xml` and `application/jrd+json` responses. Raises `Goldfinger::Error::NotFound` on failure to fetch the Webfinger or XRD data.
5
+
6
+ ## Installation
7
+
8
+ gem install goldfinger
9
+
10
+ ## Usage
11
+
12
+ data = Goldfinger.finger('acct:gargron@quitter.no')
13
+ data.link('http://schemas.google.com/g/2010#updates-from')[:href]
14
+ # => "https://quitter.no/api/statuses/user_timeline/7477.atom"
15
+
16
+ ## RFC support
17
+
18
+ The gem only parses link data. It does not currently parse aliases, properties, or more complex structures.
data/lib/goldfinger.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'goldfinger/request'
2
+ require 'goldfinger/result'
3
+ require 'goldfinger/utils'
4
+ require 'goldfinger/client'
5
+
6
+ module Goldfinger
7
+ module Error
8
+ class NotFound < StandardError
9
+ end
10
+ end
11
+
12
+ def self.finger(uri)
13
+ Goldfinger::Client.new(uri).finger
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ require 'addressable'
2
+ require 'nokogiri'
3
+
4
+ module Goldfinger
5
+ class Client
6
+ include Goldfinger::Utils
7
+
8
+ def initialize(uri)
9
+ @uri = uri
10
+ end
11
+
12
+ def finger
13
+ ssl = true
14
+
15
+ begin
16
+ _, template = perform_get(url(ssl))
17
+ rescue HTTP::Error
18
+ if ssl
19
+ ssl = false
20
+ retry
21
+ else
22
+ raise Goldfinger::Error::NotFound
23
+ end
24
+ end
25
+
26
+ headers, body = perform_get(url_from_template(template))
27
+ Goldfinger::Result.new(headers, body)
28
+ rescue HTTP::Error
29
+ raise Goldfinger::Error::NotFound
30
+ end
31
+
32
+ private
33
+
34
+ def url(ssl = true)
35
+ "http#{'s' if ssl}://#{domain}/.well-known/host-meta"
36
+ end
37
+
38
+ def url_from_template(template)
39
+ xml = Nokogiri::XML(template)
40
+ links = xml.xpath('//xmlns:Link[@rel="lrdd"]', xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0')
41
+
42
+ raise Goldfinger::Error::NotFound if links.empty?
43
+
44
+ url = Addressable::Template.new(links.first.attribute('template').value)
45
+ url.expand({ uri: @uri }).to_s
46
+ end
47
+
48
+ def domain
49
+ @uri.split('@').last
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ require 'http'
2
+ require 'addressable'
3
+
4
+ module Goldfinger
5
+ class Request
6
+ def initialize(request_method, path, options = {})
7
+ @request_method = request_method
8
+ @uri = Addressable::URI.parse(path)
9
+ @options = options
10
+ end
11
+
12
+ def perform
13
+ response = http_client.request(@request_method, @uri.to_s, @options)
14
+ [response.headers, response.body]
15
+ end
16
+
17
+ private
18
+
19
+ def http_client
20
+ HTTP
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ module Goldfinger
2
+ class Result
3
+ def initialize(headers, body)
4
+ @mime_type = headers.get(HTTP::Headers::CONTENT_TYPE).first
5
+ @body = body
6
+ @links = {}
7
+
8
+ parse
9
+ end
10
+
11
+ def links
12
+ @links.to_a
13
+ end
14
+
15
+ def link(rel)
16
+ @links[rel]
17
+ end
18
+
19
+ private
20
+
21
+ def parse
22
+ case @mime_type
23
+ when 'application/jrd+json'
24
+ when 'application/json'
25
+ parse_json
26
+ when 'application/xrd+xml'
27
+ parse_xml
28
+ end
29
+ end
30
+
31
+ def parse_json
32
+ json = JSON.parse(@body)
33
+ json['links'].each { |link| @links[link['rel']] = Hash[link.keys.map { |key| [key.to_sym, link[key]] }] }
34
+ end
35
+
36
+ def parse_xml
37
+ xml = Nokogiri::XML(@body)
38
+ links = xml.xpath('//xmlns:Link', xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0')
39
+ links.each { |link| @links[link.attribute('rel').value] = Hash[link.attributes.keys.map { |key| [key.to_sym, link.attribute(key).value] }] }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ module Goldfinger
2
+ module Utils
3
+ def perform_get(path, options = {})
4
+ perform_request(:get, path, options)
5
+ end
6
+
7
+ def perform_request(request_method, path, options = {})
8
+ Goldfinger::Request.new(request_method, path, options).perform
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goldfinger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugen Rochko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: A Webfinger utility for Ruby
70
+ email: eugen@zeonfederated.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE
76
+ - README.md
77
+ - lib/goldfinger.rb
78
+ - lib/goldfinger/client.rb
79
+ - lib/goldfinger/request.rb
80
+ - lib/goldfinger/result.rb
81
+ - lib/goldfinger/utils.rb
82
+ homepage: https://github.com/Gargron/goldfinger
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.0.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A Webfinger utility for Ruby
106
+ test_files: []
107
+ has_rdoc: