Class: Hallon::Image

Inherits:
Base
  • Object
show all
Extended by:
Observable::Image
Includes:
Linkable, Loadable
Defined in:
lib/hallon/image.rb

Overview

Images are JPEG images that can be linked to and saved.

Instance Attribute Summary

Attributes inherited from Base

#pointer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Observable::Image

extended, initialize_callbacks, load_callback

Methods included from Loadable

#load

Methods included from Linkable

#===, included, #to_str

Methods inherited from Base

from, from_link, #is_linkable?, #session, to_link, #to_pointer, #to_s

Constructor Details

#initialize(link) ⇒ Image

Create a new instance of an Image.

Examples:

from a link

image = Hallon::Image.new("spotify:image:3ad93423add99766e02d563605c6e76ed2b0e450")

from an image id

image = Hallon::Image.new("3ad93423add99766e02d563605c6e76ed2b0e450")

Parameters:

  • link (String, Link, Spotify::Image)

    link or image id



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hallon/image.rb', line 35

def initialize(link)
  if link.respond_to?(:=~) and link =~ %r~(?:image[:/]|\A)([a-fA-F0-9]{40})\z~
    link = to_id($1)
  end

  @pointer = to_pointer(link, Spotify::Image) do
    Spotify.image_create(session.pointer, link)
  end

  subscribe_for_callbacks do |callbacks|
    Spotify.image_remove_load_callback(pointer, callbacks, nil)
    Spotify.image_add_load_callback(pointer, callbacks, nil)
  end
end

Class Method Details

.sizesObject

A list of available image sizes.



22
23
24
# File 'lib/hallon/image.rb', line 22

def self.sizes
  Spotify.enum_type(:image_size).symbols
end

Instance Method Details

#==(other) ⇒ Boolean

Overridden to first and foremost compare by id if applicable.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/hallon/image.rb', line 96

def ==(other)
  super or if other.is_a?(Image)
    raw_id == other.raw_id
  end
end

#dataString

Returns raw image data as a binary encoded string.

Returns:

  • (String)

    raw image data as a binary encoded string.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hallon/image.rb', line 79

def data
  FFI::MemoryPointer.new(:size_t) do |size|
    data = Spotify.image_data(pointer, size)
    size = size.read_size_t

    if size > 0
      return data.read_bytes(size)
    else
      return "".force_encoding("BINARY")
    end
  end
end

#formatSymbol

Returns image format, one of :jpeg or :unknown.

Returns:

  • (Symbol)

    image format, one of :jpeg or :unknown



62
63
64
# File 'lib/hallon/image.rb', line 62

def format
  Spotify.image_format(pointer)
end

Returns pointer representation of given link.

Parameters:

Returns:

  • (Spotify::Link)

    pointer representation of given link.



9
10
11
# File 'lib/hallon/image.rb', line 9

from_link :as_image do |link|
  Spotify.image_create_from_link(session.pointer, link)
end

#idString

Returns image ID as a sequence of hexadecimal digits.

Returns:

  • (String)

    image ID as a sequence of hexadecimal digits.

See Also:



68
69
70
# File 'lib/hallon/image.rb', line 68

def id
  to_hex(raw_id)
end

#loaded?Boolean

Returns true if the image is loaded.

Returns:

  • (Boolean)

    true if the image is loaded.



51
52
53
# File 'lib/hallon/image.rb', line 51

def loaded?
  Spotify.image_is_loaded(pointer)
end

#raw_idString

Returns image ID as a binary string.

Returns:

  • (String)

    image ID as a binary string.

See Also:



74
75
76
# File 'lib/hallon/image.rb', line 74

def raw_id
  Spotify.image_image_id(pointer)
end

#statusSymbol

Returns image error status.

Returns:

  • (Symbol)

    image error status.

See Also:

  • Error.explain


57
58
59
# File 'lib/hallon/image.rb', line 57

def status
  Spotify.image_error(pointer)
end

#to_hex(id) ⇒ String (protected)

Returns image id as a hexadecimal string.

Parameters:

  • id (String)

    an image id as a binary string

Returns:

  • (String)

    image id as a hexadecimal string

See Also:



106
107
108
# File 'lib/hallon/image.rb', line 106

def to_hex(id)
  id.unpack('H40')[0]
end

#to_id(hex) ⇒ String (protected)

Returns image id as a binary string.

Parameters:

  • hex (String)

    an image id as a hexadecimal string

Returns:

  • (String)

    image id as a binary string

See Also:



113
114
115
# File 'lib/hallon/image.rb', line 113

def to_id(hex)
  [hex].pack('H40')
end

Returns Link for the current object.

Returns:



13
# File 'lib/hallon/image.rb', line 13

to_link :from_image