mymemory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mymemory.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 andrea longhi
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Mymemory
2
+
3
+ A (very) basic wrapper on mymemory.com translation resful API. See the
4
+ [mymemory website](http://mymemory.translated.net/doc/spec.php) for usage
5
+ constraints.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+ ```bash
12
+ gem 'mymemory'
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+ ```bash
22
+ gem install mymemory
23
+ ```
24
+
25
+
26
+ ## Usage
27
+ ```ruby
28
+ text = 'a rose for Emily'
29
+ Mymemory.translate(text, :from => :en, :to => :it)
30
+ # => 'una rosa per Emily'
31
+ ```
32
+ The ```from => :en``` key is optional as long as the original language is english.
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task :default => :spec
data/lib/mymemory.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'open-uri'
2
+ require 'mymemory/version'
3
+ require 'mymemory/translation'
4
+
5
+ module Mymemory
6
+ class LanguageMissingError < Exception
7
+ def message
8
+ 'please provide a target language value'
9
+ end
10
+ end
11
+
12
+ # Mymemory.translate(text, :from => :en, :to => :it)
13
+ def self.translate(text, opts)
14
+ translation = Translation.new(text, opts)
15
+ translation.translated_text
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'json'
2
+ require 'httparty'
3
+
4
+ module Mymemory
5
+ class Translation
6
+ BASE_URL = 'http://mymemory.translated.net/api/get'
7
+
8
+ attr_accessor :text, :from, :to, :response_json
9
+
10
+ def initialize(text, opts)
11
+ @text = text
12
+ @from = opts.fetch(:from, :en)
13
+ @to = opts[:to] or raise LanguageMissingError.new
14
+ end
15
+
16
+ def url
17
+ URI.escape("#{BASE_URL}?q=#{text}&langpair=#{lang_pair}")
18
+ end
19
+
20
+ def lang_pair
21
+ [from, to].join('|')
22
+ end
23
+
24
+ def translated_text
25
+ parsed_response['responseData']['translatedText']
26
+ end
27
+
28
+ def parsed_response
29
+ response.code == 200 ? JSON.parse(response.body) : {}
30
+ end
31
+
32
+ private
33
+
34
+ def response
35
+ @response ||= HTTParty.get(url)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Mymemory
2
+ VERSION = "0.0.1"
3
+ end
data/mymemory.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mymemory/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["andrea longhi"]
6
+ gem.email = ["andrea@spaghetticode.it"]
7
+ gem.description = %q{A (very) basic wrapper on mymemory.com translation resful API}
8
+ gem.summary = %q{A (very) basic wrapper on mymemory.com translation resful API}
9
+ gem.homepage = "https://github.com/spaghetticode/mymemory"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "mymemory"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Mymemory::VERSION
17
+
18
+ gem.add_dependency 'rake'
19
+ gem.add_dependency 'httparty'
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'guard-rspec'
22
+ gem.add_development_dependency 'vcr'
23
+ gem.add_development_dependency 'fakeweb'
24
+ end
@@ -0,0 +1,80 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://mymemory.translated.net/api/get?q=Wasn't%20born%20to%20Follow&langpair=en%7Cit
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ connection:
11
+ - close
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ date:
18
+ - Thu, 22 Nov 2012 09:38:27 GMT
19
+ server:
20
+ - Apache/2.2.16 (Debian) PHP/5.4.5-1~dotdeb.0 mod_python/3.3.1 Python/2.6.6
21
+ mod_ssl/2.2.16 OpenSSL/0.9.8o
22
+ x-powered-by:
23
+ - PHP/5.4.5-1~dotdeb.0
24
+ cache-control:
25
+ - no-cache, no-store, max-age=0, must-revalidate
26
+ pragma:
27
+ - no-cache
28
+ expires:
29
+ - Fri, 01 Jan 1990 00:00:00 GMT
30
+ x-backend-content-length:
31
+ - '10'
32
+ x-embedded-status:
33
+ - '200'
34
+ x-frame-options:
35
+ - SAMEORIGIN
36
+ x-xss-protection:
37
+ - '0'
38
+ access-control-allow-origin:
39
+ - ''
40
+ vary:
41
+ - User-Agent,Accept-Encoding
42
+ content-length:
43
+ - '3545'
44
+ connection:
45
+ - close
46
+ content-type:
47
+ - application/json; charset=utf-8
48
+ body:
49
+ encoding: US-ASCII
50
+ string: ! '{"responseData":{"translatedText":"Non era nato per seguire"},"responseDetails":"","responseStatus":200,"matches":[{"id":"0","segment":"Wasn''t
51
+ born to Follow","translation":"Non era nato per seguire","quality":"70","reference":"Machine
52
+ Translation provided by Google, Microsoft, Worldlingo or MyMemory customized
53
+ engine.","usage-count":1,"subject":"All","created-by":"MT!","last-updated-by":null,"create-date":"2012-11-22","last-update-date":"2012-11-22","match":0.85},{"id":"5593788","segment":"SIGNATURES
54
+ FOLLOW","translation":"SEGUONO FIRME","quality":"0","reference":"","usage-count":1,"subject":"Science","created-by":"rprosser","last-updated-by":null,"create-date":"2006-11-27
55
+ 16:19:25","last-update-date":"2006-11-27 16:19:25","match":0.37},{"id":"260745485","segment":"Follow
56
+ use instructions on the label.","translation":"Seguire le istruzioni sull\u2019etichetta.","quality":"74","reference":"","usage-count":1,"subject":"Marketing","created-by":"curro","last-updated-by":null,"create-date":"2006-05-16
57
+ 16:18:51","last-update-date":"2006-05-16 16:18:51","match":0.25},{"id":"260746461","segment":"Follow
58
+ with Firming Facial Peel Off Mask.","translation":"Quindi applicare la maschera
59
+ facciale rassodante.","quality":"0","reference":"","usage-count":1,"subject":"Marketing","created-by":"curro","last-updated-by":null,"create-date":"2008-01-15
60
+ 19:09:45","last-update-date":"2008-01-15 19:09:45","match":0.22},{"id":"233444728","segment":"One
61
+ morning she woke up and her notebook wasn''t working.","translation":"Una
62
+ mattina si sveglia e il suo notebook non si accende.","quality":"0","reference":"","usage-count":2,"subject":"Computer_Science","created-by":"","last-updated-by":null,"create-date":"2009-02-16
63
+ 14:22:01","last-update-date":"2009-02-16 14:22:01","match":0.18},{"id":"428932346","segment":"I
64
+ wanted to get out on the tenth floor. It wasn '' t on.","translation":"Volevo
65
+ scendere al decimo piano: non ci sono riuscita.","quality":"","reference":"http:\/\/www.europarl.europa.eu\/|@|http:\/\/www.europarl.europa.eu\/","usage-count":1,"subject":"Social_Science","created-by":"MyMemoryLoader","last-updated-by":null,"create-date":"2012-03-23
66
+ 16:52:56","last-update-date":"2012-03-23 16:52:56","match":0.16},{"id":"1046047","segment":"1
67
+ The offer in the ad wasnrquote t at all appealing","translation":"1 Lrquote
68
+ offerta della pubblicit\u00e0 non era affatto attraente","quality":"0","reference":"","usage-count":1,"subject":"Marketing","created-by":"curro","last-updated-by":null,"create-date":"1970-01-01
69
+ 00:59:59","last-update-date":"1970-01-01 00:59:59","match":0.15},{"id":"5591807","segment":"Project
70
+ results to date are as follows:","translation":"I risultati finora ottenuti
71
+ dal progetto sono i seguenti:","quality":"0","reference":"","usage-count":1,"subject":"Science","created-by":"rprosser","last-updated-by":null,"create-date":"2006-09-19
72
+ 13:37:10","last-update-date":"2006-09-19 13:37:10","match":0.08},{"id":"426707368","segment":"Perhaps,
73
+ Mr Schnellhardt, you are right that it wasn'' t totally practical, it wasn''
74
+ t perfect, it wasn'' t quite right.","translation":"Non nego, onorevole Schnellhardt,
75
+ che forse abbiamo emanato una legislazione non completamente applicabile,
76
+ non proprio perfetta, non assolutamente inappuntabile.","quality":"","reference":"http:\/\/www.europarl.europa.eu\/|@|http:\/\/www.europarl.europa.eu\/","usage-count":1,"subject":"Social_Science","created-by":"MyMemoryLoader","last-updated-by":null,"create-date":"2012-02-29
77
+ 11:37:34","last-update-date":"2012-02-29 11:37:34","match":0.06}]}'
78
+ http_version: '1.1'
79
+ recorded_at: Thu, 22 Nov 2012 09:38:28 GMT
80
+ recorded_with: VCR 2.0.1
@@ -0,0 +1 @@
1
+ {"responseData":{"translatedText":"Hello world"},"responseDetails":"","responseStatus":200,"matches":[{"id":"434413610","segment":"Hello World","translation":"Hello world","quality":"","reference":"\/\/it.wikipedia.org\/wiki\/Hello_world","usage-count":1,"subject":"All","created-by":"Wikipedia","last-updated-by":null,"create-date":"2012-11-22","last-update-date":"2012-11-22","match":0.95},{"id":"0","segment":"Hello World","translation":"Ciao Mondo","quality":"70","reference":"Machine Translation provided by Google, Microsoft, Worldlingo or MyMemory customized engine.","usage-count":1,"subject":"All","created-by":"MT!","last-updated-by":null,"create-date":"2012-11-22","last-update-date":"2012-11-22","match":0.85}]}
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ module Mymemory
4
+ describe Translation do
5
+ let(:subject) { Translation.new('hello world', :to => :it) }
6
+
7
+ it 'source language defaults to english' do
8
+ subject.from.should == :en
9
+ end
10
+
11
+ describe '#lang_pair' do
12
+ it 'joins the source and target language code with a pipe' do
13
+ subject.lang_pair.should == 'en|it'
14
+ end
15
+ end
16
+
17
+ describe '#url' do
18
+ it 'correctly encodes the url' do
19
+ url = 'http://mymemory.translated.net/api/get?q=hello%20world&langpair=en%7Cit'
20
+ subject.url.should == url
21
+ end
22
+ end
23
+
24
+ context 'when the target language is missing' do
25
+ it 'raises LanguageMissingError error' do
26
+ expect do
27
+ Translation.new('white rabbit', :from => :en)
28
+ end.to raise_error(LanguageMissingError)
29
+ end
30
+ end
31
+
32
+ describe '#parsed_response' do
33
+ context 'request was successful' do
34
+ let(:response) { double(:code => 200, :body => '{"some": {"cute": "json"}}') }
35
+
36
+ it 'returns an hash from parsed json' do
37
+ subject.stub(:response => response)
38
+ subject.parsed_response.should == {'some' => {'cute' => 'json'}}
39
+ end
40
+ end
41
+
42
+ context 'when request was not successful' do
43
+ let(:response) { double(:code => 422, :body => 'whatever!') }
44
+
45
+ it 'returns an empty hash' do
46
+ subject.stub(:response => response)
47
+ subject.parsed_response.should == {}
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mymemory do
4
+ context 'when making external API calls', :vcr do
5
+ it 'returns expected translation' do
6
+ text = "Wasn't born to Follow"
7
+ result = Mymemory.translate(text, :from => :en, :to => :it)
8
+ result.should == 'Non era nato per seguire'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'mymemory'
4
+ require 'vcr_config'
5
+
6
+ RSpec.configure do |config|
7
+ config.color = true
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'vcr'
2
+
3
+ VCR.configure do |c|
4
+ c.cassette_library_dir = 'spec/cassettes'
5
+ c.hook_into :fakeweb
6
+ c.configure_rspec_metadata!
7
+ end
8
+
9
+ RSpec.configure do |c|
10
+ c.treat_symbols_as_metadata_keys_with_true_values = true
11
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mymemory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - andrea longhi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
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
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: vcr
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakeweb
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: A (very) basic wrapper on mymemory.com translation resful API
111
+ email:
112
+ - andrea@spaghetticode.it
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .travis.yml
119
+ - Gemfile
120
+ - Guardfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/mymemory.rb
125
+ - lib/mymemory/translation.rb
126
+ - lib/mymemory/version.rb
127
+ - mymemory.gemspec
128
+ - spec/cassettes/Mymemory/when_making_external_API_calls/returns_expected_translation.yml
129
+ - spec/fixtures/hello_world.json
130
+ - spec/mymemory/translation_spec.rb
131
+ - spec/mymemory_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/vcr_config.rb
134
+ homepage: https://github.com/spaghetticode/mymemory
135
+ licenses: []
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ segments:
147
+ - 0
148
+ hash: -1638617341322682128
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
156
+ - 0
157
+ hash: -1638617341322682128
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.24
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: A (very) basic wrapper on mymemory.com translation resful API
164
+ test_files:
165
+ - spec/cassettes/Mymemory/when_making_external_API_calls/returns_expected_translation.yml
166
+ - spec/fixtures/hello_world.json
167
+ - spec/mymemory/translation_spec.rb
168
+ - spec/mymemory_spec.rb
169
+ - spec/spec_helper.rb
170
+ - spec/vcr_config.rb