pics_or_it_didnt_happen 1.1.3 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pics_or_it_didnt_happen.rb +34 -12
  3. metadata +18 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6499a083ae19e977da132e5a4e3d9cfc87ebcd8f15ecfc5ca58abaa238e1e927
4
- data.tar.gz: beeb9e58fed600d2eb51ef9a09ad34fa6e93106d09e585c3fa4356f8f2afdfa3
3
+ metadata.gz: d2372771aabe6cfcc3fe2945a37b1327e79ae63a15083a24e3d9b441e236f190
4
+ data.tar.gz: 876dfad3b04e2194a9e9a9a01db316e1638abb34a89e20eafcf16df90f4c043a
5
5
  SHA512:
6
- metadata.gz: 88c6e124dcc03b22a851b26ba212c5e63ec831c53a998968c5379cffbc26759ead5363e7c2ddbd27012773b92d94a0131a839add5fdde97bee3be7bfeaa4dc9e
7
- data.tar.gz: aab98afa1b45ca028cc70a8cbe8c1ee174f4adaed037ff89e8ee652e36e1258ab5c7557f7414b99a7497ae7adab75da92e2a75048779ecf31ebbfb5ecf18a3bb
6
+ metadata.gz: b55a279d23229d6cc080cc64b66a3c56b750b4167eac6d29b88fed72140f3ba6542c7d987ee1194d2b28a83dc2b90996a0e81908797420dd998cb57c6dc16f19
7
+ data.tar.gz: db4264c04c9dbe5ac220f459e30a407cf7a574fb6f3290bb09d714ab9ca56627b15a41229dd346f440ef7ea4f552f94c12d1639646d8fe4a9b746acc070b24de
@@ -1,28 +1,50 @@
1
1
  require 'base64'
2
2
 
3
3
  module PicsOrItDidntHappen
4
- def self.image_to_data_url_image_tag(file_path, alt_text: nil, classes: nil)
5
- raise ArgumentError, "No file was found at #{file_path}" unless File.exist?(file_path)
4
+ def self.image_to_data_url_image_tag(passed_image, alt_text: nil, class: nil)
5
+ raise ArgumentError, "passed_image must be a binary string or a file path" unless passed_image.is_a?(String)
6
+ # check if passed_image is a file path that exists
7
+ check_if_file_exists = Proc.new do |file_path|
8
+ begin
9
+ File.exist?(file_path)
10
+ rescue ArgumentError => e
11
+ false
12
+ end
13
+ end
14
+ if check_if_file_exists.call(passed_image)
15
+ # read the file as binary
16
+ begin
17
+ file_in_binary = IO.binread(passed_image)
18
+ rescue ArgumentError => e
19
+ raise ArgumentError, "#{passed_image} could not be read as a binary string"
20
+ end
21
+ else # passed_image may be a binary string of an image
22
+ file_in_binary = passed_image
23
+ end
24
+ # validate the keyword arguments
6
25
  raise ArgumentError, "alt_text must be a string" unless alt_text.is_a?(String) || alt_text.nil?
7
- raise ArgumentError, "classes must be a string" unless classes.is_a?(String) || classes.nil?
8
- file_in_binary = IO.binread(file_path)
26
+ klass = binding.local_variable_get(:class)
27
+ raise ArgumentError, "class must be a string or an Array of strings" unless klass.is_a?(String) || (klass.is_a?(Array) && klass.all? { |c| c.is_a?(String) } ) || klass.nil?
28
+ # convert an array of classes to a single string separated by spaces
29
+ klass = klass.join(" ") if klass.is_a?(Array)
30
+ # encode the binary string / image as base64
9
31
  base64_encoded_image_data = [file_in_binary].pack('m0')
10
- src_string = "data:#{mime_type_of(file_path)};base64,#{base64_encoded_image_data}"
11
- html_string = "<img src=\"#{src_string}\""
12
- html_string += " alt=\"#{alt_text}\"" if alt_text
13
- html_string += " class=\"#{classes}\"" if classes
14
- return html_string.strip + ">"
32
+ # build the HTML string
33
+ src_string = "data:#{mime_type_of(file_in_binary)};base64,#{base64_encoded_image_data}"
34
+ html_strings = ["src=\"#{src_string}\""]
35
+ html_strings << "alt=\"#{alt_text}\"" if alt_text
36
+ html_strings << "class=\"#{klass}\"" if klass
37
+ return "<img " + html_strings.join(" ") + "/>"
15
38
  end
16
39
 
17
40
  # this method was inspired by a Alain Beauvois's StackOverflow answer: https://stackoverflow.com/a/16635245
18
- def self.mime_type_of(file_path)
19
- raise ArgumentError, "No file was found at #{file_path}" unless File.exist?(file_path)
41
+ def self.mime_type_of(image_binary_string)
20
42
  # define some regular expressions to match the most common image file types
21
43
  png = Regexp.new("\x89PNG".force_encoding("binary"))
22
44
  jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
23
45
  jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
24
46
  # Read the first 10 bytes of the file
25
- case IO.read(file_path, 10)
47
+ case image_binary_string[0, 10].unpack1("A*") # unpack1("A*") converts the binary string to a regular string for regex matching
26
48
  # MIME types from this list: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
27
49
  when /^GIF8/
28
50
  'image/gif'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pics_or_it_didnt_happen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Matthew Crossley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-09 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: |
14
28
  Sometimes, you might want your HTML to include a one-off image file that is just for one person. Making this file public may be undesireable for security reasons, or perhaps simply because it is not worth the overhead of multiple HTTP requests.
15
29
  This gem provides a utility method that takes a locally-saved image file, perhaps within your non-public tmp directory, encodes it as Base64, and returns an HTML <img> element with the correct data URL attributes.
@@ -43,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
57
  - !ruby/object:Gem::Version
44
58
  version: '0'
45
59
  requirements: []
46
- rubygems_version: 3.4.8
60
+ rubygems_version: 3.4.14
47
61
  signing_key:
48
62
  specification_version: 4
49
63
  summary: A Ruby gem that lets you include images in HTML using data URLs (and avoid