Class: Avaticon

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

Constant Summary collapse

BASE_PATH =
File.dirname(__FILE__)
DEFAULT_SITEINFO_PATH =
File.join(BASE_PATH, 'siteinfo.json')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Avaticon

Returns a new instance of Avaticon.



11
12
13
14
15
16
17
18
# File 'lib/avaticon.rb', line 11

def initialize opt = {}
  @siteinfo = []
  @tw_id = opt[:tw_id]
  @tw_pw = opt[:tw_pw]
  (opt[:siteinfo_path] || DEFAULT_SITEINFO_PATH).split(/\n/).each do |i|
    load_siteinfo i
  end
end

Instance Attribute Details

#siteinfoObject

Returns the value of attribute siteinfo.



9
10
11
# File 'lib/avaticon.rb', line 9

def siteinfo
  @siteinfo
end

Class Method Details

.get_html(url, opt = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/avaticon.rb', line 81

def self.get_html url, opt = {}
  open(url).read
#     if /twitter.com/ === url
#       Avaticon.get_tw_html url, opt
#     else
#       open(url).read
#     end
end

.get_tw_html(url, opt = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/avaticon.rb', line 90

def self.get_tw_html url, opt = {}
  require 'mechanize'
  agent = WWW::Mechanize.new
  agent.user_agent_alias = 'Mac Safari'
  page = agent.get'http://twitter.com/login'
   = page.forms.find { |i|
    i.action == 'https://twitter.com/sessions'
  }
  .fields.find { |i| i.name == 'session[username_or_email]'}.value = opt[:tw_id]
  .fields.find { |i| i.name == 'session[password]'}.value = opt[:tw_pw]
  .submit
  agent.get(url).body
end

.path2url(url, path) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/avaticon.rb', line 65

def self.path2url url, path
  #FIXME
  case path
  when /^http/
    path
  when /^\//
    u = URI.parse(url)
    u.path = path
    u.to_s
  else
    # url + path
    tmp = url.split('/')
    (tmp[0..(tmp.size - 2)] << path).join('/')
  end
end

Instance Method Details

#get_icon(service, user_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/avaticon.rb', line 40

def get_icon service, user_id
  info = @siteinfo.find { |i| i['service_name'] == service }
  if info
    url = info['iconPageUrl'].gsub('{user_id}', user_id)
    html = Avaticon.get_html(url, :tw_id => @tw_id, :tw_pw => @tw_pw)
    icon = Nokogiri::HTML(html).at(info['iconImageElement'])
    if icon
      Avaticon.path2url url, icon['src']
    end
  end
end

#load_siteinfo(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/avaticon.rb', line 20

def load_siteinfo path
  JSON.parse(open(path).read).each do |i|
    # 1.9 feature
    # index = @siteinfo.index { |j| j['service_name'] == i['service_name']}
    index = nil
    @siteinfo.each_with_index do |j, ind|
      if j['service_name'] == i['service_name']
        index = ind
        break
      end
    end

    if index
      @siteinfo[index] = i
    else
      @siteinfo.push i
    end
  end
end

#search_by_url(url) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/avaticon.rb', line 52

def search_by_url url
  @siteinfo.each do |i|
    m = Regexp.new(i['url']).match(url)
    if m
      return get_icon(i['service_name'], m.to_a[1])
    end
  end
end

#servicesObject



61
62
63
# File 'lib/avaticon.rb', line 61

def services
  @siteinfo.map{ |i| i['service_name'] }
end