Class: Rubyhexagon::Post::File Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/post/file.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:

  • 3.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 an File

Parameters:

  • image (Hash)

    file information

Author:

  • Maxine Michalski

Since:

  • 3.0.0



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubyhexagon/post/file.rb', line 65

def initialize(file)
  unless file.is_a?(Hash)
    raise ArgumentError, "#{file.class} is not a Hash"
  end
  unless file.keys.sort == %i[ext height md5 size url width]
    raise ArgumentError, 'Missing keys in file Hash.'
  end
  file.each do |k, v|
    if %i[width height ext size md5].include?(k)
      instance_variable_set("@#{k}".to_sym, v)
    elsif k == :url
      @url = v.nil? ? nil : 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.

File file extension

Returns:

  • (String)

    extension of file

Since:

  • 3.0.0



34
35
36
# File 'lib/rubyhexagon/post/file.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.

File height

Returns:

  • (Integer)

    image/video height

Since:

  • 3.0.0



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

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.

File MD5 hash string

Returns:

  • (String)

    MD5 hash

Since:

  • 3.0.0



50
51
52
# File 'lib/rubyhexagon/post/file.rb', line 50

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.

File size

Returns:

  • (Integer)

    file size in bytes

Since:

  • 3.0.0



46
47
48
# File 'lib/rubyhexagon/post/file.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.

File url

Returns:

  • (URI)

    url of file or nil

Since:

  • 3.0.0



30
31
32
# File 'lib/rubyhexagon/post/file.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.

File width

Returns:

  • (Integer)

    image/video width

Since:

  • 3.0.0



38
39
40
# File 'lib/rubyhexagon/post/file.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 File 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:

  • 3.0.0



106
107
108
109
# File 'lib/rubyhexagon/post/file.rb', line 106

def ==(other)
  other.is_a?(File) && @md5 == other.md5 && @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:

  • 3.0.0



95
96
97
# File 'lib/rubyhexagon/post/file.rb', line 95

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:

  • 3.0.0



86
87
88
# File 'lib/rubyhexagon/post/file.rb', line 86

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 File into a Hash

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

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 3.0.0



118
119
120
121
122
123
124
# File 'lib/rubyhexagon/post/file.rb', line 118

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