codic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codic.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keisuke KITA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # Codic
2
+
3
+ Simple access webservice "codic"
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'codic'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install codic
18
+
19
+ ## Usage
20
+
21
+ execute as command
22
+ ```
23
+ codic something_word
24
+ ```
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'codic'
4
+
5
+ module Codic
6
+ class << self
7
+ def search_or_suggest(word)
8
+ begin
9
+ Codic.display(Codic.search(word))
10
+ rescue CodicError
11
+ Codic.display(Codic.suggest(word, suggest_dic(word)))
12
+ end
13
+ end
14
+
15
+ def suggest_dic(word)
16
+ if /\A[a-zA-Z].*\Z/o =~ word
17
+ :english
18
+ else
19
+ :naming
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ if ARGV.empty?
26
+ while word = $STDIN.gets
27
+ Codic.search_or_suggest(word.chomp)
28
+ puts "-"*60
29
+ end
30
+ else
31
+ Codic.search_or_suggest(ARGV[0])
32
+ end
33
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "codic"
8
+ gem.version = Codic::VERSION
9
+ gem.authors = ["Keisuke KITA"]
10
+ gem.email = ["kei.kita2501@gmail.com"]
11
+ gem.description = %q{simple access codic via terminal}
12
+ gem.summary = %q{simple access codinc via terminal}
13
+ gem.homepage = "https://github.com/kitak/codic"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'nokogiri'
21
+ end
@@ -0,0 +1,134 @@
1
+ # coding: utf-8
2
+ require "codic/version"
3
+ require "uri"
4
+ require "open-uri"
5
+ require "nokogiri"
6
+ require "json"
7
+
8
+
9
+ module Codic
10
+ URL_ROOT = "http://codic.jp"
11
+
12
+ class CodicError < StandardError; end
13
+
14
+ module ContentCushion
15
+ def content
16
+ ""
17
+ end
18
+ end
19
+ class ::NilClass
20
+ include Codic::ContentCushion
21
+ end
22
+
23
+ class << self
24
+ def suggest(word, dic)
25
+ res = JSON.parse(open(URI.encode("#{Codic::URL_ROOT}/suggest?q=#{word}&dic=#{dic}"),
26
+ "Accept" => "application/json, text/javascript, */*; q=0.01",
27
+ "X-Requested-With" => "XMLHttpRequest").read)
28
+ res["titles"].map! { |t| t.gsub(/\<\/?mark\>/, '') }
29
+ suggests = res["titles"].zip(res["descriptions"], res["urls"]).map do |t, d, u|
30
+ {
31
+ title: t,
32
+ description: d,
33
+ url: u
34
+ }
35
+ end
36
+
37
+ {
38
+ type: "suggest",
39
+ suggests: suggests
40
+ }
41
+ end
42
+
43
+ def search(word)
44
+ raise ArgumentError, "word is empty" if word.gsub(/\s/, "") == ""
45
+ @doc = Nokogiri::HTML(open(URI.encode("#{Codic::URL_ROOT}/search?q=#{word}")))
46
+ raise CodicError, "Not Found" unless found?
47
+
48
+ case entry_type
49
+ when "english"
50
+ analyse_english
51
+ when "naming"
52
+ analyse_naming
53
+ end
54
+ end
55
+
56
+ def display(result)
57
+ case result[:type]
58
+ when "english"
59
+ unless result[:flections] == ""
60
+ puts result[:flections]
61
+ puts
62
+ end
63
+ result[:words].each do |w|
64
+ puts "#{w[:no]}. #{w[:translated]} [#{w[:class_ja]}]"
65
+ end
66
+ when "naming"
67
+ result[:words].each do |w|
68
+ puts "#{w[:translated]} [#{w[:class_ja]}]"
69
+ unless w[:description] == ""
70
+ puts " #{w[:description]}"
71
+ puts
72
+ end
73
+ end
74
+ when "suggest"
75
+ result[:suggests].each do |s|
76
+ puts "#{s[:title]} #{s[:description]}"
77
+ end
78
+ else
79
+ raise ArgumentError, "Invalid Type"
80
+ end
81
+ end
82
+
83
+ private
84
+ def found?
85
+ @doc.css('.not-found').empty?
86
+ end
87
+
88
+ def entry_type
89
+ @doc.at('.entry')['class'].split(' ')[1]
90
+ end
91
+
92
+ def analyse_english
93
+ flections = @doc.at('#grammar .flections').content
94
+ words = @doc.css('#translations .translation').map do |li|
95
+ {
96
+ no: li.at('.no').content[0..-2].to_i,
97
+ class_ja: li.at('.word-class')['title'],
98
+ class_en: li.at('.word-class')['class'].split(' ')[-1],
99
+ translated: li.at('.translated').content
100
+ }
101
+ end
102
+
103
+ {
104
+ type: "english",
105
+ flections: flections,
106
+ words: words
107
+ }
108
+ end
109
+
110
+ def analyse_naming
111
+ words = @doc.css('#translations .translation').map do |article|
112
+ w = article.at('.translated').content
113
+ {
114
+ translated: w,
115
+ class_ja: article.at('.word-class').content,
116
+ description: article.at('.description').content,
117
+ url: "#{Codic::URL_ROOT}/entries/#{w}"
118
+ }
119
+ end
120
+
121
+ {
122
+ type: "naming",
123
+ words: words
124
+ }
125
+ end
126
+ end
127
+ end
128
+
129
+ if __FILE__ == $0
130
+ puts Codic.search("access")
131
+ puts Codic.search("検証する")
132
+ puts Codic.suggest("検証す", "naming")
133
+ puts Codic.suggest("red", "english")
134
+ end
@@ -0,0 +1,3 @@
1
+ module Codic
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keisuke KITA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: simple access codic via terminal
31
+ email:
32
+ - kei.kita2501@gmail.com
33
+ executables:
34
+ - codic
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/codic
44
+ - codic.gemspec
45
+ - lib/codic.rb
46
+ - lib/codic/version.rb
47
+ homepage: https://github.com/kitak/codic
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: simple access codinc via terminal
71
+ test_files: []
72
+ has_rdoc: