linguara 0.0.4 → 0.0.5

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.4
1
+ 0.0.5
@@ -1,6 +1,6 @@
1
1
  module Linguara
2
2
  class Configuration
3
- attr_accessor :api_key, :server_path, :return_url, :user, :password, :request_valid_for, :site_url, :log, :return_request
3
+ attr_accessor :api_key, :server_path, :return_url, :user, :password, :request_valid_for, :site_url, :log
4
4
 
5
5
  def initialize
6
6
  self.log = true
@@ -0,0 +1,9 @@
1
+ class LinguaraRequest
2
+
3
+ attr_accessor :request, :response
4
+
5
+ def initialize(options = {})
6
+ self.request = options[:request]
7
+ self.response = options[:response]
8
+ end
9
+ end
data/lib/linguara.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
+ require 'nokogiri'
3
4
  require 'linguara/configuration'
4
5
  require 'linguara/utils'
5
6
  require 'linguara/translation'
7
+ require 'linguara/request'
6
8
 
7
9
  module Linguara
8
10
  extend Linguara::Utils
@@ -31,6 +33,7 @@ module Linguara
31
33
  end
32
34
  end
33
35
 
36
+ # Sends translation request
34
37
  def send_translation_request(element, options = {})
35
38
  translation = Translation.new(element, options)
36
39
  url= URI.parse("#{Linguara.configuration.server_path}api/translations.xml")
@@ -38,30 +41,46 @@ module Linguara
38
41
  send_linguara_request(req, url)
39
42
  end
40
43
 
44
+ # Sends status request
41
45
  def send_status_query(translation_request_id)
42
46
  url= URI.parse("#{Linguara.configuration.server_path}api/translations/#{translation_request_id}/status.xml")
43
47
  req = prepare_request(url, :authorization => true)
44
48
  send_linguara_request(req, url)
45
49
  end
46
50
 
51
+ # Sends languages request
47
52
  def send_languages_request(options={})
48
53
  url= URI.parse("#{Linguara.configuration.server_path}api/languages.xml")
49
54
  req = prepare_request(url, options.merge(:method => :get))
50
55
  send_linguara_request(req, url)
51
56
  end
52
57
 
53
- def send_specializations_request(options)
58
+ # Sends specializations request
59
+ def send_specializations_request(options = {})
54
60
  url= URI.parse("#{Linguara.configuration.server_path}api/specializations.xml")
55
61
  req = prepare_request(url, options.merge(:method => :get))
56
62
  send_linguara_request(req, url)
57
63
  end
58
64
 
59
- def send_translators_request(options)
65
+ # Sends translators request
66
+ def send_translators_request(options = {})
60
67
  url= URI.parse("#{Linguara.configuration.server_path}api/translators.xml")
61
68
  req = prepare_request(url, options.merge(:method => :get))
62
69
  send_linguara_request(req, url)
63
70
  end
64
71
 
72
+ def available_languages
73
+ Nokogiri::XML(send_languages_request.response.body).xpath("//language").map do |element|
74
+ [element.xpath('name').inner_text, element.xpath('code').inner_text]
75
+ end
76
+ end
77
+
78
+ def available_specializations
79
+ Nokogiri::XML(send_specializations_request.response.body).xpath("//specialization").map do |element|
80
+ [element.xpath('name').inner_text, element.xpath('id').inner_text]
81
+ end
82
+ end
83
+
65
84
  # Override this method if you want to perform some action when connection
66
85
  # with linguara cannot be established e.g. log request or redo the send
67
86
  def handle_request_error
@@ -112,12 +131,7 @@ module Linguara
112
131
  log("SENDING LINGUARA REQUEST TO #{url.path}: \n#{req.body}")
113
132
  res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
114
133
  log("LINGUARA RESPONSE: #{res.message} -- #{res.body}")
115
- #TODO there should be object returned, whih contains request and respnse
116
- if Linguara.configuration.return_request
117
- return req
118
- else
119
- return res
120
- end
134
+ return LinguaraRequest.new( :request => req, :response => res )
121
135
  rescue Errno::ETIMEDOUT
122
136
  handle_request_error
123
137
  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.4"
8
+ s.version = "0.0.5"
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-23}
12
+ s.date = %q{2010-07-30}
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 = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/linguara.rb",
28
28
  "lib/linguara/active_record.rb",
29
29
  "lib/linguara/configuration.rb",
30
+ "lib/linguara/request.rb",
30
31
  "lib/linguara/translation.rb",
31
32
  "lib/linguara/utils.rb",
32
33
  "linguara.gemspec",
@@ -36,7 +36,7 @@ describe "Linguara" do
36
36
  it 'sends request' do
37
37
  blog_post = BlogPost.new( :title => "Old title", :body => "Old body")
38
38
  FakeWeb.register_uri(:post, "http://www.example.com/api/translations.xml", :body => 'response_from_linguara', :status => 200)
39
- response = Linguara.send_translation_request(blog_post)
39
+ response = Linguara.send_translation_request(blog_post).response
40
40
  response.body.should eql('response_from_linguara')
