Class: Rubyhexagon::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.

Author:

  • Maxine Michalski

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubyhexagon/image.rb', line 53

def initialize(file)
  @url = URI.parse(file[:url])
  @md5 = File.basename(file[:url]).sub(/\.\w{3,4}$/, '')
  @ext = file[:ext]
  @width = file[:width].to_i
  @height = file[:height].to_i
  @size = file[:size].to_i
  @user_agent = { 'User-Agent' =>
                 "#{Rubyhexagon::NAME}/#{Rubyhexagon::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



31
32
33
# File 'lib/rubyhexagon/image.rb', line 31

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



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

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



40
41
42
# File 'lib/rubyhexagon/image.rb', line 40

def md5
  @md5
end

#sizeInteger (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 file size in bytes.

Returns:

  • (Integer)

    file size in bytes

Since:

  • 1.0.0



43
44
45
# File 'lib/rubyhexagon/image.rb', line 43

def size
  @size
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



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

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



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

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)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



70
71
72
# File 'lib/rubyhexagon/image.rb', line 70

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubyhexagon/image.rb', line 83

def save!(dirname = './', filename = nil)
  raise ArgumentError, 'Not a directory!' unless File.directory?(dirname)
  filename ||= @md5
  loop do
    stream = @url.open(@user_agent).read
    File.open("#{dirname}/#{filename}.#{@ext}", 'w') do |f|
      f.print stream
    end
    break if Digest::MD5.hexdigest(stream) == @md5
  end
end