Class: TimestampMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/timestamp_maker.rb,
lib/timestamp_maker/handlers/ffmpeg.rb,
lib/timestamp_maker/handlers/image_magick.rb,
lib/timestamp_maker/mime_recognizers/marcel.rb,
lib/timestamp_maker/time_zone_lookupers/mock.rb,
lib/timestamp_maker/time_zone_lookupers/wheretz.rb,
lib/timestamp_maker/time_zone_lookupers/geo_name.rb

Defined Under Namespace

Modules: Handlers, MimeRecognizers, TimeZoneLookupers

Constant Summary collapse

COORDINATE_ORIGINS =
%w[
  top-left
  top-right
  bottom-left
  bottom-right
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_recognizer: MimeRecognizers::Marcel.new, handlers: [ Handlers::ImageMagick.new( time_zone_lookuper: TimeZoneLookupers::Wheretz.new ), Handlers::Ffmpeg.new( time_zone_lookuper: TimeZoneLookupers::Wheretz.new ) ]) ⇒ TimestampMaker

Returns a new instance of TimestampMaker.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/timestamp_maker.rb', line 25

def initialize(
  mime_recognizer: MimeRecognizers::Marcel.new,
  handlers: [
    Handlers::ImageMagick.new(
      time_zone_lookuper: TimeZoneLookupers::Wheretz.new
    ),
    Handlers::Ffmpeg.new(
      time_zone_lookuper: TimeZoneLookupers::Wheretz.new
    )
  ]
)
  @mime_recognizer = mime_recognizer
  @handlers = handlers
end

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



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

def handlers
  @handlers
end

#instance=(value) ⇒ Object (writeonly)

Sets the attribute instance

Parameters:

  • value

    the value to set the attribute instance to.



20
21
22
# File 'lib/timestamp_maker.rb', line 20

def instance=(value)
  @instance = value
end

#mime_recognizerObject

Returns the value of attribute mime_recognizer.



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

def mime_recognizer
  @mime_recognizer
end

Class Method Details

.instanceObject



21
22
23
# File 'lib/timestamp_maker.rb', line 21

def self.instance
  @instance ||= new
end

Instance Method Details

#add_timestamp(input_path, output_path, format: '%Y-%m-%d %H:%M:%S', time: nil, font_size: 32, font_family: 'Sans', font_color: 'white', background_color: '#000000B3', time_zone: nil, coordinate_origin: 'top-left', x: 32, y: 32, font_padding: 8) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/timestamp_maker.rb', line 40

def add_timestamp(
  input_path, output_path,
  format: '%Y-%m-%d %H:%M:%S',
  time: nil,
  font_size: 32,
  font_family: 'Sans',
  font_color: 'white',
  background_color: '#000000B3',
  time_zone: nil,
  coordinate_origin: 'top-left',
  x: 32,
  y: 32,
  font_padding: 8
)
  mime_type = mime_recognizer.recognize(input_path)
  handler = handlers.find { |i| i.accept?(mime_type) }
  raise "Unsupported MIME type: ##{mime_type}" if handler.nil?

  time = handler.creation_time(input_path) if time.nil?
  raise ArgumentError unless time.is_a?(Time)

  time.localtime(TZInfo::Timezone.get(time_zone)) unless time_zone.nil?

  unless COORDINATE_ORIGINS.include?(coordinate_origin)
    raise(
      ArgumentError,
      "coordinate origin should be one of #{COORDINATE_ORIGINS.join(',')}"
    )
  end

  handler.add_timestamp(
    input_path, output_path, time,
    format: format,
    font_size: font_size,
    font_family: font_family,
    font_color: font_color,
    background_color: background_color,
    coordinate_origin: coordinate_origin,
    x: x,
    y: y,
    font_padding: font_padding
  )
end