Class: Crest::Photo

Inherits:
Object
  • Object
show all
Defined in:
lib/crest/photo.rb

Overview

The picture a card carries: bytes as they were handed over, or the file a path names.

Constant Summary collapse

SIGNATURES =

The first bytes of each image format a phone will read a saved card's picture from.

{ 'PNG' => "\x89PNG".b, 'JPEG' => "\xFF\xD8\xFF".b }
UNREADABLE =

What a host hears about a picture no phone would draw.

'crest: the photo is neither a PNG nor a JPEG, so the card goes out without it'

Instance Method Summary collapse

Constructor Details

#initialize(photo) ⇒ Photo

Returns a new instance of Photo.

Parameters:

  • photo (String, Pathname)

    image bytes, or the file to read them from.



11
12
13
14
15
# File 'lib/crest/photo.rb', line 11

def initialize(photo)
  @bytes = photo.respond_to?(:binread) ? photo.binread : photo
  @type = SIGNATURES.find { |_, signature| @bytes.b.start_with? signature }&.first
  warn UNREADABLE unless @type
end

Instance Method Details

#propertyString

Returns PHOTO property, or nothing where the bytes are in no format a phone reads.

Returns:

  • (String)

    PHOTO property, or nothing where the bytes are in no format a phone reads.



18
19
20
# File 'lib/crest/photo.rb', line 18

def property
  @property ||= "PHOTO;ENCODING=b;TYPE=#{@type}:#{[@bytes].pack 'm0'}" if @type
end