file_to_data_uri 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +11 -5
- data/file_to_data_uri.gemspec +1 -1
- data/lib/file_to_data_uri/version.rb +2 -2
- data/lib/file_to_data_uri.rb +12 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e69a903b1dddc2738d8774783c256f92a98e3449e17ee050b27371b425b76749
|
4
|
+
data.tar.gz: 51d706f2508bc19cf9c932362a4f9b52539a940fad4d332805e9770d05851b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48952831c00c1a4296599bffea8b2b0f1928fe9be63bd373fdeb62f72b52a076bfbefe86ab17cf7375e4d0a2d8eb4889e0c873b15b99a0673258e8bc813bbbda
|
7
|
+
data.tar.gz: 9cdd2e783859a204b1e6ed56059a7b524c5dd6529d20cd2e9c6a5601710781b8bea60940da87235e8cd1e0647e2e107c804c960e69874a82da816497dc577c09
|
data/README.md
CHANGED
@@ -30,16 +30,22 @@ gem install file_to_data_uri
|
|
30
30
|
|
31
31
|
```ruby
|
32
32
|
# With a file path
|
33
|
-
data_uri = DataURI.
|
34
|
-
data_uri.to_s # => "data:image/jpeg;base64,..."
|
33
|
+
data_uri = DataURI.convert("path/to/image.jpg") # => "data:image/jpeg;base64,..."
|
35
34
|
|
36
35
|
# With a file-like object (anything that responds to `read`)
|
37
36
|
file = File.open("image.png")
|
38
|
-
data_uri = DataURI.
|
39
|
-
data_uri.to_s # => "data:image/png;base64,..."
|
37
|
+
data_uri = DataURI.convert(file) # => "data:image/png;base64,..."
|
40
38
|
|
41
39
|
# Direct use in HTML
|
42
|
-
# <img src="<%= DataURI.
|
40
|
+
# <img src="<%= DataURI.convert('logo.png') %>">
|
41
|
+
```
|
42
|
+
|
43
|
+
### Legacy Usage (still supported)
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# With a file path
|
47
|
+
data_uri = DataURI.new("path/to/image.jpg")
|
48
|
+
data_uri.to_s # => "data:image/jpeg;base64,..."
|
43
49
|
```
|
44
50
|
|
45
51
|
### Supported Input Types
|
data/file_to_data_uri.gemspec
CHANGED
data/lib/file_to_data_uri.rb
CHANGED
@@ -6,6 +6,14 @@ require_relative "file_to_data_uri/version"
|
|
6
6
|
|
7
7
|
# DataURI class for generating data URIs from files or file-like objects
|
8
8
|
class DataURI
|
9
|
+
# Convert a file path or file-like object to a data URI string
|
10
|
+
#
|
11
|
+
# @param source [String, #read] File path or file-like object
|
12
|
+
# @return [String] A data URI representation of the file
|
13
|
+
def self.convert(source)
|
14
|
+
new(source).to_s
|
15
|
+
end
|
16
|
+
|
9
17
|
# Initialize with a file path or file-like object
|
10
18
|
#
|
11
19
|
# @param source [String, #read] File path or file-like object
|
@@ -52,9 +60,9 @@ class DataURI
|
|
52
60
|
elsif @source.respond_to?(:path) && @source.path
|
53
61
|
# If it's a file object with a path
|
54
62
|
mime_from_path(@source.path)
|
55
|
-
elsif @source.is_a?(String)
|
56
|
-
# If it's a file path as string
|
57
|
-
mime_from_path(@source)
|
63
|
+
elsif @source.is_a?(String) || @source.respond_to?(:to_s)
|
64
|
+
# If it's a file path as string or can be converted to string (like Pathname)
|
65
|
+
mime_from_path(@source.to_s)
|
58
66
|
else
|
59
67
|
# Default to octet-stream if we can't determine
|
60
68
|
"application/octet-stream"
|
@@ -69,4 +77,4 @@ class DataURI
|
|
69
77
|
mime = MIME::Types.type_for(path.to_s).first
|
70
78
|
mime ? mime.to_s : "application/octet-stream"
|
71
79
|
end
|
72
|
-
end
|
80
|
+
end
|