fetchable-uri 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/addressable/uri/fetch.rb +55 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9fafcccaaa61ff94909646514b9b1783aa97355a
4
+ data.tar.gz: b33df92be51e4c52dde591e410ffab635eff50fd
5
+ SHA512:
6
+ metadata.gz: f661032cc39951879daa7c79508de8718a3a5b7ef715412adaa70dc37cdcc651ddfec722784f35970bef6b167ad2b37905568a38218f900b45a807d93b6afc3b
7
+ data.tar.gz: 74dc14fce53adab2d7b5e65eabd16971bc5588cc1d2234946f34ffa6f2b7a8851c09509f752b45088f0c426ca37de51838c99f4cfed93de9d8be56cc8994a496
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+ require 'addressable/uri'
3
+ require 'net/http'
4
+
5
+ module Addressable
6
+ class URI
7
+ def fetch(limit: 50, use_get: :never)
8
+ Net::HTTP.start(hostname, port, use_ssl: scheme == 'https') do |http|
9
+ case use_get
10
+ when :always
11
+ response, uri = follow(http, self, limit, :get)
12
+ when :never
13
+ response, uri = follow(http, self, limit, :head)
14
+ when :on_error
15
+ response, uri = follow(http, self, limit, :head)
16
+ response, uri = follow(http, self, (limit / 10).to_i, :get) unless response.is_a?(Net::HTTPOK)
17
+ when :on_text
18
+ response, uri = follow(http, self, limit, :head)
19
+ response, uri = follow(http, self, (limit / 10).to_i, :get) if !response.is_a?(Net::HTTPOK) || text?(response.content_type)
20
+ end
21
+
22
+ response.uri ||= uri
23
+ response
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def text?(type)
30
+ type == 'application/json' || type.start_with?('text/')
31
+ end
32
+
33
+ def follow(http, uri, limit, method)
34
+ limit.times do
35
+ request = case method
36
+ when :get
37
+ Net::HTTP::Get.new(uri)
38
+ when :head
39
+ Net::HTTP::Head.new(uri)
40
+ end
41
+ request['user-agent'] = 'Mozilla/5.0'
42
+
43
+ response = http.request(request)
44
+
45
+ return response, uri unless response.is_a?(Net::HTTPRedirection)
46
+
47
+ previous_uri = uri
48
+ uri = self.class.join(uri, response['location']).fixup!
49
+
50
+ # Detect redirect loops
51
+ return response, uri if uri == previous_uri
52
+ end
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fetchable-uri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - DragonMaus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/addressable/uri/fetch.rb
34
+ homepage: https://github.com/dragonmaus/fetchable-uri
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.5.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: URI fetching extension for Addressable::URI
58
+ test_files: []
59
+ has_rdoc: