gravaty 10.0.0 → 11.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +7 -0
  3. data/Gemfile +5 -1
  4. data/README.md +219 -212
  5. data/Rakefile +9 -3
  6. data/copyright.md +33 -136
  7. data/examples/avatar.rb +70 -43
  8. data/examples/basics.rb +30 -20
  9. data/examples/profile.rb +77 -50
  10. data/examples/xmlrpc.rb +24 -10
  11. data/gravaty.gemspec +46 -25
  12. data/html/CHANGELOG.html +13 -20
  13. data/html/COPYING_md.html +8 -24
  14. data/html/Gemfile.html +7 -21
  15. data/html/Gravaty.html +49 -113
  16. data/html/Gravaty/Gravaty.html +138 -343
  17. data/html/Gravaty/ParsableDuckType.html +14 -33
  18. data/html/Gravaty/Parser.html +27 -66
  19. data/html/Gravaty/Parsers.html +3 -11
  20. data/html/Gravaty/Parsers/Avatar.html +19 -36
  21. data/html/Gravaty/Parsers/Callback.html +21 -37
  22. data/html/Gravaty/Parsers/Default.html +28 -45
  23. data/html/Gravaty/Parsers/Force.html +21 -37
  24. data/html/Gravaty/Parsers/Format.html +22 -42
  25. data/html/Gravaty/Parsers/Pixelsize.html +22 -42
  26. data/html/Gravaty/Parsers/Rating.html +22 -42
  27. data/html/Gravaty/Parsers/Secure.html +19 -36
  28. data/html/Gravaty/Parsers/Type.html +22 -42
  29. data/html/Gravaty/Utils.html +11 -13
  30. data/html/Gravaty/Utils/Downloader.html +3 -11
  31. data/html/Gravaty/Utils/Downloader/Downloader.html +25 -59
  32. data/html/Gravaty/Utils/Raisers.html +24 -55
  33. data/html/Gravaty/Utils/Rfc5322.html +12 -19
  34. data/html/Gravaty/Utils/RpcConnector.html +11 -13
  35. data/html/Gravaty/Utils/RpcConnector/RpcConnector.html +33 -73
  36. data/html/Object.html +728 -57
  37. data/html/README_md.html +105 -100
  38. data/html/Rakefile.html +9 -23
  39. data/html/copyright_md.html +17 -119
  40. data/html/created.rid +48 -55
  41. data/html/css/rdoc.css +1 -1
  42. data/html/index.html +97 -114
  43. data/html/js/navigation.js.gz +0 -0
  44. data/html/js/search_index.js +1 -1
  45. data/html/js/search_index.js.gz +0 -0
  46. data/html/js/searcher.js.gz +0 -0
  47. data/html/table_of_contents.html +219 -651
  48. data/lib/gravaty.rb +22 -15
  49. data/lib/gravaty/application.rb +40 -25
  50. data/lib/gravaty/constants.rb +20 -11
  51. data/lib/gravaty/locales/en.yml +9 -3
  52. data/lib/gravaty/locales/it.yml +9 -3
  53. data/lib/gravaty/locales/ja.yml +7 -4
  54. data/lib/gravaty/parser.rb +16 -10
  55. data/lib/gravaty/parsers/avatar.rb +11 -3
  56. data/lib/gravaty/parsers/callback.rb +13 -4
  57. data/lib/gravaty/parsers/default.rb +24 -16
  58. data/lib/gravaty/parsers/force.rb +12 -2
  59. data/lib/gravaty/parsers/format.rb +11 -2
  60. data/lib/gravaty/parsers/pixelsize.rb +12 -3
  61. data/lib/gravaty/parsers/rating.rb +12 -3
  62. data/lib/gravaty/parsers/secure.rb +11 -2
  63. data/lib/gravaty/parsers/type.rb +11 -2
  64. data/lib/gravaty/utils/downloader.rb +98 -96
  65. data/lib/gravaty/utils/raisers.rb +19 -12
  66. data/lib/gravaty/utils/rfc5322.rb +54 -41
  67. data/lib/gravaty/utils/rpc_connector.rb +19 -11
  68. data/lib/gravaty/version.rb +10 -2
  69. data/test/gravaty/locales/en.yml +29 -0
  70. data/test/gravaty/locales/it.yml +29 -0
  71. data/test/gravaty/locales/ja.yml +27 -0
  72. data/test/gravaty/locales/test_locales.rb +17 -13
  73. data/test/gravaty/parsers/test_avatar.rb +9 -1
  74. data/test/gravaty/parsers/test_callback.rb +12 -4
  75. data/test/gravaty/parsers/test_default.rb +25 -21
  76. data/test/gravaty/parsers/test_force.rb +9 -1
  77. data/test/gravaty/parsers/test_format.rb +18 -11
  78. data/test/gravaty/parsers/test_pixelsize.rb +19 -12
  79. data/test/gravaty/parsers/test_rating.rb +18 -11
  80. data/test/gravaty/parsers/test_secure.rb +9 -1
  81. data/test/gravaty/parsers/test_type.rb +14 -5
  82. data/test/gravaty/test_application.rb +14 -6
  83. data/test/gravaty/test_avatar.rb +94 -59
  84. data/test/gravaty/test_parser.rb +11 -5
  85. data/test/gravaty/test_profile.rb +47 -25
  86. data/test/gravaty/utils/test_downloader.rb +43 -28
  87. data/test/gravaty/utils/test_raisers.rb +42 -26
  88. data/test/gravaty/utils/test_rfc5322.rb +35 -13
  89. data/test/gravaty/utils/test_rpc_connector.rb +83 -39
  90. data/test/test_gravaty.rb +12 -4
  91. data/test/test_helper.rb +15 -9
  92. metadata +54 -40
  93. data/ISSUES.md +0 -62
  94. data/acknowledgements.md +0 -66
  95. data/authors.md +0 -63
  96. data/examples/cacert.pem +0 -3466
  97. data/html/ISSUES_md.html +0 -166
  98. data/html/acknowledgements_md.html +0 -161
  99. data/html/authors_md.html +0 -174
  100. data/html/examples/cacert_pem.html +0 -1078
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,20 +20,24 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  module Gravaty
22
30
  module Parsers
23
-
24
31
  # This class is an implementation of the Parsable duck type that
25
32
  # checks for avater or profile URI. The only needed parameter is a
26
33
  # boolean, with +true+ for avatar.
27
34
  #
28
35
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
36
+ # rubocop:disable Style/AsciiComments
29
37
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
38
+ # rubocop:enable Style/AsciiComments
30
39
  # License:: GNU General Public License version 3
31
40
  class Avatar
32
-
33
41
  # The parsable duck type interface to every parser usage.
34
42
  def parse(value = true)
35
43
  value ? 'avatar/' : ''
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,23 +20,28 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  module Gravaty
22
30
  module Parsers
23
-
24
31
  # This class is an implementation of the Parsable duck type that
25
32
  # provides the callback name. The only needed parameter is a valid
26
33
  # callback +String+ name.
27
34
  #
28
35
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
36
+ # rubocop:disable Style/AsciiComments
29
37
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
38
+ # rubocop:enable Style/AsciiComments
30
39
  # License:: GNU General Public License version 3
31
40
  class Callback
32
-
33
41
  # The parsable duck type interface to every parser usage.
34
42
  def parse(value = nil)
35
- return "callback=#{value}" unless value.nil? or value.empty?
43
+ return "callback=#{value}" unless value.nil? || value.empty?
44
+
36
45
  ''
37
46
  end
38
47
  end
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,6 +20,10 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  require 'cgi'
@@ -24,26 +32,26 @@ require_relative '../constants'
24
32
 
25
33
  module Gravaty
26
34
  module Parsers
27
-
28
35
  # This class is an implementation of the Parsable duck type that
29
36
  # checks the default option. The only needed parameter is a valid
30
37
  # default option.
31
38
  #
32
39
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
40
+ # rubocop:disable Style/AsciiComments
33
41
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
42
+ # rubocop:enable Style/AsciiComments
34
43
  # License:: GNU General Public License version 3
35
44
  class Default
36
-
37
45
  # The parsable duck type interface to every parser usage.
38
46
  def parse(value = nil)
39
- unless value.nil? or value.empty?
47
+ unless value.nil? || value.empty?
40
48
  unless DEFAULT_OPTIONS.include? value
41
- valid = %w(http https).index { |scheme| valid?(value, scheme) }
49
+ valid = %w[http https].index { |scheme| valid?(value, scheme) }
42
50
 
43
51
  raise ArgumentError, I18n.t('error.uri', value: value) if valid.nil?
44
52
  end
45
53
 
46
- return 'd=' + CGI::escape(value).to_s
54
+ return "d=#{CGI.escape(value)}"
47
55
  end
48
56
  ''
49
57
  end
@@ -65,23 +73,23 @@ module Gravaty
65
73
  begin
66
74
  an_uri = parser.parse an_uri[0]
67
75
 
68
- #See URI validation rules at
76
+ # See URI validation rules at
69
77
  # http://en.gravatar.com/site/implement/images/#default-image
70
78
 
71
- #rule 2
72
- return false if (an_uri.scheme == 'http') and
73
- (an_uri.port != Net::HTTP.http_default_port)
74
- return false if (an_uri.scheme == 'https') and
75
- (an_uri.port != Net::HTTP.https_default_port)
79
+ # rule 2
80
+ return false if (an_uri.scheme == 'http') &&
81
+ (an_uri.port != Net::HTTP.http_default_port)
82
+ return false if (an_uri.scheme == 'https') &&
83
+ (an_uri.port != Net::HTTP.https_default_port)
76
84
 
77
- #rule 3
85
+ # rule 3
78
86
  allowed = IMAGES_FORMATS
79
- .index { |format| an_uri.path.end_with? format }
87
+ .index { |format| an_uri.path.end_with? format }
80
88
  return false if allowed.nil?
81
89
 
82
- #rule 4
90
+ # rule 4
83
91
  return false unless an_uri.query.nil?
84
- rescue
92
+ rescue StandardError
85
93
  return false
86
94
  end
87
95
  true
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,22 +20,28 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  module Gravaty
22
30
  module Parsers
23
-
24
31
  # This class is an implementation of the Parsable duck type that
25
32
  # checks forces the default value. The only needed parameter is a
26
33
  # boolean, with +true+ for forced.
27
34
  #
28
35
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
36
+ # rubocop:disable Style/AsciiComments
29
37
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
38
+ # rubocop:enable Style/AsciiComments
30
39
  # License:: GNU General Public License version 3
31
40
  class Force
32
41
  # The parsable duck type interface to every parser usage.
33
42
  def parse(value = false)
34
43
  return 'f=y' if value
44
+
35
45
  ''
36
46
  end
37
47
  end
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,6 +20,10 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  require_relative '../constants'
@@ -23,13 +31,14 @@ require_relative '../utils/raisers'
23
31
 
24
32
  module Gravaty
25
33
  module Parsers
26
-
27
34
  # This class is an implementation of the Parsable duck type that
28
35
  # checks the profile type. The only needed parameter is a valid
29
36
  # profile format.
30
37
  #
31
38
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
39
+ # rubocop:disable Style/AsciiComments
32
40
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
41
+ # rubocop:enable Style/AsciiComments
33
42
  # License:: GNU General Public License version 3
34
43
  class Format
35
44
  include Utils::Raisers
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,6 +20,10 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  require_relative '../constants'
@@ -23,13 +31,14 @@ require_relative '../utils/raisers'
23
31
 
24
32
  module Gravaty
25
33
  module Parsers
26
-
27
34
  # This class is an implementation of the Parsable duck type that
28
35
  # checks the image size. The only needed parameter is a valid image
29
36
  # size.
30
37
  #
31
38
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
39
+ # rubocop:disable Style/AsciiComments
32
40
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
41
+ # rubocop:enable Style/AsciiComments
33
42
  # License:: GNU General Public License version 3
34
43
  class Pixelsize
35
44
  include Utils::Raisers
@@ -37,7 +46,7 @@ module Gravaty
37
46
  # The parsable duck type interface to every parser usage.
38
47
  def parse(value = nil)
39
48
  raiser_to_i('error.value', value, ALLOWED_SIZES)
40
- value.nil? ? '' : "s=#{value.to_s}"
49
+ value.nil? ? '' : "s=#{value}"
41
50
  end
42
51
  end
43
52
  end
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,6 +20,10 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  require_relative '../constants'
@@ -23,13 +31,14 @@ require_relative '../utils/raisers'
23
31
 
24
32
  module Gravaty
25
33
  module Parsers
26
-
27
34
  # This class is an implementation of the Parsable duck type that
28
35
  # checks the rating type. The only needed parameter is a valid
29
36
  # rating type.
30
37
  #
31
38
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
39
+ # rubocop:disable Style/AsciiComments
32
40
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
41
+ # rubocop:enable Style/AsciiComments
33
42
  # License:: GNU General Public License version 3
34
43
  class Rating
35
44
  include Utils::Raisers
@@ -37,7 +46,7 @@ module Gravaty
37
46
  # the parsable duck type interface to every parser usage.
38
47
  def parse(value = nil)
39
48
  raiser_downcase('error.value', value, RATING_OPTIONS)
40
- value.nil? ? '' : "rating=#{value.downcase.to_s}"
49
+ value.nil? ? '' : "rating=#{value.downcase}"
41
50
  end
42
51
  end
43
52
  end
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,17 +20,22 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  module Gravaty
22
30
  module Parsers
23
-
24
31
  # This class is an implementation of the Parsable duck type that
25
32
  # provides the secure or unsecure URI. The only needed parameter is
26
33
  # a boolean, with +true+ for secure.
27
34
  #
28
35
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
36
+ # rubocop:disable Style/AsciiComments
29
37
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
38
+ # rubocop:enable Style/AsciiComments
30
39
  # License:: GNU General Public License version 3
31
40
  class Secure
32
41
  # The parsable duck type interface to every parser usage.
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2013 Marco Bresciani
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,6 +20,10 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2013 Marco Bresciani
25
+ #
26
+ # SPDX-License-Identifier: GPL-3.0-or-later
19
27
  #++
20
28
 
21
29
  require_relative '../constants'
@@ -23,13 +31,14 @@ require_relative '../utils/raisers'
23
31
 
24
32
  module Gravaty
25
33
  module Parsers
26
-
27
34
  # This class is an implementation of the Parsable duck type that
28
35
  # checks the avatar type. The only needed parameter is a valid
29
36
  # avatar format.
30
37
  #
31
38
  # Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
39
+ # rubocop:disable Style/AsciiComments
32
40
  # Copyright:: Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
41
+ # rubocop:enable Style/AsciiComments
33
42
  # License:: GNU General Public License version 3
34
43
  class Type
35
44
  include Utils::Raisers
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # gravaty
3
- # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Marco Bresciani
5
+ # rubocop:disable Style/AsciiComments
6
+ # © 2012 Jon Maken
7
+ # rubocop:enable Style/AsciiComments
4
8
  #
5
9
  # This file is part of gravaty.
6
10
  #
@@ -16,11 +20,14 @@
16
20
  #
17
21
  # You should have received a copy of the GNU General Public License
