linguara 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -44,7 +44,7 @@ module Linguara
44
44
  protected
45
45
 
46
46
  def linguara_key_name(field, index)
47
- "#{self.class.class_name}_#{self.id}_#{index}_#{field}"
47
+ "linguara_#{self.class.class_name}_#{self.id}_#{index}_#{field}"
48
48
  end
49
49
 
50
50
  #override this if you want to have a customised method for determining which language to use.
@@ -1,22 +1,56 @@
1
1
  class Translation
2
2
 
3
- attr_accessor :translation_hash
3
+ attr_accessor :translation_hash, :element
4
4
 
5
5
  def initialize(element,options = {})
6
- unless options[:translation] && options[:translation][:due_date]
7
- options[:translation] = Hash.new
8
- options[:translation][:due_date] ||= Date.today + Linguara.configuration.request_valid_for.to_i.days unless Linguara.configuration.request_valid_for.blank?
9
- options[:translation][:due_date] ||= Date.today + 1.month
6
+ unless options[:due_date]
7
+ options[:due_date] ||= Date.today + Linguara.configuration.request_valid_for.to_i.days unless Linguara.configuration.request_valid_for.blank?
8
+ options[:due_date] ||= Date.today + 1.month
10
9
  end
11
10
  options[:return_url] ||= Linguara.configuration.return_url
12
11
  options[:source_language] ||= I18n.locale.to_s
13
- options[:content] = Hash.new
14
- options[:content][:plain] = Hash.new
15
- options[:content][:plain][:paragraphs] = element.fields_to_send
12
+ self.element = element#.fields_to_send
16
13
  self.translation_hash = options
17
14
  end
18
15
 
19
16
  def to_hash
20
- self.translation_hash
17
+ ret = {}
18
+ ret[:translation] = self.translation_hash
19
+ ret
20
+ end
21
+
22
+ def to_xml(options = {}, builder_options = {})
23
+ builder_options[:indent] ||= 2
24
+ xml = builder_options[:builder] ||= Builder::XmlMarkup.new(:indent => builder_options[:indent])
25
+ xml.instruct! unless builder_options[:skip_instruct]
26
+ xml.translation do
27
+ self.translation_hash.merge(options).each_pair do |k, v|
28
+ next if k.blank? or v.blank?
29
+ tag_element(xml, k, v)
30
+ end
31
+ xml.content :type => 'xml' do
32
+ self.element.fields_to_send.each_pair do |k, v|
33
+ xml.paragraph v, :id => k
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def to_xml_recursive(xml, object)
42
+ object.each_pair do |k, v|
43
+ tag_element(xml, k, v)
44
+ end
45
+ end
46
+
47
+ def tag_element(builder, k, v)
48
+ if v.kind_of?(Hash)
49
+ builder.tag!(k) do
50
+ to_xml_recursive(builder, v)
51
+ end
52
+ else
53
+ builder.tag!(k, v)
54
+ end
21
55
  end
22
56
  end
@@ -1,21 +1,9 @@
1
1
  module Linguara
2
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 == "" ? CGI::escape(k.to_s) : "[#{CGI::escape(k.to_s)}]"); serialize_form_data(v, "#{path}#{token}",serialized_params)}
8
- else
9
- #end of recursion
10
- serialized_params << "#{path}=#{CGI::escape(data.to_s)}"
11
- end
12
-
13
- return serialized_params.join("&") if (path == "")
14
- end
15
3
 
16
- def strip_blank_attributes(data)
17
- data = data.each_pair { |k,v| v.is_a?(Hash) ? (strip_blank_attributes(v)) : (v) }
18
- data.delete_if { |k,v| v.blank?}
19
- end
4
+ def strip_blank_attributes(data)
5
+ data = data.each_pair { |k,v| v.is_a?(Hash) ? (strip_blank_attributes(v)) : (v) }
6
+ data.delete_if { |k,v| v.blank?}
7
+ end
20
8
  end
