Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
prikha committed Sep 28, 2012
0 parents commit e56022b
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.gem
.bundle
Gemfile.lock
pkg/*
.idea/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in dickens.gemspec
gemspec
20 changes: 20 additions & 0 deletions 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.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
#Dickens
## is for dictionaries
TODO: write some readme
6 changes: 6 additions & 0 deletions Rakefile
@@ -0,0 +1,6 @@
require "rake"
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)
task default: :spec
23 changes: 23 additions & 0 deletions dickens.gemspec
@@ -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
82 changes: 82 additions & 0 deletions lib/dickens.rb
@@ -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
3 changes: 3 additions & 0 deletions lib/dickens/version.rb
@@ -0,0 +1,3 @@
module Dickens
VERSION = "0.0.1alpha"
end
12 changes: 12 additions & 0 deletions spec/dickens/dickens_spec.rb
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'dickens'

0 comments on commit e56022b

Please sign in to comment.