Permalink
Browse files

initial

  • Loading branch information...
0 parents commit e56022b2be6e6631caa22b88c7b2531f64416942 prikha committed Sep 28, 2012
Showing with 159 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 Gemfile
  3. +20 −0 LICENCE
  4. +3 −0 README.md
  5. +6 −0 Rakefile
  6. +23 −0 dickens.gemspec
  7. +82 −0 lib/dickens.rb
  8. +3 −0 lib/dickens/version.rb
  9. +12 −0 spec/dickens/dickens_spec.rb
  10. +1 −0 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+.idea/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in dickens.gemspec
+gemspec
20 LICENCE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Sergey Prikhodko
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+#Dickens
+## is for dictionaries
+TODO: write some readme
@@ -0,0 +1,6 @@
+require "rake"
+require "bundler/gem_tasks"
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new(:spec)
+task default: :spec
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "dickens/version"
+
+Gem::Specification.new do |s|
+ s.name = "dickens"
+ s.version = Dickens::VERSION
+ s.authors = ["prikha"]
+ s.email = ["sergey@zengile.com"]
+ s.homepage = "http://github.com/prikha/dickens"
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "dickens"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "rspec"
+
+end
@@ -0,0 +1,82 @@
+require "dickens/version"
+
+require 'rbconfig'
+require RbConfig::CONFIG['target_os'] == 'mingw32' && !(RUBY_VERSION =~ /1.9/) ? 'win32/open3' : 'open3'
+
+require 'active_support/core_ext/class/attribute_accessors'
+
+begin
+ require 'active_support/core_ext/object/blank'
+rescue LoadError
+ require 'active_support/core_ext/blank'
+end
+
+module Dickens
+ class StarDict
+ @@executable = "sdcv"
+ @@config = {
+ :use_dict => false,
+ :utf8_input => true,
+ :utf8_output => true,
+ :non_interactive => true,
+ :data_dir=>false
+ }
+ cattr_accessor :config, :executable
+
+
+ #Использование:
+ # sdcv [ПАРАМЕТР...] words
+ #
+ #Параметры приложения:
+ #-v, --version display version information and exit
+ #-l, --list-dicts display list of available dictionaries and exit
+ #-u, --use-dict=bookname for search use only dictionary with this bookname
+ #-n, --non-interactive for use in scripts
+ #-0, --utf8-output output must be in utf8
+ #-1, --utf8-input input of sdcv in utf8
+ #-2, --data-dir=path/to/dir use this directory as path to stardict data directory
+
+ class << self
+ def find(word)
+ word.to_s
+ invoke(word)
+ end
+
+ protected
+
+ def invoke(word)
+ command = [@@executable, prepare_options, word].join(" ")
+ stdin, stdout, stderr = Open3.popen3(command)
+ return stdout.readlines.join
+ end
+
+ def prepare_options
+ raise WrongFormatError unless @@config.is_a? Hash
+ options = normalize_options(@@config)
+ options.flatten
+ end
+
+ def normalize_options(options)
+ normalized_options = {}
+ options.each do |key, value|
+ next unless value
+ normalized_key = "--#{normalize_arg key}"
+ normalized_options[normalized_key] = normalize_value(value)
+ end
+ normalized_options
+ end
+
+ def normalize_arg(arg)
+ arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
+ end
+
+ def normalize_value(value)
+ value.is_a?(TrueClass) ? nil : value.to_s
+ end
+
+ end
+
+
+
+ end
+end
@@ -0,0 +1,3 @@
+module Dickens
+ VERSION = "0.0.1alpha"
+end
@@ -0,0 +1,12 @@
+#coding:utf-8
+require 'spec_helper'
+
+describe Dickens::StarDict do
+ it "should get the result" do
+ Dickens::StarDict.find("петля").should_not be_nil
+ end
+
+ it "should list the dictionaries" do
+ Dickens::StarDict.list.should be_a(Array)
+ end
+end
@@ -0,0 +1 @@
+require 'dickens'

0 comments on commit e56022b

Please sign in to comment.