21
9
  end
data/lib/linguara.rb CHANGED
@@ -37,36 +37,31 @@ module Linguara
37
37
  def send_translation_request(element, options = {})
38
38
  translation = Translation.new(element, options)
39
39
  url= URI.parse("#{Linguara.configuration.server_path}api/translations.xml")
40
- req = prepare_request(url, translation.to_hash.merge( :authorization => true))
41
- send_linguara_request(req, url)
40
+ send_linguara_request(url, :post, translation.to_xml(strip_blank_attributes(options).merge(authorization_options)))
42
41
  end
43
42
 
44
43
  # Sends status request
45
44
  def send_status_query(translation_request_id)
46
45
  url= URI.parse("#{Linguara.configuration.server_path}api/translations/#{translation_request_id}/status.xml")
47
- req = prepare_request(url, :authorization => true)
48
- send_linguara_request(req, url)
46
+ send_linguara_request(url, :post, authorization_xml)
49
47
  end
50
48
 
51
49
  # Sends languages request
52
50
  def send_languages_request(options={})
53
51
  url= URI.parse("#{Linguara.configuration.server_path}api/languages.xml")
54
- req = prepare_request(url, options.merge(:method => :get))
55
- send_linguara_request(req, url)
52
+ send_linguara_request(url, :get)
56
53
  end
57
54
 
58
55
  # Sends specializations request
59
56
  def send_specializations_request(options = {})
60
57
  url= URI.parse("#{Linguara.configuration.server_path}api/specializations.xml")
61
- req = prepare_request(url, options.merge(:method => :get))
62
- send_linguara_request(req, url)
58
+ send_linguara_request(url, :get)
63
59
  end
64
60
 
65
61
  # Sends translators request
66
62
  def send_translators_request(options = {})
67
63
  url= URI.parse("#{Linguara.configuration.server_path}api/translators.xml")
68
- req = prepare_request(url, options.merge(:method => :get))
69
- send_linguara_request(req, url)
64
+ send_linguara_request(url, :get)
70
65
  end
71
66
 
72
67
  def available_languages
@@ -102,30 +97,33 @@ module Linguara
102
97
  end
103
98
 
104
99
  private
105
-
106
- def prepare_request(url, data = {} )
107
- data = strip_blank_attributes(data)
108
- if data[:method] == :get
100
+
101
+ def authorization_options
102
+ {
103
+ :site_url => Linguara.configuration.site_url,
104
+ :account_token => Linguara.configuration.api_key
105
+ }
106
+ end
107
+
108
+ def authorization_xml
109
+ "<translation><site_url>#{Linguara.configuration.site_url}</site_url><account_token>#{Linguara.configuration.api_key}</account_token></translation>"
110
+ end
111
+
112
+ def prepare_request(url, method, data)
113
+ if method == :get
109
114
  req = Net::HTTP::Get.new(url.path)
110
- data.delete(:method)
111
115
  else
112
116
  req = Net::HTTP::Post.new(url.path)
113
117
  end
114
- if data[:authorization]
115
- data.delete(:authorization)
116
- req.body = serialize_form_data({
117
- :site_url => Linguara.configuration.site_url,
118
- :account_token => Linguara.configuration.api_key
119
- }.merge(data))
120
- else
121
- req.body = serialize_form_data(data)
122
- end
123
- req.content_type = 'application/x-www-form-urlencoded'
118
+ #log("BODY ----- :\n\n #{data}")
119
+ req.body = "#{data}"
120
+ req.content_type = 'text/xml'
124
121
  req.basic_auth(Linguara.configuration.user, Linguara.configuration.password)
125
122
  req
126
123
  end
127
-
128
- def send_linguara_request(req, url)
124
+
125
+ def send_linguara_request(url, method, data = '' )
126
+ req = prepare_request url, method, data
129
127
  #TODO handle timeout
