Permalink
Browse files

Initial Import

  • Loading branch information...
0 parents commit 8dfab92f18be7d608285954bc417f93d940c8449 chatgris committed Jul 26, 2011
1 .rvmrc
@@ -0,0 +1 @@
+rvm ruby-1.9.2@c3po --create
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
@@ -0,0 +1,31 @@
+PATH
+ remote: .
+ specs:
+ c3po (0.1.0)
+ typhoeus (~> 0.2.4)
+ yajl-ruby (~> 0.8.2)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.2)
+ mime-types (1.16)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ typhoeus (0.2.4)
+ mime-types
+ mime-types
+ yajl-ruby (0.8.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ c3po!
+ rspec (~> 2.6)
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 af83
+
+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,68 @@
+C3PO
+====
+
+Ruby translator client. Actual provider :
+
+ * Google Translate
+ * Bing Translate
+
+Ruby 1.9.2 is strongly recommanded.
+
+
+Dependency
+----------
+
+`libcurl4-gnutls-dev`
+
+
+Installation
+------------
+
+Install it with rubygems:
+
+``` ruby
+gem install c3po
+```
+
+With bundler, add it to your `Gemfile`:
+
+``` ruby
+gem "c3po", "~>0.1"
+```
+
+
+Usage
+-----
+
+``` ruby
+# Define a provider
+C3po.configure do |config|
+ config.provider = :google
+ config.google_api_key = "MYAPIKEY"
+end
+
+# Define several provider
+C3po.configure do |config|
+ config.provider = [:google, :bing]
+ config.google_api_key = "MYAPIKEY"
+ config.bing_api_key = "MYAPIKEY"
+end
+
+# Let's translate
+translator = C3po.new('I want this droid to be translated')
+translator.translate(:en, :fr) # =>
+translator.is # => :en
+translator.is?(:de) # => false
+```
+
+Todo
+----
+
+* Bing adaptor
+* Exceptions
+* More specs
+
+Copyright
+---------
+
+See LICENSE for further details.
@@ -0,0 +1,17 @@
+Gem::Specification.new do |s|
+ s.name = "c3po"
+ s.version = "0.1.0"
+ s.date = Time.now.utc.strftime("%Y-%m-%d")
+ s.homepage = "http://github.com/chatgris/c3po"
+ s.authors = "chatgris"
+ s.email = "jboyer@af83.com"
+ s.description = "Ruby translation client"
+ s.summary = "Ruby translation client"
+ s.extra_rdoc_files = %w(README.md)
+ s.files = Dir["LICENSE", "README.md", "Gemfile", "lib/**/*.rb", "init.rb"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.8.3}
+ s.add_development_dependency "rspec", "~>2.6"
+ s.add_dependency "typhoeus", "~>0.2.4"
+ s.add_dependency "yajl-ruby", "~>0.8.2"
+end
@@ -0,0 +1,2 @@
+# encoding: utf-8
+require "c3po"
@@ -0,0 +1,74 @@
+# encoding: utf-8
+require 'typhoeus'
+require 'yajl'
+
+class C3po
+
+ autoload :Translator, 'c3po/translator'
+ autoload :Client, 'c3po/client'
+
+ module Translator
+ autoload :Configuration, 'c3po/translator/configuration'
+ autoload :Google, 'c3po/translator/google'
+ autoload :Bing, 'c3po/translator/bing'
+ end
+
+ include Client
+ include C3po::Translator
+
+ attr_accessor :base, :to_be_translated, :errors, :result
+
+ # Define a translator object.
+ #
+ # @example :
+ # translator = C3po.new('to be translated')
+ # translator.translate(:fr, :en)
+ #
+ # @param [ String ] string String on witch we will work
+ #
+ # @since 0.0.1
+ #
+ def initialize(to_be_translated = nil)
+ select_provider
+ @to_be_translated = to_be_translated
+ @base = BASE_URL
+ @errors = []
+ end
+
+ class << self
+
+ # Define a configure block.
+ #
+ # Delegates to C3po::Translator::Configuration
+ #
+ # @example Define the option.
+ # C3po.configure do |config|
+ # config.provider = :google
+ # end
+ #
+ # @param [ Proc ] block The block getting called.
+ #
+ # @since 0.0.1
+ #
+ def configure(&block)
+ C3po::Translator::Configuration.configure(&block)
+ end
+
+ end # self
+
+ private
+
+ # Choose provider based on configuration
+ #
+ # @since 0.0.1
+ #
+ def select_provider
+ case C3po::Translator::Configuration.provider
+ when :google
+ self.class.send :include, Google
+ when :bing
+ self.class.send :include, Bing
+ end
+ end
+
+end # C3po
@@ -0,0 +1,49 @@
+# encoding: utf-8
+class C3po
+ module Client
+
+ attr_accessor :request
+
+ # Query a web service.
+ #
+ # Returns a translated string or an array of errors.
+ #
+ # @exemple :
+ # fetch {:key => 'MYAPIKEY',
+ # :q => 'Something to translate'
+ # }
+ #
+ # @params [Hash] Query containing all the params.
+ #
+ # @return [String] Translated string.
+ # [Array] Errors.
+ #
+ # @since 0.0.1
+ #
+ def fetch(query)
+ request = Typhoeus::Request.new @base, :params => query
+
+ request.on_complete do |response|
+ if response.success?
+ begin
+ @result = parse response.body
+ rescue
+ @errors << 'Invalid response'
+ end
+ elsif response.timed_out?
+ @errors << 'timeout'
+ elsif response.code == 0
+ @errors << response.curl_error_message
+ else
+ @errors << "HTTP request failed: #{response.code}"
+ end
+ end
+
+ hydra = Typhoeus::Hydra.hydra
+ hydra.queue request
+ hydra.run
+ @errors.empty? ? @result : @errors
+ end
+
+ end # Client
+end # C3po
@@ -0,0 +1,52 @@
+# encoding: utf-8
+class C3po
+ module Translator
+
+ # Translate a string.
+ #
+ # @example :
+ # translator = C3po.new('translate')
+ # translator.translate(:en, :fr)
+ #
+ # @param [ Symbol ] language to translate from
+ # @param [ Symbol ] language to translate to
+ #
+ # @return [String] the translated string
+ #
+ # @since 0.0.1
+ #
+ def translate(from, to)
+ fetch build_query(from, to)
+ end
+
+ # Identify language.
+ #
+ # @example :
+ # translator = C3po.new('translate')
+ # translator.is
+ #
+ # @return [Symbol] the identified language
+ #
+ # @since 0.0.1
+ #
+ def is
+ end
+
+ # Check language.
+ #
+ # @example :
+ # translator = C3po.new('translate')
+ # translator.is? :fr
+ #
+ # @param [ Symbol ] language to check
+ #
+ # @return [Boolean]
+ #
+ # @since 0.0.1
+ #
+ def is?(language)
+ language == is
+ end
+
+ end #Translator
+end #C3po
@@ -0,0 +1,8 @@
+# encoding: utf-8
+class C3po
+ module Translator
+ $stderr.puts 'hum'
+ module Bing
+ end #Bing
+ end # Translator
+end #C3po
@@ -0,0 +1,29 @@
+# encoding: utf-8
+class C3po
+ module Translator
+
+ class Configuration
+
+ class << self
+
+ attr_accessor :provider, :google_api_key
+
+ # Define a configure block.
+ #
+ # @example Define the option.
+ # C3po.configure do |config|
+ # config.provider = :google
+ # end
+ #
+ # @param [ Proc ] block The block getting called.
+ #
+ # @since 0.0.1
+ #
+ def configure(&block)
+ yield self
+ end
+
+ end # self
+ end # Configuration
+ end # Translator
+end #C3po
Oops, something went wrong.

0 comments on commit 8dfab92

Please sign in to comment.