uric 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +44 -0
  3. data/lib/aliases.yml +9 -0
  4. data/lib/uric.rb +100 -0
  5. metadata +131 -0
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 rynqs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = uric
2
+
3
+ Parse host, file-type, title from URI and attach an alias to host, file-type
4
+
5
+ == Usage
6
+ require 'uric'
7
+
8
+ obj = Uric::URI.new('http://www.example.com/hoge.pdf')
9
+
10
+ p obj.host #=> "www.example.com"
11
+
12
+ p obj.type #=> "application/pdf"
13
+
14
+ obj.add_host_alias('www.example.com', 'Example')
15
+
16
+ obj.add_type_alias('application/pdf', 'PDF file')
17
+
18
+ p obj.host #=> "Example"
19
+
20
+ p obj.type #=> "PDF file"
21
+
22
+ obj.path = 'https://github.com/'
23
+
24
+ p obj.title #=> "GitHub - Social Coding"
25
+
26
+ p obj.host #=> "github.com"
27
+
28
+ p obj.type #=> ""
29
+
30
+ == Contributing to uric
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
34
+ * Fork the project
35
+ * Start a feature/bugfix branch
36
+ * Commit and push until you are happy with your contribution
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2011 rynqs. See LICENSE.txt for
43
+ further details.
44
+
data/lib/aliases.yml ADDED
@@ -0,0 +1,9 @@
1
+ ---
2
+ hosts:
3
+ www.google.com: Google
4
+ www.mhlw.go.jp: MHLW
5
+ manuals.info.apple.com: Apple
6
+ types:
7
+ text/html: HTML
8
+ application/pdf: PDF
9
+ text/xml: XML
data/lib/uric.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'addressable/uri'
2
+ require 'mime/types'
3
+ require 'yaml'
4
+ require 'mechanize'
5
+
6
+ module Uric
7
+ class URI
8
+ attr_accessor :path, :dic
9
+ @@dic = []
10
+ def initialize(uri='')
11
+ @path = uri
12
+ dic_load
13
+ end
14
+
15
+ def host_origin
16
+ Addressable::URI.parse(@path).host.to_s
17
+ end
18
+
19
+ def type_origin
20
+ MIME::Types.type_for(@path)[0].to_s
21
+ end
22
+
23
+ def host
24
+ if @dic['hosts'].has_key?(self.host_origin)
25
+ @dic['hosts'][self.host_origin].to_s
26
+ else
27
+ self.host_origin
28
+ end
29
+ end
30
+
31
+ def type
32
+ if @dic['types'].has_key?(self.type_origin)
33
+ @dic['types'][self.type_origin].to_s
34
+ else
35
+ self.type_origin
36
+ end
37
+ end
38
+
39
+ def title
40
+ begin
41
+ agent = Mechanize.new
42
+ @title = agent.get(Addressable::URI.parse(@path).normalize).title
43
+ rescue => e
44
+ STDERR.puts e
45
+ rescue Timeout::Error => e
46
+ STDERR.puts e
47
+ end
48
+ end
49
+
50
+ def add_host_alias(key, value)
51
+ add_alias(key, value, 'hosts')
52
+ end
53
+
54
+ def add_type_alias(key, value)
55
+ add_alias(key, value, 'types')
56
+ end
57
+
58
+ def add_alias(key, value, category)
59
+ unless @dic[category].has_key?(key)
60
+ @dic[category].store(key, value)
61
+ dic_reload
62
+ else
63
+ @dic[category][key] = value
64
+ end
65
+ dic_reload
66
+ @dic[category][key]
67
+ end
68
+
69
+ def remove_host_alias(key)
70
+ remove_alias(key, 'hosts')
71
+ end
72
+
73
+ def remove_type_alias(key)
74
+ remove_alias(key, 'types')
75
+ end
76
+
77
+ def remove_alias(key, category)
78
+ if @dic[category].has_key?(key)
79
+ @dic[category].delete(key)
80
+ dic_reload
81
+ end
82
+ end
83
+
84
+ def dic_load
85
+ @dic = YAML.load_file(File.join(File.dirname(__FILE__), 'aliases.yml'))
86
+ end
87
+
88
+ def dic_save
89
+ open(File.join(File.dirname(__FILE__), 'aliases.yml'), 'w') do |f|
90
+ f.write(YAML.dump(@dic))
91
+ end
92
+ end
93
+
94
+ def dic_reload
95
+ dic_save
96
+ dic_load
97
+ end
98
+ end
99
+ end
100
+
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uric
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - rynqs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-27 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: &70135344367520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70135344367520
25
+ - !ruby/object:Gem::Dependency
26
+ name: mime-types
27
+ requirement: &70135344365880 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70135344365880
36
+ - !ruby/object:Gem::Dependency
37
+ name: mechanize
38
+ requirement: &70135344364820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70135344364820
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda
49
+ requirement: &70135344349560 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70135344349560
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &70135344348560 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.0.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70135344348560
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &70135344347180 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.6.4
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70135344347180
80
+ - !ruby/object:Gem::Dependency
81
+ name: rcov
82
+ requirement: &70135344345720 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70135344345720
91
+ description: Parse host, file-type, title from URI and attach an alias to host, file-type
92
+ email: cecilcat@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - lib/aliases.yml
100
+ - lib/uric.rb
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ homepage: http://github.com/rynqs/uric
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 1351409818923746902
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.10
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: altanative URI Parser
131
+ test_files: []