linguara 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +2 -2
- data/VERSION +1 -1
- data/lib/linguara.rb +31 -2
- data/lib/linguara/active_record.rb +13 -49
- data/lib/linguara/configuration.rb +0 -2
- data/lib/linguara/utils.rb +18 -0
- data/linguara.gemspec +10 -3
- data/spec/data/schema.rb +6 -0
- data/spec/helper_model/blog_post.rb +3 -0
- data/spec/helper_model/database_mock.rb +7 -0
- data/spec/linguara_spec.rb +40 -2
- data/spec/spec_helper.rb +18 -0
- metadata +11 -4
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= linguara
|
2
2
|
|
3
|
-
|
3
|
+
This gem allows integratation with Linguara WebAPI.
|
4
4
|
|
5
5
|
== Note on Patches/Pull Requests
|
6
6
|
|
@@ -16,4 +16,4 @@ Description goes here.
|
|
16
16
|
|
17
17
|
Copyright (c) 2010 Aleksander Dabrowski. See LICENSE for details.
|
18
18
|
|
19
|
-
Thanks to: Piotr Barczuk. Additional thanks to Łukasz Adamczak, and Jurek Prokop, who
|
19
|
+
Thanks to: Piotr Barczuk. Additional thanks to Łukasz Adamczak, and Jurek Prokop, who were starring at mine screen while I was programming hard parts.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/linguara.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
3
|
require 'linguara/configuration'
|
4
|
+
require 'linguara/utils'
|
4
5
|
|
5
6
|
module Linguara
|
7
|
+
extend Linguara::Utils
|
6
8
|
autoload :ActiveRecord, 'linguara/active_record'
|
7
9
|
|
8
10
|
class << self
|
9
11
|
attr_accessor :configuration
|
10
12
|
|
11
|
-
def configure
|
13
|
+
def configure
|
12
14
|
self.configuration ||= Configuration.new
|
13
15
|
yield(configuration)
|
14
16
|
end
|
@@ -16,7 +18,7 @@ module Linguara
|
|
16
18
|
def accept_translation(translation)
|
17
19
|
target_language = translation[:target_language]
|
18
20
|
translation[:paragraph].each do |key,value|
|
19
|
-
class_name,id,field_name = value[:id].split('_')
|
21
|
+
class_name,id,order,field_name = value[:id].split('_')
|
20
22
|
content = value[:content]
|
21
23
|
original_locale = I18n.locale
|
22
24
|
element = class_name.constantize.find(id)
|
@@ -27,6 +29,33 @@ module Linguara
|
|
27
29
|
I18n.locale = original_locale
|
28
30
|
end
|
29
31
|
end
|
32
|
+
|
33
|
+
def send_request(element)
|
34
|
+
url= URI.parse(Linguara.configuration.server_path)
|
35
|
+
req = Net::HTTP::Post.new(url.path)
|
36
|
+
req.body = serialize_form_data({
|
37
|
+
:translation => {
|
38
|
+
:return_url => Linguara.configuration.return_url,
|
39
|
+
:due_date => '27-06-2010',
|
40
|
+
:source_language =>"pl",
|
41
|
+
:target_language =>"en",
|
42
|
+
:paragraph => element.fields_to_send,
|
43
|
+
:account_token => Linguara.configuration.api_key}})
|
44
|
+
req.content_type = 'application/x-www-form-urlencoded'
|
45
|
+
req.basic_auth Linguara.configuration.user, Linguara.configuration.password
|
46
|
+
#TODO handle timeout
|
47
|
+
begin
|
48
|
+
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
|
49
|
+
rescue Errno::ETIMEDOUT
|
50
|
+
handle_request_error
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# ovverride this method if you want to perform some action when connection
|
55
|
+
# with linguara cannot be established e.g. log request or redo the send
|
56
|
+
def handle_request_error
|
57
|
+
end
|
58
|
+
|
30
59
|
end
|
31
60
|
end
|
32
61
|
|
@@ -11,67 +11,31 @@ module Linguara
|
|
11
11
|
class_inheritable_accessor :linguara_translation_attribute_names
|
12
12
|
|
13
13
|
self.linguara_translation_attribute_names = attr_names.map(&:to_sym)
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
before_save :send_to_linguara
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
module InstanceMethods
|
20
|
-
protected
|
21
|
-
|
22
|
-
#TODO describe it, spec and move somewhere else
|
23
|
-
# http://www.keyongtech.com/5211204-nested-http-params-on-ruby
|
24
|
-
def serialize_form_data(data, path = "",serialized_params = [])
|
25
|
-
if data.kind_of? Hash
|
26
|
-
data.each_pair{|k,v| token = (path == "" ? url_encode(k) : "[#{url_encode(k)}]"); serialize_form_data(v, "#{path}#{token}",serialized_params)}
|
27
|
-
elsif data.kind_of? Array
|
28
|
-
data.each{|v| serialize_form_data(v, "#{path}[]", serialized_params) }
|
29
|
-
else
|
30
|
-
#end of recursion
|
31
|
-
serialized_params << "#{path}=#{url_encode(data)}"
|
32
|
-
end
|
33
|
-
|
34
|
-
return serialized_params.join("&") if (path == "")
|
35
|
-
end
|
36
|
-
|
37
|
-
def linguara_key_name(field)
|
38
|
-
"#{self.class.class_name}_#{self.id}_#{field}"
|
39
|
-
end
|
40
20
|
|
41
21
|
def fields_to_send
|
42
22
|
prepared_fields = {}
|
43
|
-
linguara_translation_attribute_names.
|
44
|
-
|
23
|
+
linguara_translation_attribute_names.each_with_index do |name, index|
|
24
|
+
key_name = linguara_key_name(name, index)
|
25
|
+
prepared_fields[key_name] = { :id => key_name, :content => self.send(name) }
|
45
26
|
end
|
46
27
|
prepared_fields
|
47
28
|
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def linguara_key_name(field, index)
|
33
|
+
"#{self.class.class_name}_#{self.id}_#{index}_#{field}"
|
34
|
+
end
|
48
35
|
|
49
36
|
def send_to_linguara
|
50
|
-
|
51
|
-
|
52
|
-
req.body = serialize_form_data({
|
53
|
-
:translation => {
|
54
|
-
:return_url => Linguara.configuration.return_url,
|
55
|
-
:due_date => '27-06-2010',
|
56
|
-
:source_language =>"pl",
|
57
|
-
:target_language =>"en",
|
58
|
-
:paragraph => fields_to_send,
|
59
|
-
:account_token => Linguara.configuration.api_key}})
|
60
|
-
req.content_type = 'application/x-www-form-urlencoded'
|
61
|
-
req.basic_auth Linguara.configuration.user, Linguara.configuration.password
|
62
|
-
|
63
|
-
logger.info(req.method)
|
64
|
-
logger.info(req.path)
|
65
|
-
logger.info(req.body)
|
66
|
-
logger.info(req.body_stream)
|
67
|
-
|
68
|
-
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
|
69
|
-
|
70
|
-
logger.info(res.code)
|
71
|
-
logger.info(res.message)
|
72
|
-
logger.info(res.body)
|
73
|
-
end
|
74
|
-
|
37
|
+
Linguara.send_request(self)
|
38
|
+
end
|
75
39
|
|
76
40
|
end
|
77
41
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Linguara
|
2
|
+
module Utils
|
3
|
+
#TODO describe it, spec and move somewhere else
|
4
|
+
# http://www.keyongtech.com/5211204-nested-http-params-on-ruby
|
5
|
+
def serialize_form_data(data, path = "",serialized_params = [])
|
6
|
+
if data.kind_of? Hash
|
7
|
+
data.each_pair{|k,v| token = (path == "" ? url_encode(k) : "[#{url_encode(k)}]"); serialize_form_data(v, "#{path}#{token}",serialized_params)}
|
8
|
+
elsif data.kind_of? Array
|
9
|
+
data.each{|v| serialize_form_data(v, "#{path}[]", serialized_params) }
|
10
|
+
else
|
11
|
+
#end of recursion
|
12
|
+
serialized_params << "#{path}=#{url_encode(data)}"
|
13
|
+
end
|
14
|
+
|
15
|
+
return serialized_params.join("&") if (path == "")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/linguara.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{linguara}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Aleksander Dabrowski"]
|
12
|
-
s.date = %q{2010-05
|
12
|
+
s.date = %q{2010-06-05}
|
13
13
|
s.description = %q{Gem to integrate with linguara api}
|
14
14
|
s.email = %q{aleks@kumulator.pl}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,7 +26,11 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/linguara.rb",
|
27
27
|
"lib/linguara/active_record.rb",
|
28
28
|
"lib/linguara/configuration.rb",
|
29
|
+
"lib/linguara/utils.rb",
|
29
30
|
"linguara.gemspec",
|
31
|
+
"spec/data/schema.rb",
|
32
|
+
"spec/helper_model/blog_post.rb",
|
33
|
+
"spec/helper_model/database_mock.rb",
|
30
34
|
"spec/linguara_spec.rb",
|
31
35
|
"spec/spec.opts",
|
32
36
|
"spec/spec_helper.rb"
|
@@ -37,7 +41,10 @@ Gem::Specification.new do |s|
|
|
37
41
|
s.rubygems_version = %q{1.3.7}
|
38
42
|
s.summary = %q{Gem to integrate with linguara api}
|
39
43
|
s.test_files = [
|
40
|
-
"spec/
|
44
|
+
"spec/data/schema.rb",
|
45
|
+
"spec/helper_model/blog_post.rb",
|
46
|
+
"spec/helper_model/database_mock.rb",
|
47
|
+
"spec/linguara_spec.rb",
|
41
48
|
"spec/spec_helper.rb"
|
42
49
|
]
|
43
50
|
|
data/spec/data/schema.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
def prepare_database
|
2
|
+
config = { :adapter => 'sqlite3', :database => ':memory:' }
|
3
|
+
ActiveRecord::Base.establish_connection(config)
|
4
|
+
schema_path ||= File.expand_path(File.dirname(__FILE__) + '/../data/schema.rb')
|
5
|
+
ActiveRecord::Migration.verbose = false
|
6
|
+
ActiveRecord::Base.silence { load(schema_path) }
|
7
|
+
end
|
data/spec/linguara_spec.rb
CHANGED
@@ -1,7 +1,45 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Linguara" do
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
it "should sent message to linguara for translatable classes" do
|
6
|
+
FakeWeb.register_uri(:post, 'http://www.example.com/', :exception => Net::HTTPError)
|
7
|
+
blog_post = BlogPost.new( :title => "Title text", :body => "Body text")
|
8
|
+
lambda {
|
9
|
+
blog_post.save
|
10
|
+
}.should raise_error(Net::HTTPError)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should accept correct translation' do
|
14
|
+
FakeWeb.register_uri(:post, 'http://www.example.com/', :body => 'response_for_linguara', :status => 200)
|
15
|
+
blog_post = BlogPost.new( :title => "Old title", :body => "Old body")
|
16
|
+
blog_post.save
|
17
|
+
id = blog_post.id
|
18
|
+
lambda {
|
19
|
+
Linguara.accept_translation(
|
20
|
+
:paragraph => {
|
21
|
+
"BlogPost_#{id}_0_title"=> { :id =>"BlogPost_#{id}_0_title", :content =>"Hello World!"},
|
22
|
+
"BlogPost_#{id}_1_body"=> { :id =>"BlogPost_#{id}_1_body", :content =>"Today is great weather, and I am going to EuRuKo."}
|
23
|
+
},
|
24
|
+
:source_language =>"pl",
|
25
|
+
:target_language =>"en"
|
26
|
+
)
|
27
|
+
}.should change(BlogPost, :count).by(0)
|
28
|
+
translation = BlogPost.last
|
29
|
+
translation.title.should eql("Hello World!")
|
30
|
+
translation.body.should eql("Today is great weather, and I am going to EuRuKo.")
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'Linguara::ActiveRecord' do
|
36
|
+
it "should prepare fields to translation" do
|
37
|
+
blog_post = BlogPost.new( :title => "Title text", :body => "Body text")
|
38
|
+
fields_to_send = blog_post.fields_to_send.to_a.sort { |a,b| a[0] <=> b[0] }
|
39
|
+
fields_to_send.size.should eql(2)
|
40
|
+
# It's complicated becouse linguara API is complicated. It will be much simplier in future
|
41
|
+
fields_to_send.first[1][:content].should eql("Title text")
|
42
|
+
fields_to_send.last[1][:content].should eql("Body text")
|
6
43
|
end
|
7
44
|
end
|
45
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,27 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_support'
|
7
|
+
require 'helper_model/database_mock'
|
3
8
|
require 'linguara'
|
9
|
+
require 'helper_model/blog_post'
|
10
|
+
require "erb"
|
11
|
+
include ERB::Util
|
4
12
|
require 'spec'
|
5
13
|
require 'spec/autorun'
|
14
|
+
require 'fakeweb'
|
15
|
+
|
16
|
+
prepare_database
|
17
|
+
|
18
|
+
Linguara.configure do |config|
|
19
|
+
config.api_key = 'api_keu'
|
20
|
+
config.server_path = 'http://www.example.com/'
|
21
|
+
config.return_url = 'http://maverick.kumulator.com:82/linguara'
|
22
|
+
end
|
6
23
|
|
7
24
|
Spec::Runner.configure do |config|
|
8
25
|
|
9
26
|
end
|
27
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linguara
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Aleksander Dabrowski
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-05
|
18
|
+
date: 2010-06-05 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -53,7 +53,11 @@ files:
|
|
53
53
|
- lib/linguara.rb
|
54
54
|
- lib/linguara/active_record.rb
|
55
55
|
- lib/linguara/configuration.rb
|
56
|
+
- lib/linguara/utils.rb
|
56
57
|
- linguara.gemspec
|
58
|
+
- spec/data/schema.rb
|
59
|
+
- spec/helper_model/blog_post.rb
|
60
|
+
- spec/helper_model/database_mock.rb
|
57
61
|
- spec/linguara_spec.rb
|
58
62
|
- spec/spec.opts
|
59
63
|
- spec/spec_helper.rb
|
@@ -92,5 +96,8 @@ signing_key:
|
|
92
96
|
specification_version: 3
|
93
97
|
summary: Gem to integrate with linguara api
|
94
98
|
test_files:
|
99
|
+
- spec/data/schema.rb
|
100
|
+
- spec/helper_model/blog_post.rb
|
101
|
+
- spec/helper_model/database_mock.rb
|
95
102
|
- spec/linguara_spec.rb
|
96
103
|
- spec/spec_helper.rb
|