Class: Rubyhexagon::Post::Image Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/post/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(image) ⇒ 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 an Image

Parameters:

  • image (Hash)

    image information

Options Hash (image):

  • :url (String)

    URI location

  • :ext (String)

    extension string

  • :width (Integer)

    of image

  • :height (Integer)

    of image

  • :size (Integer)

    of image

Author:

  • Maxine Michalski

Since:

  • 1.0.0



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubyhexagon/post/image.rb', line 60

def initialize(image)
  unless image.is_a?(Hash)
    raise ArgumentError, "#{image.class} is not a Hash"
  end
  if image.keys != %i[url ext width height size]
    mis = %i[url ext width height size] - image.keys
    raise ArgumentError, "Missing key#{mis.count > 1 ? 's' : ''}: #{mis}"
  end
  image.each do |k, v|
    if %i[ext width height size].include?(k)
      instance_variable_set("@#{k}".to_sym, v)
    elsif k == :url
      @url = URI.parse(v)
    end
  end
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.

Image file extension

Returns:

  • (String)

    extension of file

Since:

  • 1.0.0



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

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.

Image height

Returns:

  • (Integer)

    image/video height

Since:

  • 1.0.0



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

def height
  @height
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.

Image file size

Returns:

  • (Integer)

    file size in bytes

Since:

  • 1.0.0



46
47
48
# File 'lib/rubyhexagon/post/image.rb', line 46

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.

Image url

Returns:

  • (URI)

    url of file

Since:

  • 1.0.0



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

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.

Image width

Returns:

  • (Integer)

    image/video width

Since:

  • 1.0.0



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

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)

Examples:

Compare two notes, that are unequal

Note.new(id: 1) == Note.new(id: 2) #=> false

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



102
103
104
# File 'lib/rubyhexagon/post/image.rb', line 102

def ==(other)
  other.is_a?(Image) && @url == other.url && @size == other.size
end

#aspect_ratioFloat

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.

A convenient function to return the aspect ratio of a given image

Returns:

  • (Float)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



91
92
93
# File 'lib/rubyhexagon/post/image.rb', line 91

def aspect_ratio
  @width / @height.to_f
end

#resolutionArray<Integer>

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.

A convenient function to return both width and height

Returns:

  • (Array<Integer>)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



82
83
84
# File 'lib/rubyhexagon/post/image.rb', line 82

def resolution
  [@width, @height]
end

#to_hashHash

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.

Turn object into a hash representation of itself

Examples:

Turn an Image into a Hash

Note.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



113
114
115
116
117
118
119
# File 'lib/rubyhexagon/post/image.rb', line 113

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end