Class: Atig::OFilter::ShortUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/atig/ofilter/short_url.rb

Constant Summary collapse

MIN_LEN =
20

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ShortUrl

Returns a new instance of ShortUrl.



10
11
12
13
14
# File 'lib/atig/ofilter/short_url.rb', line 10

def initialize(context)
  @log  = context.log
  @opts = context.opts
  @http = Atig::Http.new @log
end

Instance Method Details

#call(status) ⇒ Object



16
17
18
19
# File 'lib/atig/ofilter/short_url.rb', line 16

def call(status)
  mesg = status[:status]
  status.merge(:status => short_urls(mesg))
end

#short_urls(mesg) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/atig/ofilter/short_url.rb', line 21

def short_urls(mesg)
  shorten = case
            when @opts.bitlify.to_s.include?(":")
              , key, len = @opts.bitlify.to_s.split(":", 3)
              @len = (len || MIN_LEN).to_i
              Bitly. @log, , key
            when @opts.bitlify
              @len = (@opts.bitlify.to_s || MIN_LEN).to_i
              Bitly. @log
            else
              return mesg
            end
  mesg.gsub(URI.regexp(%w[http https])) do|url|
    if URI.rstrip(url).size < @len then
      url
    else
      shorten.shorten url
    end
  end
end