18
22
  # along with gravaty. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+ # SPDX-FileCopyrightText: 2012 Jon Maken
25
+ #
26
+ # SPDX-License-Identifier: BSD-3-Clause
19
27
  #++
20
28
 
21
29
  module Gravaty
22
30
  module Utils
23
-
24
31
  # This module is a simple wrapper container for Peter R. Marreck's
25
32
  # gist.
26
33
  #
@@ -28,14 +35,13 @@ module Gravaty
28
35
  # License:: 3-clause BSD
29
36
  # Revision:: 2012-03-25 23:01:19 -0600
30
37
  module Downloader
31
-
32
38
  require 'net/http'
33
39
  require 'net/ftp'
34
40
  require 'openssl'
35
41
  require 'fileutils'
36
42
  require 'tempfile'
37
43
 
38
- #!/usr/bin/env ruby
44
+ # !/usr/bin/env ruby
39
45
  #
40
46
  # An HTTP/HTTPS/FTP file downloader library/CLI based upon
41
47
  # MiniPortile's HTTP implementation.
@@ -44,113 +50,109 @@ module Gravaty
44
50
  # License:: 3-clause BSD
45
51
  # Revision:: 2012-03-25 23:01:19 -0600
46
52
  class Downloader
47
-
48
53
  VERSION = '0.1.0'
49
54
 
50
55
  class << self
51
56
  attr_accessor :logger, :max_ca_verify_depth, :ftp_data_chunk_size
52
57
  end
53
58
 
54
- @logger = STDOUT.binmode
59
+ @logger = $stdout.binmode
55
60
  @max_ca_verify_depth = 5
56
61
  @ftp_data_chunk_size = 8192
57
62
 
58
-
59
63
  def self.download_file(url, full_path, count = 3)
60
- raise RuntimeError, I18n.t('error.nil') if url.nil?
64
+ raise I18n.t('error.nil') if url.nil?
61
65
  return if File.exist?(full_path)
62
66
 
63
67
  http_download(url, full_path, count)
64
68
  end
65
69
 
