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 (private)

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")

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 (private)

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 (private)

Overridden to first and foremost compare by id if applicable.


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 (private)


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 (private)


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

def format
  Spotify.image_format(pointer)
end

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 (private)

Returns 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 (private)


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

def loaded?
  Spotify.image_is_loaded(pointer)
end

#raw_idString (private)

Returns 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 (private)

Returns 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.

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.

See Also:


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

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

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

to_link :from_image