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

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#add_alias(key, value, category) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/uric.rb', line 58

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



50
51
52
# File 'lib/uric.rb', line 50

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

#add_type_alias(key, value) ⇒ Object



54
55
56
# File 'lib/uric.rb', line 54

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

#dic_loadObject



84
85
86
# File 'lib/uric.rb', line 84

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

#dic_reloadObject



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

def dic_reload
  dic_save
  dic_load
end

#dic_saveObject



88
89
90
91
92
# File 'lib/uric.rb', line 88

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

#hostObject



23
24
25
26
27
28
29
# File 'lib/uric.rb', line 23

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



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

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

#remove_alias(key, category) ⇒ Object



77
78
79
80
81
82
# File 'lib/uric.rb', line 77

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

#remove_host_alias(key) ⇒ Object



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

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

#remove_type_alias(key) ⇒ Object



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

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

#titleObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/uric.rb', line 39

def title
  begin
    agent = Mechanize.new
    @title = agent.get(Addressable::URI.parse(@path).normalize).title
  rescue => e
    STDERR.puts e
  rescue Timeout::Error => e
    STDERR.puts e
  end
end

#typeObject



31
32
33
34
35
36
37
# File 'lib/uric.rb', line 31

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



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

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