to_lang 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ Gemfile.lock
5
5
  coverage/*
6
6
  doc/*
7
7
  .yardoc
8
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
@@ -0,0 +1,5 @@
1
+ ---
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+
@@ -2,6 +2,10 @@ h1. to_lang
2
2
 
3
3
  *to_lang* is a Ruby library that adds language translation methods to strings and arrays, backed by the Google Translate API.
4
4
 
5
+ h2. Build Status
6
+
7
+ "!https://travis-ci.org/jimmycuadra/to_lang.png!":http://travis-ci.org/jimmycuadra/to_lang
8
+
5
9
  h2. Installation
6
10
 
7
11
  Simply run @gem install to_lang@.
data/Rakefile CHANGED
@@ -5,12 +5,3 @@ require 'rspec/core/rake_task'
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
 
7
7
  task :default => :spec
8
-
9
- require 'yard'
10
- YARD::Rake::YardocTask.new do |t|
11
- t.options = [
12
- '--no-private',
13
- '--protected',
14
- '--markup', 'textile',
15
- ]
16
- end
@@ -1,5 +1,6 @@
1
1
  require 'httparty'
2
2
  require 'cgi'
3
+ require "to_lang/core_ext"
3
4
 
4
5
  module ToLang
5
6
  # Responsible for making the actual HTTP request to the Google Translate API.
@@ -0,0 +1,49 @@
1
+ # Hash extensions taken from John Nunemaker's Crack gem.
2
+ #
3
+ # Crack was previously a dependency for HTTParty, but since it
4
+ # no longer is, the methods being used by {ToLang} are included
5
+ # here directly.
6
+ #
7
+ class Hash
8
+ # Convert the entire has into a string suitable for HTTP requests.
9
+ #
10
+ # @return [String] This hash as a query string.
11
+ #
12
+ def to_params
13
+ params = self.map { |k,v| normalize_param(k,v) }.join
14
+ params.chop! # trailing &
15
+ params
16
+ end
17
+
18
+ # Turns a key value pair into a string suitable for HTTP requests.
19
+ #
20
+ # @param [Object] key The key for the param.
21
+ # @param [Object] value The value for the param.
22
+ #
23
+ # @return [String] This key value pair as a param
24
+ #
25
+ def normalize_param(key, value)
26
+ param = ''
27
+ stack = []
28
+
29
+ if value.is_a?(Array)
30
+ param << value.map { |element| normalize_param("#{key}[]", element) }.join
31
+ elsif value.is_a?(Hash)
32
+ stack << [key,value]
33
+ else
34
+ param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
35
+ end
36
+
37
+ stack.each do |parent, hash|
38
+ hash.each do |key, value|
39
+ if value.is_a?(Hash)
40
+ stack << ["#{parent}[#{key}]", value]
41
+ else
42
+ param << normalize_param("#{parent}[#{key}]", value)
43
+ end
44
+ end
45
+ end
46
+
47
+ param
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  module ToLang
2
2
  # The current version of the Ruby gem.
3
3
  #
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -13,7 +13,7 @@ describe ToLang::Connector do
13
13
  :target => 'es',
14
14
  :q => ["banana", "apple"]
15
15
  }
16
- ToLang::Connector::UNSORTED_QUERY_STRING_NORMALIZER.call(params).should == "key_with_no_value&key=apikey&target=es&q=banana&q=apple"
16
+ ToLang::Connector::UNSORTED_QUERY_STRING_NORMALIZER.call(params).should =~ /q=banana&q=apple/
17
17
  end
18
18
  end
19
19
 
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Hash do
4
+ describe "#to_params" do
5
+ test_hashes = {
6
+ { "foo" => "bar", "baz" => "bat" } => "foo=bar&baz=bat",
7
+ { "foo" => [ "bar", "baz" ] } => "foo[]=bar&foo[]=baz",
8
+ { "foo" => [ {"bar" => "1"}, {"bar" => 2} ] } => "foo[][bar]=1&foo[][bar]=2",
9
+ { "foo" => { "bar" => [ {"baz" => 1}, {"baz" => "2"} ] } } => "foo[bar][][baz]=1&foo[bar][][baz]=2",
10
+ { "foo" => {"1" => "bar", "2" => "baz"} } => "foo[1]=bar&foo[2]=baz"
11
+ }
12
+
13
+ test_hashes.each do |hash, params|
14
+ it "converts hash: #{hash.inspect} to params: #{params.inspect}" do
15
+ hash.to_params.split('&').sort.should == params.split('&').sort
16
+ end
17
+ end
18
+
19
+ it "doesn't leave a trailing &" do
20
+ {
21
+ :name => 'Bob',
22
+ :address => {
23
+ :street => '111 Ruby Ave.',
24
+ :city => 'Ruby Central',
25
+ :phones => ['111-111-1111', '222-222-2222']
26
+ }
27
+ }.to_params.should_not =~ /&$/
28
+ end
29
+
30
+ it "URL encodes unsafe characters" do
31
+ { :q => "?&\" +" }.to_params.should == "q=%3F%26%22%20%2B"
32
+ end
33
+
34
+ it "converts deeply nested hashes" do
35
+ {
36
+ :one => {
37
+ :two => {
38
+ :three => true
39
+ }
40
+ }
41
+ }.to_params.should == "one[two][three]=true"
42
+ end
43
+ end
44
+ end
@@ -16,14 +16,12 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_dependency "httparty", "~> 0.7.0"
20
- s.add_development_dependency "bundler", "~> 1.0.10"
21
- s.add_development_dependency "rake", "~> 0.8.7"
22
- s.add_development_dependency "rspec", "~> 2.5.0"
23
- s.add_development_dependency "simplecov", "~> 0.4.1"
24
- s.add_development_dependency "yard", "~> 0.6.5"
25
- s.add_development_dependency "RedCloth", "~> 4.2.7"
26
- s.add_development_dependency "guard-rspec", "~> 0.2.0"
27
- s.add_development_dependency "rb-fsevent", "~> 0.4.0" if RUBY_PLATFORM[/darwin/]
28
- s.add_development_dependency "growl", "~> 1.0.3" if RUBY_PLATFORM[/darwin/]
19
+ s.add_dependency "httparty", "~> 0.8.0"
20
+ s.add_development_dependency "rake", "~> 0.9.2"
21
+ s.add_development_dependency "rspec", "~> 2.6.0"
22
+ s.add_development_dependency "simplecov", "~> 0.5.3"
23
+ s.add_development_dependency "RedCloth", "~> 4.2.8"
24
+ s.add_development_dependency "guard-rspec", "~> 0.4.5"
25
+ s.add_development_dependency "rb-fsevent", "~> 0.4.3" if RUBY_PLATFORM[/darwin/]
26
+ s.add_development_dependency "growl_notify", "~> 0.0.1" if RUBY_PLATFORM[/darwin/]
29
27
  end
metadata CHANGED
@@ -1,139 +1,116 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: to_lang
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
4
5
  prerelease:
5
- version: 0.3.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Jimmy Cuadra
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-03-14 00:00:00 -07:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-09-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: httparty
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70175836589280 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
18
+ requirements:
22
19
  - - ~>
23
- - !ruby/object:Gem::Version
24
- version: 0.7.0
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
25
22
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
31
- none: false
32
- requirements:
33
- - - ~>
34
- - !ruby/object:Gem::Version
35
- version: 1.0.10
36
- type: :development
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *70175836589280
25
+ - !ruby/object:Gem::Dependency
39
26
  name: rake
40
- prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
27
+ requirement: &70175836588020 !ruby/object:Gem::Requirement
42
28
  none: false
43
- requirements:
29
+ requirements:
44
30
  - - ~>
45
- - !ruby/object:Gem::Version
46
- version: 0.8.7
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
47
33
  type: :development
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
34
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
35
+ version_requirements: *70175836588020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70175836585300 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
40
+ requirements:
55
41
  - - ~>
56
- - !ruby/object:Gem::Version
57
- version: 2.5.0
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
58
44
  type: :development
59
- version_requirements: *id004
60
- - !ruby/object:Gem::Dependency
61
- name: simplecov
62
45
  prerelease: false
63
- requirement: &id005 !ruby/object:Gem::Requirement
46
+ version_requirements: *70175836585300
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &70175836583680 !ruby/object:Gem::Requirement
64
50
  none: false
65
- requirements:
51
+ requirements:
66
52
  - - ~>
67
- - !ruby/object:Gem::Version
68
- version: 0.4.1
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.3
69
55
  type: :development
70
- version_requirements: *id005
71
- - !ruby/object:Gem::Dependency
72
- name: yard
73
56
  prerelease: false
74
- requirement: &id006 !ruby/object:Gem::Requirement
75
- none: false
76
- requirements:
77
- - - ~>
78
- - !ruby/object:Gem::Version
79
- version: 0.6.5
80
- type: :development
81
- version_requirements: *id006
82
- - !ruby/object:Gem::Dependency
57
+ version_requirements: *70175836583680
58
+ - !ruby/object:Gem::Dependency
83
59
  name: RedCloth
84
- prerelease: false
85
- requirement: &id007 !ruby/object:Gem::Requirement
60
+ requirement: &70175836582060 !ruby/object:Gem::Requirement
86
61
  none: false
87
- requirements:
62
+ requirements:
88
63
  - - ~>
89
- - !ruby/object:Gem::Version
90
- version: 4.2.7
64
+ - !ruby/object:Gem::Version
65
+ version: 4.2.8
91
66
  type: :development
92
- version_requirements: *id007
93
- - !ruby/object:Gem::Dependency
94
- name: guard-rspec
95
67
  prerelease: false
96
- requirement: &id008 !ruby/object:Gem::Requirement
68
+ version_requirements: *70175836582060
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: &70175836580900 !ruby/object:Gem::Requirement
97
72
  none: false
98
- requirements:
73
+ requirements:
99
74
  - - ~>
100
- - !ruby/object:Gem::Version
101
- version: 0.2.0
75
+ - !ruby/object:Gem::Version
76
+ version: 0.4.5
102
77
  type: :development
103
- version_requirements: *id008
104
- - !ruby/object:Gem::Dependency
105
- name: rb-fsevent
106
78
  prerelease: false
107
- requirement: &id009 !ruby/object:Gem::Requirement
79
+ version_requirements: *70175836580900
80
+ - !ruby/object:Gem::Dependency
81
+ name: rb-fsevent
82
+ requirement: &70175836579720 !ruby/object:Gem::Requirement
108
83
  none: false
109
- requirements:
84
+ requirements:
110
85
  - - ~>
111
- - !ruby/object:Gem::Version
112
- version: 0.4.0
86
+ - !ruby/object:Gem::Version
87
+ version: 0.4.3
113
88
  type: :development
114
- version_requirements: *id009
115
- - !ruby/object:Gem::Dependency
116
- name: growl
117
89
  prerelease: false
118
- requirement: &id010 !ruby/object:Gem::Requirement
90
+ version_requirements: *70175836579720
91
+ - !ruby/object:Gem::Dependency
92
+ name: growl_notify
93
+ requirement: &70175836578060 !ruby/object:Gem::Requirement
119
94
  none: false
120
- requirements:
95
+ requirements:
121
96
  - - ~>
122
- - !ruby/object:Gem::Version
123
- version: 1.0.3
97
+ - !ruby/object:Gem::Version
98
+ version: 0.0.1
124
99
  type: :development
125
- version_requirements: *id010
126
- description: Adds language translation methods to strings and arrays, backed by the Google Translate API
127
- email:
100
+ prerelease: false
101
+ version_requirements: *70175836578060
102
+ description: Adds language translation methods to strings and arrays, backed by the
103
+ Google Translate API
104
+ email:
128
105
  - jimmy@jimmycuadra.com
129
- executables:
106
+ executables:
130
107
  - to_lang
131
108
  extensions: []
132
-
133
109
  extra_rdoc_files: []
134
-
135
- files:
110
+ files:
136
111
  - .gitignore
112
+ - .rspec
113
+ - .travis.yml
137
114
  - .yardopts
138
115
  - Gemfile
139
116
  - Guardfile
@@ -144,43 +121,48 @@ files:
144
121
  - lib/to_lang.rb
145
122
  - lib/to_lang/codemap.rb
146
123
  - lib/to_lang/connector.rb
124
+ - lib/to_lang/core_ext.rb
147
125
  - lib/to_lang/translatable.rb
148
126
  - lib/to_lang/version.rb
149
127
  - spec/spec_helper.rb
150
128
  - spec/to_lang/connector_spec.rb
129
+ - spec/to_lang/core_ext_spec.rb
151
130
  - spec/to_lang/translatable_spec.rb
152
131
  - spec/to_lang_spec.rb
153
132
  - to_lang.gemspec
154
- has_rdoc: true
155
133
  homepage: https://github.com/jimmycuadra/to_lang
156
134
  licenses: []
157
-
158
135
  post_install_message:
159
136
  rdoc_options: []
160
-
161
- require_paths:
137
+ require_paths:
162
138
  - lib
163
- required_ruby_version: !ruby/object:Gem::Requirement
139
+ required_ruby_version: !ruby/object:Gem::Requirement
164
140
  none: false
165
- requirements:
166
- - - ">="
167
- - !ruby/object:Gem::Version
168
- version: "0"
169
- required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ segments:
146
+ - 0
147
+ hash: -2363449024598261982
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
149
  none: false
171
- requirements:
172
- - - ">="
173
- - !ruby/object:Gem::Version
174
- version: "0"
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ segments:
155
+ - 0
156
+ hash: -2363449024598261982
175
157
  requirements: []
176
-
177
158
  rubyforge_project: to_lang
178
- rubygems_version: 1.6.0
159
+ rubygems_version: 1.8.10
179
160
  signing_key:
180
161
  specification_version: 3
181
162
  summary: Translate strings with Google Translate
182
- test_files:
163
+ test_files:
183
164
  - spec/spec_helper.rb
184
165
  - spec/to_lang/connector_spec.rb
166
+ - spec/to_lang/core_ext_spec.rb
185
167
  - spec/to_lang/translatable_spec.rb
186
168
  - spec/to_lang_spec.rb