Class: Uric::URI

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

Constant Summary collapse

@@dic =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = '') ⇒ URI

Returns a new instance of URI.



10
11
12
13
# File 'lib/uric.rb', line 10

def initialize(uri='')
  @path = uri
  dic_load
end

Instance Attribute Details

#dicObject

Returns the value of attribute dic.



8
9
10
# File 'lib/uric.rb', line 8

def dic
  @dic
end

Instance Method Details

#add_alias(key, value, category) ⇒ Object



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

def add_alias(key, value, category)
  unless @dic[category].has_key?(key)
    @dic[category].store(key, value)
    dic_reload
  else
    @dic[category][key] = value
  end
  dic_reload
  @dic[category][key]
end

#add_host_alias(key, value) ⇒ Object



72
73
74
# File 'lib/uric.rb', line 72

def add_host_alias(key, value)
  add_alias(key, value, 'hosts')
end

#add_type_alias(key, value) ⇒ Object



76
77
78
# File 'lib/uric.rb', line 76

def add_type_alias(key, value)
  add_alias(key, value, 'types')
end

#dic_loadObject



106
107
108
# File 'lib/uric.rb', line 106

def dic_load
  @dic = YAML.load_file(File.join(File.dirname(__FILE__), 'aliases.yml'))
end

#dic_reloadObject



116
117
118
119
# File 'lib/uric.rb', line 116

def dic_reload
  dic_save
  dic_load
end

#dic_saveObject



110
111
112
113
114
# File 'lib/uric.rb', line 110

def dic_save
  open(File.join(File.dirname(__FILE__), 'aliases.yml'), 'w') do |f|
    f.write(YAML.dump(@dic))
  end
end

#file_nameObject



68
69
70
# File 'lib/uric.rb', line 68

def file_name
  File.basename(self.path)
end

#hostObject



36
37
38
39
40
41
42
# File 'lib/uric.rb', line 36

def host
  if @dic['hosts'].has_key?(self.host_origin)
    @dic['hosts'][self.host_origin].to_s 
  else 
    self.host_origin 
  end
end

#host_originObject



28
29
30
# File 'lib/uric.rb', line 28

def host_origin 
  Addressable::URI.parse(self.path).host.to_s
end

#pathObject



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

def path
  agent = Mechanize.new
  agent.get(Addressable::URI.parse(self.path_origin).normalize).uri.to_s
end

#path=(uri) ⇒ Object



24
25
26
# File 'lib/uric.rb', line 24

def path=(uri)
  @path = uri
end

#path_originObject



15
16
17
# File 'lib/uric.rb', line 15

def path_origin
  @path
end

#remove_alias(key, category) ⇒ Object



99
100
101
102
103
104
# File 'lib/uric.rb', line 99

def remove_alias(key, category)
  if @dic[category].has_key?(key)
    @dic[category].delete(key)
    dic_reload
  end
end

#remove_host_alias(key) ⇒ Object



91
92
93
# File 'lib/uric.rb', line 91

def remove_host_alias(key)
  remove_alias(key, 'hosts')
end

#remove_type_alias(key) ⇒ Object



95
96
97
# File 'lib/uric.rb', line 95

def remove_type_alias(key)
  remove_alias(key, 'types')
end

#titleObject



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

def title
  begin
    agent = Mechanize.new
    page = agent.get(Addressable::URI.parse(self.path).normalize)
    if page.respond_to?(:title)
      @title = page.title.to_s
    else
      @title = self.file_name
    end
  rescue => e
    STDERR.puts e
  rescue Timeout::Error => e
    STDERR.puts e
  end
end

#typeObject



44
45
46
47
48
49
50
# File 'lib/uric.rb', line 44

def type
  if @dic['types'].has_key?(self.type_origin)
    @dic['types'][self.type_origin].to_s
  else
    self.type_origin
  end
end

#type_originObject



32
33
34
# File 'lib/uric.rb', line 32

def type_origin
  MIME::Types.type_for(self.path)[0].to_s
end