Module: Codic

Defined in:
lib/codic.rb,
lib/codic/cli.rb,
lib/codic/version.rb

Defined Under Namespace

Modules: ContentCushion Classes: CLI

Constant Summary collapse

URL_ROOT =
"http://codic.jp"
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.display(result) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/codic.rb', line 54

def display(result)
  return unless result

  case result[:type]
  when "english"
    unless result[:flections] == ""
      puts result[:flections] 
      puts
    end
    puts "# 検索結果" unless result[:words].empty?
    result[:words].each do |w|
      class_ja = w[:class_ja].empty? ? "" : "#{w[:class_ja]}"
      puts "#{w[:no]}. #{w[:translated]} #{class_ja}" 
    end

    puts "\n# 関連" unless result[:relations].empty?
    result[:relations].each do |r|
      puts "#{r[:seltext]} #{r[:digest]} #{r[:description]}"
    end
  when "naming"
    result[:words].each do |w|
      puts "#{w[:translated]} [#{w[:class_ja]}]"
      unless w[:description] == ""
        puts "  #{w[:description]}" 
        puts 
      end
    end
  when "suggest"
    result[:suggests].each do |s|
      puts "#{s[:title]} #{s[:description]}"
    end
  else
    raise ArgumentError, "Invalid Type"
  end
end

.search(word) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/codic.rb', line 41

def search(word)
  raise ArgumentError, "word is empty" if word.gsub(/\s/, "") == ""
  @doc = Nokogiri::HTML(open(URI.encode("#{Codic::URL_ROOT}/search?q=#{word}")))
  return nil unless found?

  case entry_type 
  when "english"
    analyse_english
  when "naming"
    analyse_naming
  end
end

.suggest(word, dic) ⇒ Object



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

def suggest(word, dic)
  res =  JSON.parse(open(URI.encode("#{Codic::URL_ROOT}/suggest?q=#{word}&dic=#{dic}"),
                    "Accept" => "application/json, text/javascript, */*; q=0.01",
                    "X-Requested-With" => "XMLHttpRequest").read)
  res["titles"].map! { |t| t.gsub(/\<\/?mark\>/, '') }
  suggests = res["titles"].zip(res["descriptions"], res["urls"]).map do |t, d, u|
    {
      title: t,
      description: d,
      url: u
    }
  end

  {
    type: "suggest",
    suggests: suggests
  }
end