aloha_analyzer 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/aloha_analyzer/user.rb +36 -36
- data/lib/aloha_analyzer/version.rb +1 -1
- data/spec/aloha_analyzer/user_spec.rb +116 -62
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f365ff8ea98aed30b293c506c024b2bd9cc6392e
|
4
|
+
data.tar.gz: 41440c2ab6bad0ddb4b3473c7cd7dd4a58cd6715
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f24982aee08a1063139ca71613aafcf5158c34a4a97a6078968e0038f115ef1ab92ad4e2a7bb46063707972943edbedfff02053c695ec2addadbd90996a3a5f
|
7
|
+
data.tar.gz: 902e781c090f5ef1711bb645987645e629f0ffa7e81904286bf23a0223ede1f5c24c2131efe86df15ee1ce3cabd1765475c6ed17fbc4b40ec2c104b153e9bb3c
|
data/lib/aloha_analyzer/user.rb
CHANGED
@@ -1,30 +1,26 @@
|
|
1
1
|
module AlohaAnalyzer
|
2
2
|
class User
|
3
3
|
|
4
|
-
attr_reader :language
|
4
|
+
attr_reader :language, :analysis
|
5
5
|
|
6
|
-
def initialize(language, users, options = {})
|
7
|
-
@language = language.downcase
|
8
|
-
@users = users
|
6
|
+
def initialize(language, users, options = {}, analysis = nil)
|
7
|
+
@language = clean_language(language.downcase)
|
8
|
+
@users = clean_users(users)
|
9
9
|
@users_count = users.size
|
10
10
|
@options = options
|
11
|
-
@analysis =
|
12
|
-
|
13
|
-
clean_language!
|
14
|
-
clean_users_languages!
|
11
|
+
@analysis = analysis || boilerplate
|
15
12
|
end
|
16
13
|
|
17
14
|
def analyze
|
18
|
-
prepare!
|
19
15
|
@users.each do |user|
|
20
16
|
if user['lang'] == @language
|
21
17
|
add_account_language_user(user)
|
22
|
-
@analysis[
|
18
|
+
@analysis['account_language']['count'] += 1
|
23
19
|
else
|
24
20
|
add_foreign_language_user(user)
|
25
|
-
@analysis[
|
21
|
+
@analysis['foreign_languages_count'] += 1
|
26
22
|
end
|
27
|
-
@analysis[
|
23
|
+
@analysis['count'] += 1
|
28
24
|
end
|
29
25
|
@analysis
|
30
26
|
end
|
@@ -32,56 +28,60 @@ module AlohaAnalyzer
|
|
32
28
|
private
|
33
29
|
|
34
30
|
def add_account_language_user(user)
|
35
|
-
unless too_many_users?(@analysis[
|
36
|
-
@analysis[
|
31
|
+
unless too_many_users?(@analysis['account_language']['users'])
|
32
|
+
@analysis['account_language']['users'].push(user)
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
40
36
|
def add_foreign_language_user(user)
|
41
37
|
prepare_foreign_language(user['lang'])
|
42
|
-
@analysis[
|
43
|
-
unless too_many_users?(@analysis[
|
44
|
-
@analysis[
|
38
|
+
@analysis['foreign_languages'][user['lang']]['count'] += 1
|
39
|
+
unless too_many_users?(@analysis['foreign_languages'][user['lang']]['users'])
|
40
|
+
@analysis['foreign_languages'][user['lang']]['users'].push(user)
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
48
44
|
def prepare_foreign_language(abbreviation)
|
49
|
-
if @analysis[
|
50
|
-
@analysis[
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
if @analysis['foreign_languages'][abbreviation].nil?
|
46
|
+
@analysis['foreign_languages'][abbreviation] = {
|
47
|
+
'count' => 0,
|
48
|
+
'language' => Language.find_by_abbreviation(abbreviation),
|
49
|
+
'users' => []
|
54
50
|
}
|
55
51
|
end
|
56
52
|
end
|
57
53
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
54
|
+
def boilerplate
|
55
|
+
{
|
56
|
+
'account_language' => {
|
57
|
+
'count' => 0,
|
58
|
+
'language' => Language.find_by_abbreviation(@language),
|
59
|
+
'users' => []
|
60
|
+
},
|
61
|
+
'foreign_languages_count' => 0,
|
62
|
+
'count' => 0,
|
63
|
+
'foreign_languages' => {}
|
63
64
|
}
|
64
|
-
@analysis[:foreign_languages_count] = 0
|
65
|
-
@analysis[:count] = 0
|
66
|
-
@analysis[:foreign_languages] = Hash.new
|
67
65
|
end
|
68
66
|
|
69
67
|
def too_many_users?(users)
|
70
|
-
if @options[
|
68
|
+
if @options['user_limit_per_language'] && users.size >= @options['user_limit_per_language']
|
71
69
|
true
|
72
70
|
else
|
73
71
|
false
|
74
72
|
end
|
75
73
|
end
|
76
74
|
|
77
|
-
def clean_language
|
78
|
-
if Language.aliases.keys.include?(
|
79
|
-
|
75
|
+
def clean_language(language)
|
76
|
+
if Language.aliases.keys.include?(language)
|
77
|
+
Language.aliases[language]
|
78
|
+
else
|
79
|
+
language
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
def
|
84
|
-
|
83
|
+
def clean_users(users)
|
84
|
+
users.map do |user|
|
85
85
|
if Language.aliases.keys.include?(user['lang'].downcase)
|
86
86
|
user['lang'] = Language.aliases[user['lang'].downcase]
|
87
87
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe AlohaAnalyzer::User do
|
4
|
-
subject(:user) { described_class.new(language, users, options) }
|
4
|
+
subject(:user) { described_class.new(language, users, options, analysis) }
|
5
5
|
let(:language) { 'en' }
|
6
6
|
let(:options) { {} }
|
7
|
+
let(:analysis) { nil }
|
7
8
|
|
8
9
|
describe '#new' do
|
9
10
|
let(:users) { [] }
|
@@ -31,17 +32,17 @@ describe AlohaAnalyzer::User do
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
|
-
context 'when
|
35
|
-
let(:
|
35
|
+
context 'when analysis is not nil' do
|
36
|
+
let(:analysis) { double }
|
36
37
|
|
37
|
-
it '
|
38
|
-
subject.
|
38
|
+
it 'sets the analysis to the argument' do
|
39
|
+
subject.analysis.should eq analysis
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
44
|
describe '#analyze' do
|
44
|
-
subject(:analyze) { described_class.new(language, users, options).analyze }
|
45
|
+
subject(:analyze) { described_class.new(language, users, options, analysis).analyze }
|
45
46
|
context 'when no users' do
|
46
47
|
let(:users) { [] }
|
47
48
|
|
@@ -50,21 +51,21 @@ describe AlohaAnalyzer::User do
|
|
50
51
|
end
|
51
52
|
|
52
53
|
it 'includes the total count' do
|
53
|
-
subject[
|
54
|
+
subject['count'].should eq 0
|
54
55
|
end
|
55
56
|
|
56
57
|
it 'has no results with the user language' do
|
57
|
-
subject[
|
58
|
+
subject['account_language']['count'].should eq 0
|
58
59
|
end
|
59
60
|
|
60
61
|
it 'has no results without the user language' do
|
61
|
-
subject[
|
62
|
-
subject[
|
62
|
+
subject['foreign_languages'].should eq({})
|
63
|
+
subject['foreign_languages_count'].should eq 0
|
63
64
|
end
|
64
65
|
|
65
66
|
it 'includes the user lanugage' do
|
66
|
-
subject[
|
67
|
-
|
67
|
+
subject['account_language']['language'].should eq(
|
68
|
+
'abbreviation'=>'en', 'greeting' => 'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia'
|
68
69
|
)
|
69
70
|
end
|
70
71
|
end
|
@@ -85,32 +86,32 @@ describe AlohaAnalyzer::User do
|
|
85
86
|
end
|
86
87
|
|
87
88
|
it 'includes the total count' do
|
88
|
-
subject[
|
89
|
+
subject['count'].should eq 4
|
89
90
|
end
|
90
91
|
|
91
92
|
it 'includes the user lanugage' do
|
92
|
-
subject[
|
93
|
-
|
94
|
-
|
95
|
-
|
93
|
+
subject['account_language'].should eq(
|
94
|
+
'count' => 2,
|
95
|
+
'language' => {'abbreviation'=>'en', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia', 'greeting'=>'hello!'},
|
96
|
+
'users' => [{'id' => '1', 'lang' => 'en'}, {'id' => '3', 'lang' => 'en'}]
|
96
97
|
)
|
97
98
|
end
|
98
99
|
|
99
100
|
it 'includs the foreign followers count' do
|
100
|
-
subject[
|
101
|
+
subject['foreign_languages_count'].should eq 2
|
101
102
|
end
|
102
103
|
|
103
104
|
it 'returns results based on the user language' do
|
104
|
-
subject[
|
105
|
+
subject['foreign_languages'].should == {
|
105
106
|
'fr' => {
|
106
|
-
count
|
107
|
-
language
|
108
|
-
users
|
107
|
+
'count' => 1,
|
108
|
+
'language' => {'abbreviation'=>'fr', 'name'=>'French', 'greeting'=>'bonjour!', 'population'=>14000000, 'countries'=>'France, Canada, Belgium, Switzerland'},
|
109
|
+
'users' => [{'id' => '2', 'lang' => 'fr'}]
|
109
110
|
},
|
110
111
|
'de' => {
|
111
|
-
count
|
112
|
-
language
|
113
|
-
users
|
112
|
+
'count' => 1,
|
113
|
+
'language' => {'abbreviation'=>'de', 'name'=>'German', 'greeting'=>'hallo!', 'population'=>5000000, 'countries'=>'Germany, Austria, Switzerland, Belgium'},
|
114
|
+
'users' => [{'id' => '4', 'lang' => 'de'}]
|
114
115
|
}
|
115
116
|
}
|
116
117
|
end
|
@@ -129,26 +130,26 @@ describe AlohaAnalyzer::User do
|
|
129
130
|
end
|
130
131
|
|
131
132
|
it 'includes the total count' do
|
132
|
-
subject[
|
133
|
+
subject['count'].should eq 2
|
133
134
|
end
|
134
135
|
|
135
136
|
it 'includes the user lanugage' do
|
136
|
-
subject[
|
137
|
-
|
137
|
+
subject['account_language']['language'].should eq(
|
138
|
+
'abbreviation'=>'en', 'greeting' => 'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia'
|
138
139
|
)
|
139
140
|
end
|
140
141
|
|
141
142
|
it 'returns results based on the user language' do
|
142
|
-
subject[
|
143
|
-
|
144
|
-
|
145
|
-
|
143
|
+
subject['account_language'].should == {
|
144
|
+
'count' => 2,
|
145
|
+
'language' => {'abbreviation'=>'en', 'name'=>'English', 'population'=>238000000, 'countries' => 'USA, UK, Canada, Ireland, Australia', 'greeting'=>'hello!'},
|
146
|
+
'users' => [{'id' => '1', 'lang' => 'en'}, {'id' => '2', 'lang' => 'en'}]
|
146
147
|
}
|
147
148
|
end
|
148
149
|
|
149
150
|
it 'returns results results based on the non user language' do
|
150
|
-
subject[
|
151
|
-
subject[
|
151
|
+
subject['foreign_languages'].should == {}
|
152
|
+
subject['foreign_languages_count'].should eq 0
|
152
153
|
end
|
153
154
|
end
|
154
155
|
|
@@ -166,32 +167,32 @@ describe AlohaAnalyzer::User do
|
|
166
167
|
end
|
167
168
|
|
168
169
|
it 'includes the total count' do
|
169
|
-
subject[
|
170
|
+
subject['count'].should eq 3
|
170
171
|
end
|
171
172
|
|
172
173
|
it 'returns results based on the user language' do
|
173
|
-
subject[
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
subject['account_language'].should == {
|
175
|
+
'count' => 0,
|
176
|
+
'language' => {'abbreviation'=>'en', 'greeting' => 'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia'},
|
177
|
+
'users' => []
|
177
178
|
}
|
178
179
|
end
|
179
180
|
|
180
181
|
it 'includes the correct foreign_languages_count' do
|
181
|
-
subject[
|
182
|
+
subject['foreign_languages_count'].should eq 3
|
182
183
|
end
|
183
184
|
|
184
185
|
it 'returns results results based on the non user language' do
|
185
|
-
subject[
|
186
|
+
subject['foreign_languages'].should eq(
|
186
187
|
'fr' => {
|
187
|
-
|
188
|
-
|
189
|
-
|
188
|
+
'count' => 2,
|
189
|
+
'language' => { 'abbreviation'=>'fr', 'name'=>'French', 'greeting'=>'bonjour!', 'population'=>14000000, 'countries' => 'France, Canada, Belgium, Switzerland' },
|
190
|
+
'users' => [{'id' => '2', 'lang' => 'fr'}, {'id' => '3', 'lang' => 'fr'}]
|
190
191
|
},
|
191
192
|
'de' => {
|
192
|
-
|
193
|
-
|
194
|
-
|
193
|
+
'count' => 1,
|
194
|
+
'language' => {'abbreviation'=>'de', 'name'=>'German', 'greeting'=>'hallo!', 'population'=>5000000, 'countries' => 'Germany, Austria, Switzerland, Belgium' },
|
195
|
+
'users' => [{'id' => '1', 'lang' => 'de'}]
|
195
196
|
}
|
196
197
|
)
|
197
198
|
end
|
@@ -207,27 +208,27 @@ describe AlohaAnalyzer::User do
|
|
207
208
|
}
|
208
209
|
|
209
210
|
it 'includes the total count' do
|
210
|
-
subject[
|
211
|
+
subject['count'].should eq 3
|
211
212
|
end
|
212
213
|
|
213
214
|
it 'includes the user lanugage' do
|
214
|
-
subject[
|
215
|
-
|
216
|
-
|
217
|
-
|
215
|
+
subject['account_language'].should == {
|
216
|
+
'count' => 2,
|
217
|
+
'language' => { 'abbreviation'=>'en', 'greeting' => 'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia' },
|
218
|
+
'users' => [{'id' => '1', 'lang' => 'en'}, {'id' => '3', 'lang' => 'en'}]
|
218
219
|
}
|
219
220
|
end
|
220
221
|
|
221
222
|
it 'includes the correct foreign_languages_count' do
|
222
|
-
subject[
|
223
|
+
subject['foreign_languages_count'].should eq 1
|
223
224
|
end
|
224
225
|
|
225
226
|
it 'merges english and british' do
|
226
|
-
subject[
|
227
|
+
subject['foreign_languages'].should eq(
|
227
228
|
'fr' => {
|
228
|
-
|
229
|
-
|
230
|
-
|
229
|
+
'count' => 1,
|
230
|
+
'language' => {'abbreviation'=>'fr', 'name'=>'French', 'greeting'=>'bonjour!', 'population'=>14000000, 'countries' => 'France, Canada, Belgium, Switzerland'},
|
231
|
+
'users' => [{'id' => '2', 'lang' => 'fr'}]
|
231
232
|
}
|
232
233
|
)
|
233
234
|
end
|
@@ -235,7 +236,7 @@ describe AlohaAnalyzer::User do
|
|
235
236
|
end
|
236
237
|
|
237
238
|
context 'when user limit per language' do
|
238
|
-
let(:options) { { user_limit_per_language
|
239
|
+
let(:options) { { 'user_limit_per_language' => 1} }
|
239
240
|
let(:users) {
|
240
241
|
[
|
241
242
|
{'id' => '1', 'lang' => 'en'},
|
@@ -247,10 +248,63 @@ describe AlohaAnalyzer::User do
|
|
247
248
|
}
|
248
249
|
|
249
250
|
it 'does not add more users per language' do
|
250
|
-
subject[
|
251
|
-
subject[
|
252
|
-
subject[
|
253
|
-
subject[
|
251
|
+
subject['account_language']['users'].size.should eq 1
|
252
|
+
subject['account_language']['count'].should eq 2
|
253
|
+
subject['foreign_languages']['fr']['users'].size.should eq 1
|
254
|
+
subject['foreign_languages']['fr']['count'].should eq 3
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context 'when existing analysis' do
|
259
|
+
let(:users) {
|
260
|
+
[
|
261
|
+
{'id' => '4', 'lang' => 'en'},
|
262
|
+
{'id' => '5', 'lang' => 'fr'}
|
263
|
+
]
|
264
|
+
}
|
265
|
+
|
266
|
+
let(:analysis) do
|
267
|
+
{
|
268
|
+
'account_language' => {
|
269
|
+
'count' => 2,
|
270
|
+
'language' => { 'abbreviation'=>'en', 'greeting' => 'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia' },
|
271
|
+
'users' => [{'id' => '1', 'lang' => 'en'}, {'id' => '3', 'lang' => 'en'}]
|
272
|
+
},
|
273
|
+
'foreign_languages_count' => 1,
|
274
|
+
'count' => 3,
|
275
|
+
'foreign_languages' => {
|
276
|
+
'fr' => {
|
277
|
+
'count' => 1,
|
278
|
+
'language' => {'abbreviation'=>'fr', 'name'=>'French', 'greeting'=>'bonjour!', 'population'=>14000000, 'countries' => 'France, Canada, Belgium, Switzerland'},
|
279
|
+
'users' => [{'id' => '2', 'lang' => 'fr'}]
|
280
|
+
}
|
281
|
+
}
|
282
|
+
}
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'starts from the existing analysis' do
|
286
|
+
subject.should eq(
|
287
|
+
'account_language' => {
|
288
|
+
'count' => 3,
|
289
|
+
'language' => {'abbreviation'=>'en', 'greeting'=>'hello!', 'name'=>'English', 'population'=>238000000, 'countries'=>'USA, UK, Canada, Ireland, Australia'},
|
290
|
+
'users' => [{'id'=>'1', 'lang'=>'en'}, {'id'=>'3', 'lang'=>'en'}, {'id'=>'4', 'lang'=>'en'}]
|
291
|
+
},
|
292
|
+
'foreign_languages_count' => 2,
|
293
|
+
'count' => 5,
|
294
|
+
'foreign_languages' => {
|
295
|
+
'fr' => {
|
296
|
+
'count' => 2,
|
297
|
+
'language' => {
|
298
|
+
'abbreviation' => 'fr',
|
299
|
+
'name' => 'French',
|
300
|
+
'greeting' => 'bonjour!',
|
301
|
+
'population' => 14000000,
|
302
|
+
'countries' => 'France, Canada, Belgium, Switzerland'
|
303
|
+
},
|
304
|
+
'users' => [{'id'=>'2', 'lang'=>'fr'}, {'id'=>'5', 'lang'=>'fr'}]
|
305
|
+
}
|
306
|
+
}
|
307
|
+
)
|
254
308
|
end
|
255
309
|
end
|
256
310
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aloha_analyzer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthieu Aussaguel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|