Class: Twitterpunch::Poster

Inherits:
Object
  • Object
show all
Defined in:
lib/twitterpunch/poster.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Poster

Returns a new instance of Poster.



9
10
11
12
13
14
15
# File 'lib/twitterpunch/poster.rb', line 9

def initialize(config)
  @config = config
  @client = Twitter::REST::Client.new(config[:twitter])
  @sound  = @config[:sendsound] || "#{@config[:resources]}/tweet_sent.wav"
  @length = 113 - @config[:hashtag].length
  @queue  = Twitterpunch::Queue.new(config)
end

Instance Method Details

#chirpObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/twitterpunch/poster.rb', line 54

def chirp
  case RUBY_PLATFORM
  when /mingw|cygwin/
    begin
      require 'win32/sound'
      Win32::Sound.play(@sound)
    rescue LoadError
      puts 'gem install win32-sound to enable sounds.'
    end
  when /darwin/
    system("afplay #{@sound}")
  end
end

#post(files) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/twitterpunch/poster.rb', line 17

def post(files)
  files.each do |img|
    message = @queue.pop || @config[:messages].sample
    message = "#{message[0..@length]} ##{@config[:hashtag]}"

    resample(img) do |path|
      @client.update_with_media(message, File.new(path))
    end
    chirp()
  end
end

#resample(img) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/twitterpunch/poster.rb', line 29

def resample(img)
  path = File.expand_path(img)
  size = File.size?(path)
  max  = 3000000 # max size for twitter images

  if size < max
    yield path
  else
    # since filesize grows exponentially, this will be smaller than absolutely necessary.
    ratio   = Float(max) / Float(size)
    tmpfile = Tempfile.new('twitterpunch')

    image = Magick::Image.read(path).first
    image.resize!(ratio)
    image.write(tmpfile.path)

    yield tmpfile.path

    tmpfile.close
    tmpfile.unlink

    puts "Resized image to #{Integer(ratio * 100)}%."
  end
end