web_translate_it 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/history.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## Version 2.0.3 / 2012-06-07
2
+
3
+ * Fix: String#translation_for('xx') should not return an array of all translations. #88
4
+ * Fix: Term#translation_for('xx') should not return an array of all term translations.
5
+ * String#find and Term#find now return nil if no element was found with that ID.
6
+
1
7
  ## Version 2.0.2 / 2012-06-06
2
8
 
3
9
  * String, Translation, Term and TermTranslation classes now raise exceptions and display useful error messages. #87
@@ -86,6 +86,7 @@ module WebTranslateIt
86
86
  end
87
87
 
88
88
  # Find a String based on its ID
89
+ # Return a String object, or nil if not found.
89
90
  #
90
91
  # Implementation Example:
91
92
  #
@@ -104,8 +105,9 @@ module WebTranslateIt
104
105
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
105
106
 
106
107
  begin
107
- response = Util.handle_response(Connection.http_connection.request(request), true, true)
108
- string = WebTranslateIt::String.new(YAML.load(response))
108
+ response = Connection.http_connection.request(request)
109
+ return nil if response.code.to_i == 404
110
+ string = WebTranslateIt::String.new(YAML.load(response.body))
109
111
  string.new_record = false
110
112
  return string
111
113
  rescue Timeout::Error
@@ -165,7 +167,9 @@ module WebTranslateIt
165
167
  #
166
168
 
167
169
  def translation_for(locale)
168
- return self.translations unless self.translations == []
170
+ translation = self.translations.detect{ |t| t.locale == locale }
171
+ return translation if translation
172
+ return nil if self.new_record
169
173
  request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{self.id}/locales/#{locale}/translations.yaml")
170
174
  request.add_field("X-Client-Name", "web_translate_it")
171
175
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
@@ -54,7 +54,7 @@ module WebTranslateIt
54
54
  terms = []
55
55
  while(request) do
56
56
  response = Connection.http_connection.request(request)
57
- YAML.load(response).each do |term_response|
57
+ YAML.load(response.body).each do |term_response|
58
58
  term = WebTranslateIt::Term.new(term_response)
59
59
  term.new_record = false
60
60
  terms.push(term)
@@ -77,6 +77,7 @@ module WebTranslateIt
77
77
  end
78
78
 
79
79
  # Find a Term based on its ID
80
+ # Returns a Term object or nil if not found.
80
81
  #
81
82
  # Implementation Example:
82
83
  #
@@ -95,7 +96,8 @@ module WebTranslateIt
95
96
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
96
97
 
97
98
  begin
98
- response = Util.handle_response(Connection.http_connection.request(request), true, true)
99
+ response = Connection.http_connection.request(request)
100
+ return nil if response.code.to_i == 404
99
101
  term = WebTranslateIt::Term.new(YAML.load(response.body))
100
102
  term.new_record = false
101
103
  return term
@@ -156,7 +158,9 @@ module WebTranslateIt
156
158
  #
157
159
 
158
160
  def translation_for(locale)
159
- return self.translations unless self.translations == []
161
+ translation = self.translations.detect{ |t| t.locale == locale }
162
+ return translation if translation
163
+ return nil if self.new_record
160
164
  request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{self.id}/locales/#{locale}/translations.yaml")
161
165
  request.add_field("X-Client-Name", "web_translate_it")
162
166
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
@@ -93,7 +93,7 @@ describe WebTranslateIt::String do
93
93
  string_fetched.should_not be_nil
94
94
 
95
95
  string_fetched.delete
96
- lambda { WebTranslateIt::String.find(string.id) }.should raise_error
96
+ WebTranslateIt::String.find(string.id).should be_nil
97
97
  end
98
98
  end
99
99
  end
@@ -133,4 +133,27 @@ describe WebTranslateIt::String do
133
133
  end
134
134
  end
135
135
  end
136
+
137
+ describe "#translation_for" do
138
+ it "should fetch translations" do
139
+ translation = WebTranslateIt::Translation.new({ "locale" => "en", "text" => "Hello" })
140
+ string = WebTranslateIt::String.new({ "key" => "bacon", "translations" => [translation] })
141
+ WebTranslateIt::Connection.new(api_key) do
142
+ string.save
143
+ string_fetched = WebTranslateIt::String.find(string.id)
144
+ string_fetched.translation_for("en").should_not be_nil
145
+ string_fetched.translation_for("en").text.should == "Hello"
146
+ string_fetched.translation_for("fr").should be_nil
147
+ string.delete
148
+ end
149
+ end
150
+
151
+ it "should not return a stale object" do
152
+ string = WebTranslateIt::String.new({ :key => "bacon" })
153
+ translation = WebTranslateIt::Translation.new({ :locale => "es", :text => "text", :string_id => string.id })
154
+ string.translations << translation
155
+ string.translation_for('fr').should be_nil
156
+ string.translation_for('es').should_not be_nil
157
+ end
158
+ end
136
159
  end
data/version CHANGED
@@ -1 +1 @@
1
- 2.0.2
1
+ 2.0.3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_translate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-06 00:00:00.000000000 Z
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multipart-post