Class: ShowUrlInfoPlugin

Inherits:
CrazyDoll::Plugin show all
Defined in:
lib/crazy_doll/plugins/show_url_info_plugin.rb

Instance Attribute Summary

Attributes inherited from CrazyDoll::Plugin

#line, #opts, #params

Instance Method Summary collapse

Methods inherited from CrazyDoll::Plugin

#c, config, #config, #current_channel, #current_nick, #custom_messages, #custom_messages_chan, #custom_messages_priv, #get, #initialize, #join, #parse_message, #post, #register_events, #register_keys, #reply, #reply_to, #reply_to_a_channel?, #say, #talked_with_me?

Constructor Details

This class inherits a constructor from CrazyDoll::Plugin

Instance Method Details

#get_privmsgObject Also known as: get_chanmsg



9
10
11
12
13
14
# File 'lib/crazy_doll/plugins/show_url_info_plugin.rb', line 9

def get_privmsg
  return unless c.show_info
  for url in URI::extract(opts.message, ['http','https'])
    reply url_info(url), false
  end
end

#url_info(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/crazy_doll/plugins/show_url_info_plugin.rb', line 18

def url_info(url)
  resp = HTTParty::Request.new(Net::HTTP::Head, url)
  resp.perform
  lr = resp.last_response
  if lr['content-type'].include?('text/html')
    title = (Nokogiri::HTML(get(url))/'title').text
    title = 'No title' if title.empty?
    "[Site] #{title}"
  else
    file = File.basename(resp.uri.path).gsub(/\?.*/,'')
    file = 'unknown' if file.empty?
    "[file] #{file.inspect}, content-type: #{lr['content-type'].inspect}, content-length: #{lr['content-length'].to_i.to_human_readable_size}, " +
    "last-modified: #{lr['last-modified'].inspect}"
  end
end