66
- private
67
-
68
- def self.message(text)
69
- @logger.print text
70
- @logger.flush
71
- end
72
-
73
- def self.output(text = '')
74
- @logger.puts text
75
- @logger.flush
76
- end
77
-
78
- def self.http_download(url, full_path, count)
79
- uri = URI.parse(url)
80
-
81
- begin
82
- filename = File.basename(uri.path)
83
-
84
- if ENV['HTTP_PROXY']
85
- #protocol,
86
- userinfo, proxy_host, proxy_port = URI::split(ENV['HTTP_PROXY'])
87
- proxy_user, proxy_pass = userinfo.split(/:/) if userinfo
88
- http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass)
89
- else
90
- http = Net::HTTP.new(uri.host, uri.port)
91
- end
92
-
93
- if uri.scheme.downcase == 'https'
94
- http.use_ssl = true
95
- if ENV['SSL_CERT_FILE']
96
- cert_file = ENV['SSL_CERT_FILE'].dup
97
- cert_file.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
98
- end
99
- if cert_file && File.exist?(cert_file)
100
- http.ca_file = cert_file
101
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
102
- http.verify_depth = @max_ca_verify_depth
103
- else
104
- raise <<-EOT
105
- To download using HTTPS you must first set the SSL_CERT_FILE
106
- environment variable to the path of a valid CA certificate file.
107
- A file of bundled public CA certs may be downloaded from:
108
-
109
- http://curl.haxx.se/ca/cacert.pem
110
-
111
- EOT
112
- end
113
- end
114
-
115
- http.request_get(uri) do |response|
116
- case response
117
- when Net::HTTPNotFound
118
- output '404 - Not Found'
119
- return false
120
-
121
- when Net::HTTPClientError
122
- output "Error: Client Error: #{response.inspect}"
123
- return false
124
-
125
- when Net::HTTPRedirection
126
- raise 'Too many redirections, halting.' if count <= 0
127
- url = response['location']
128
- return http_download(url, full_path, count - 1)
129
-
130
- when Net::HTTPOK
131
- temp_file = Tempfile
132
- .new("download-#{filename}")
133
- temp_file.binmode
134
- temp_file << response.body
135
-
136
- output
137
-
138
- temp_file.close
139
- File.unlink full_path if File.exist?(full_path)
140
- FileUtils.mkdir_p File.dirname(full_path)
141
- FileUtils.mv temp_file.path, full_path, force: true
142
- else
143
- puts response
144
- end
145
- end
146
-
147
- rescue Exception => e
148
- File.unlink full_path if File.exists(full_path)
149
- output "ERROR: #{e.message}"
150
- raise 'Failed to download file'
151
- end
152
- end
153
- end # Downloader
70
+ private_class_method def self.message(text)
71
+ @logger.print text
72
+ @logger.flush
73
+ end
74
+
75
+ private_class_method def self.output(text = '')
76
+ @logger.puts text
77
+ @logger.flush
78
+ end
79
+
80
+ private_class_method def self.http_download(url, full_path, count)
81
+ uri = URI.parse(url)
82
+
83
+ begin
84
+ filename = File.basename(uri.path)
85
+
86
+ if ENV['HTTP_PROXY']
87
+ # protocol,
88
+ userinfo, proxy_host, proxy_port = URI.split(ENV['HTTP_PROXY'])
89
+ proxy_user, proxy_pass = userinfo.split(/:/) if userinfo
90
+ http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass)
91
+ else
92
+ http = Net::HTTP.new(uri.host, uri.port)
93
+ end
94
+
95
+ if uri.scheme.downcase == 'https'
96
+ http.use_ssl = true
97
+ if ENV['SSL_CERT_FILE']
98
+ cert_file = ENV['SSL_CERT_FILE'].dup
99
+ cert_file.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
100
+ end
101
+ if cert_file && File.exist?(cert_file)
102
+ http.ca_file = cert_file
103
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
104
+ http.verify_depth = @max_ca_verify_depth
105
+ else
106
+ raise <<~HOWTO_MESSAGE
107
+ To download using HTTPS you must first set the SSL_CERT_FILE
108
+ environment variable to the path of a valid CA certificate file.
109
+ A file of bundled public CA certs may be downloaded from:
110
+ #{' '}
111
+ http://curl.haxx.se/ca/cacert.pem
112
+ #{' '}
113
+ HOWTO_MESSAGE
114
+ end
115
+ end
116
+
117
+ http.request_get(uri) do |response|
118
+ case response
119
+ when Net::HTTPNotFound
120
+ output '404 - Not Found'
121
+ return false
122
+
123
+ when Net::HTTPClientError
124
+ output "Error: Client Error: #{response.inspect}"
125
+ return false
126
+
127
+ when Net::HTTPRedirection
128
+ raise 'Too many redirections, halting.' if count <= 0
129
+
130
+ url = response['location']
131
+ return http_download(url, full_path, count - 1)
132
+
133
+ when Net::HTTPOK
134
+ temp_file = Tempfile
135
+ .new("download-#{filename}")
136
+ temp_file.binmode
137
+ temp_file << response.body
138
+
139
+ output
140
+
141
+ temp_file.close
142
+ File.unlink full_path if File.exist?(full_path)
143
+ FileUtils.mkdir_p File.dirname(full_path)
144
+ FileUtils.mv temp_file.path, full_path, force: true
145
+ else
146
+ puts response
147
+ end
148
+ end
149
+ rescue StandardError => e
150
+ File.unlink full_path if File.exist?(full_path)
151
+ output "ERROR: #{e.message}"
152
+ raise "Failed to download file: #{e.message}"
153
+ end
154
+ end
155
+ end
154
156
  end
155
157
  end
156
158
  end