41
41
  end
42
42
  end
@@ -44,7 +44,7 @@ describe "Linguara" do
44
44
  describe '#send_status_query' do
45
45
  it 'sends request' do
46
46
  FakeWeb.register_uri(:post, "http://www.example.com/api/translations/5/status.xml", :body => 'response_from_linguara', :status => 200)
47
- response = Linguara.send_status_query(5)
47
+ response = Linguara.send_status_query(5).response
48
48
  response.body.should eql('response_from_linguara')
49
49
  end
50
50
  end
@@ -52,7 +52,7 @@ describe "Linguara" do
52
52
  describe '#send_languages_request' do
53
53
  it 'sends request' do
54
54
  FakeWeb.register_uri(:get, "http://www.example.com/api/languages.xml", :body => 'response_from_linguara', :status => 200)
55
- response = Linguara.send_languages_request("city"=>"321", "specialization"=>"technical", "country"=>"pl", "native_speaker"=>"1", "max_price"=>"12")
55
+ response = Linguara.send_languages_request("city"=>"321", "specialization"=>"technical", "country"=>"pl", "native_speaker"=>"1", "max_price"=>"12").response
56
56
  response.body.should eql('response_from_linguara')
57
57
  end
58
58
  end
@@ -60,7 +60,7 @@ describe "Linguara" do
60
60
  describe '#send_specializations_request' do
61
61
  it 'sends request' do
62
62
  FakeWeb.register_uri(:get, "http://www.example.com/api/specializations.xml", :body => 'response_from_linguara', :status => 200)
63
- response = Linguara.send_specializations_request("language"=>"suomi", "sworn" => "1", "native_speaker"=>"1", "max_price"=>"12")
63
+ response = Linguara.send_specializations_request("language"=>"suomi", "sworn" => "1", "native_speaker"=>"1", "max_price"=>"12").response
64
64
  response.body.should eql('response_from_linguara')
65
65
  end
66
66
  end
@@ -68,7 +68,7 @@ describe "Linguara" do
68
68
  describe '#send_translators_request' do
69
69
  it 'sends request' do
70
70
  FakeWeb.register_uri(:get, "http://www.example.com/api/translators.xml", :body => 'response_from_linguara', :status => 200)
71
- response = Linguara.send_translators_request("city"=>"warsaw", "country"=>"pl", "max_price"=>"12", "query"=>"wojcisz", "target_language"=>"en", "source_language" => "pl")
71
+ response = Linguara.send_translators_request("city"=>"warsaw", "country"=>"pl", "max_price"=>"12", "query"=>"wojcisz", "target_language"=>"en", "source_language" => "pl").response
72
72
  response.body.should eql('response_from_linguara')
73
73
  end
74
74
  end
@@ -108,7 +108,54 @@ describe "Linguara" do
108
108
  end
109
109
 
110
110
  end
111
+
112
+ describe '#available_languages' do
113
+ before :each do
114
+ @response_body = "<?xml version='1.0' encoding='UTF-8'?>
115
+ <languages>
116
+ <language>
117
+ <code>pl</code>
118
+ <name>Polish</name>
119
+ </language>
120
+ <language>
121
+ <code>en</code>
122
+ <name>English</name>
123
+ </language>
124
+ <language>
125
+ <code>tlh</code>
126
+ <name>Klingon</name>
127
+ </language>
128
+ </languages>"
129
+ FakeWeb.register_uri(:get, "http://www.example.com/api/languages.xml", :body => @response_body, :status => 200)
130
+ end
131
+
132
+ it 'returns array with languages and codes' do
133
+ expected_array = [['Polish','pl'],['English','en'],['Klingon','tlh']]
134
+ Linguara.available_languages.should eql(expected_array)
135
+ end
136
+ end
111
137
 
138
+ describe '#available_specializations' do
139
+ before :each do
140
+ @response_body = "<?xml version='1.0' encoding='UTF-8'?>
141
+ <specializations>
142
+ <specialization>
143
+ <id>2</id>
144
+ <name>IT</name>
145
+ </specialization>
146
+ <specialization>
147
+ <id>3</id>
148
+ <name>Architecture</name>
149
+ </specialization>
150
+ </specializations>"
151
+ FakeWeb.register_uri(:get, "http://www.example.com/api/specializations.xml", :body => @response_body, :status => 200)
152
+ end
153
+
154
+ it 'returns array with languages and codes' do
155
+ expected_array = [['IT','2'],['Architecture','3']]
156
+ Linguara.available_specializations.should eql(expected_array)
157
+ end
158
+ end
112
159
  end
113
160
 
114
161
  describe 'Linguara::ActiveRecord' do
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: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
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-23 00:00:00 +02:00
19
+ date: 2010-07-30 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -55,6 +55,7 @@ files:
55
55
  - lib/linguara.rb
56
56
  - lib/linguara/active_record.rb
57
57
  - lib/linguara/configuration.rb
58
+ - lib/linguara/request.rb
58
59
  - lib/linguara/translation.rb
59
60
  - lib/linguara/utils.rb
60
61
  - linguara.gemspec