Class: E621::Image Private
- Inherits:
-
Object
- Object
- E621::Image
- Defined in:
- lib/rubyhexagon/image.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class for post file data. This is mostly an abstraction to have data structures in a more Ruby like nature.
Instance Attribute Summary collapse
-
#ext ⇒ String
readonly
private
Extension of file.
-
#height ⇒ Integer
readonly
private
Image/video height.
-
#md5 ⇒ String
readonly
private
MD5 hash sum.
-
#url ⇒ URI
readonly
private
Url of file.
-
#width ⇒ Integer
readonly
private
Image/video width.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
private
Comparison method to comapre two Image objects (and sub class objects).
-
#initialize(file) ⇒ Object
constructor
private
Initializer for a File.
-
#save!(dirname = './', filename = nil) ⇒ Object
private
Retrieve data from e621 and save it to file.
Constructor Details
#initialize(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializer for a File. This is not a Ruby file class, but an abstraction of e621 file information.
48 49 50 51 52 53 54 55 56 |
# File 'lib/rubyhexagon/image.rb', line 48 def initialize(file) @url = URI(file[:url]) @md5 = File.basename(file[:url]).sub(/\.\w{3,4}$/, '') @ext = file[:ext] @width = file[:width].to_i @height = file[:height].to_i @user_agent = { 'User-Agent' => "#{E621::NAME}/#{E621::VERSION} (by maxine_red on e621" } end |
Instance Attribute Details
#ext ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns extension of file.
29 30 31 |
# File 'lib/rubyhexagon/image.rb', line 29 def ext @ext end |
#height ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns image/video height.
35 36 37 |
# File 'lib/rubyhexagon/image.rb', line 35 def height @height end |
#md5 ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns MD5 hash sum.
38 39 40 |
# File 'lib/rubyhexagon/image.rb', line 38 def md5 @md5 end |
#url ⇒ URI (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns url of file.
26 27 28 |
# File 'lib/rubyhexagon/image.rb', line 26 def url @url end |
#width ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns image/video width.
32 33 34 |
# File 'lib/rubyhexagon/image.rb', line 32 def width @width end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Comparison method to comapre two Image objects (and sub class objects)
63 64 65 |
# File 'lib/rubyhexagon/image.rb', line 63 def ==(other) other.is_a?(Image) && @md5 == other.md5 end |
#save!(dirname = './', filename = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieve data from e621 and save it to file.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rubyhexagon/image.rb', line 76 def save!(dirname = './', filename = nil) raise ArgumentError, 'Not a directory!' unless File.directory?(dirname) filename ||= @md5 loop do stream = open(@url, @user_agent).read File.open("#{dirname}/#{filename}.#{@ext}", 'w') do |f| f.print stream end break if Digest::MD5.hexdigest(stream) == @md5 end end |