130
128
  begin
131
129
  log("SENDING LINGUARA REQUEST TO #{url.path}: \n#{req.body}")
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.5"
8
+ s.version = "0.0.6"
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", "Piotr Barczuk"]
12
- s.date = %q{2010-07-30}
12
+ s.date = %q{2010-08-11}
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 = [
@@ -76,35 +76,24 @@ describe "Linguara" do
76
76
  describe '#prepare_request' do
77
77
  before :each do
78
78
  @url = URI.parse("http://www.example.com/api/translations")
79
- @options = { :translation => { :translator => { :city => "Prague"}}}
79
+ @data = '<translation><site_url>text.com</site_url><account_token>abcdefg</account_token></translation>'
80
80
  end
81
81
 
82
82
  it 'sets :POST by deafault' do
83
- request = Linguara.send(:prepare_request, @url, @options)
83
+ request = Linguara.send(:prepare_request, @url, :post, @data)
84
84
  request.is_a?(Net::HTTP::Post).should be_true
85
85
  request.body.include?("post").should be_false
86
86
  end
87
87
 
88
88
  it 'sets :GET when option given' do
89
- request = Linguara.send(:prepare_request, @url, @options.merge(:method => :get))
89
+ request = Linguara.send(:prepare_request, @url, :get, @data)
90
90
  request.is_a?(Net::HTTP::Get).should be_true
91
91
  request.body.include?("get").should be_false
92
92
  end
93
93
 
94
- it 'skips blank parameters' do
95
- request = Linguara.send(:prepare_request, @url, @options.merge(:blank_attribute => ""))
96
- request.body.include?("blank_attribute").should be_false
97
- end
98
-
99
- it 'skips blank parameters deep' do
100
- request = Linguara.send(:prepare_request, @url, @options.merge(:blank_parent => { :blank_attribute => ""}))
101
- request.body.include?("blank_attribute").should be_false
102
- request.body.include?("blank_parent").should be_false
103
- end
104
-
105
- it 'sets content type to application/x-www-form-urlencoded' do
106
- request = Linguara.send(:prepare_request, @url, @options)
107
- request.content_type.should be_eql("application/x-www-form-urlencoded")
94
+ it 'sets content type to text/xml' do
95
+ request = Linguara.send(:prepare_request, @url, :get, @data)
96
+ request.content_type.should be_eql("text/xml")
108
97
  end
109
98
 
110
99
  end
@@ -7,18 +7,18 @@ describe Translation do
7
7
  end
8
8
 
9
9
  it 'creates default due_date on default' do
10
- subject.to_hash[:translation][:due_date].should eql(Date.today + Linguara.configuration.request_valid_for.to_i.days)
10
+ subject.to_xml.should match(Regexp.new("<due_date>#{Date.today + Linguara.configuration.request_valid_for.to_i.days}</due_date>"))
11
11
  end
12
12
 
13
13
  it 'creates return_url on default' do
14
- subject.to_hash[:return_url].should eql(Linguara.configuration.return_url)
14
+ subject.to_xml.should match(Regexp.new("<return_url>#{Linguara.configuration.return_url}</return_url>"))
15
15
  end
16
16
 
17
17
  it 'sets source language on default' do
18
- subject.to_hash[:source_language].should eql(I18n.locale.to_s)
18
+ subject.to_xml.should match(Regexp.new("<source_language>#{I18n.locale.to_s}</source_language>"))
19
19
  end
20
20
 
21
21
  it 'creates paragraphs for model' do
22
- subject.to_hash[:content][:plain][:paragraphs].should eql(@blog_post.fields_to_send)
22
+ subject.to_xml.should match(Regexp.new("(<paragraph id=['|\"].*['|\"]>[^<>]+</paragraph>\s*)+"))
23
23
  end
24
24
  end
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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aleksander Dabrowski
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-30 00:00:00 +02:00
19
+ date: 2010-08-11 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency