Class: Hallon::Image
- Extended by:
- Observable::Image
- 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
Class Method Summary collapse
-
.sizes ⇒ Object
A list of available image sizes.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Overridden to first and foremost compare by id if applicable.
-
#data ⇒ String
Raw image data as a binary encoded string.
-
#format ⇒ Symbol
Image format, one of
:jpeg
or:unknown
. -
#from_link ⇒ Spotify::Link
private
Pointer representation of given link.
-
#id ⇒ String
Image ID as a sequence of hexadecimal digits.
-
#initialize(link) ⇒ Image
constructor
Create a new instance of an Image.
-
#loaded? ⇒ Boolean
True if the image is loaded.
-
#raw_id ⇒ String
Image ID as a binary string.
-
#status ⇒ Symbol
Image error status.
-
#to_hex(id) ⇒ String
protected
Image id as a hexadecimal string.
-
#to_id(hex) ⇒ String
protected
Image id as a binary string.
-
#to_link ⇒ Hallon::Link
Link for the current object.
Methods included from Observable::Image
extended, initialize_callbacks, load_callback
Methods included from Loadable
Methods included from Linkable
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.
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
.sizes ⇒ Object
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.
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 |
#data ⇒ String
Returns 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 |
#format ⇒ Symbol
Returns image format, one of :jpeg
or :unknown
.
62 63 64 |
# File 'lib/hallon/image.rb', line 62 def format Spotify.image_format(pointer) end |
#from_link ⇒ Spotify::Link (private)
Returns 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 |
#id ⇒ String
Returns image ID as a sequence of hexadecimal digits.
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.
51 52 53 |
# File 'lib/hallon/image.rb', line 51 def loaded? Spotify.image_is_loaded(pointer) end |
#raw_id ⇒ String
Returns image ID as a binary string.
74 75 76 |
# File 'lib/hallon/image.rb', line 74 def raw_id Spotify.image_image_id(pointer) end |
#status ⇒ Symbol
Returns image error status.
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.
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.
113 114 115 |
# File 'lib/hallon/image.rb', line 113 def to_id(hex) [hex].pack('H40') end |
#to_link ⇒ Hallon::Link
Returns Link for the current object.
13 |
# File 'lib/hallon/image.rb', line 13 to_link :from_image |