Class: E621::Image Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.0.0

Direct Known Subclasses

Preview, Sample

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • file (Hash)

    file information

Since:

  • 1.0.0



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

#extString (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.

Returns:

  • (String)

    extension of file

Since:

  • 1.0.0



29
30
31
# File 'lib/rubyhexagon/image.rb', line 29

def ext
  @ext
end

#heightInteger (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.

Returns:

  • (Integer)

    image/video height

Since:

  • 1.0.0



35
36
37
# File 'lib/rubyhexagon/image.rb', line 35

def height
  @height
end

#md5String (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.

Returns:

  • (String)

    MD5 hash sum

Since:

  • 1.0.0



38
39
40
# File 'lib/rubyhexagon/image.rb', line 38

def md5
  @md5
end

#urlURI (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.

Returns:

  • (URI)

    url of file

Since:

  • 1.0.0



26
27
28
# File 'lib/rubyhexagon/image.rb', line 26

def url
  @url
end

#widthInteger (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.

Returns:

  • (Integer)

    image/video width

Since:

  • 1.0.0



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)

Returns:

  • (TrueClass, FalseClass)

Since:

  • 1.0.0



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.

Parameters:

  • dirname (String) (defaults to: './')

    path where to save. This needs be an existing directory. This overwrites existing files.

  • filename (String) (defaults to: nil)

    name of file, without extension

Raises:

  • (ArgumentError)

    Argument given is not an existing directory

Since:

  • 1.0.0



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