aloha_analyzer 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8468fe4dfc4cac6cf908816e029246531cf74b09
4
- data.tar.gz: 437fcf53fbd6cb145b19fce4956b6bf7b8fd5dac
3
+ metadata.gz: c390df22ade9fd5d5fa60f666546be93d834bb9a
4
+ data.tar.gz: 1ca0491eed0055d5fecb8797926d4e81b9f1bf9c
5
5
  SHA512:
6
- metadata.gz: 4367d362dfa777726cffe20f70f925356bf79eec7bc55ed743da748520c52a95e8c4415e0389b9b1a986101415a32fb5d8a799e070e75bf1b528159f5d71fd1d
7
- data.tar.gz: 932bd15e86a4c8e702db5cdc0dd87aba576573d6a530e4af95cc79a57aa484e21545975e0eb87ae0f06a02477b22561132b1e08dc93f134d67d2197432ca49ed
6
+ metadata.gz: de8a6d1dcbca9b2d4b4b3c6f89bc6b82ee9d97c2d71c57e4a7dfacea77afd9b16a972a882f384ce0b902b935462ddf5c1b351a55e8c05bcfad47a3e8dfb1b35a
7
+ data.tar.gz: 12158be8ccded5051441322d8e2dcf5253f5e181c9977bb38a6dde54c335dc39e81f0f9f8affba9170a087666e2bffa33a9a120e076409af7374c1a1a242eec1
@@ -3,52 +3,74 @@ module AlohaAnalyzer
3
3
 
4
4
  attr_reader :language
5
5
 
6
- def initialize(language, users)
6
+ def initialize(language, users, options = {})
7
7
  @language = language.downcase
8
8
  @users = users
9
9
  @users_count = users.size
10
+ @options = options
11
+ @analysis = {}
12
+
10
13
  clean_language!
11
14
  clean_users_languages!
12
15
  end
13
16
 
14
17
  def analyze
15
- boilerplate_analysis.tap do |analysys|
16
- @users.each do |user|
17
- abbreviation = user['lang']
18
- if abbreviation == @language
19
- analysys[:account_language][:count] += 1
20
- analysys[:account_language][:users].push user
21
- else
22
- if analysys[:foreign_languages][abbreviation]
23
- analysys[:foreign_languages][abbreviation][:count] += 1
24
- analysys[:foreign_languages][abbreviation][:users].push user
25
- else
26
- analysys[:foreign_languages][abbreviation] = {
27
- :count => 1,
28
- :language => Language.find_by_abbreviation(abbreviation),
29
- :users => [user]
30
- }
31
- end
32
- analysys[:foreign_languages_count] += 1
33
- end
34
- analysys[:count] += 1
18
+ prepare!
19
+ @users.each do |user|
20
+ if user['lang'] == @language
21
+ add_account_language_user(user)
22
+ @analysis[:account_language][:count] += 1
23
+ else
24
+ add_foreign_language_user(user)
25
+ @analysis[:foreign_languages_count] += 1
35
26
  end
27
+ @analysis[:count] += 1
36
28
  end
29
+ @analysis
37
30
  end
38
31
 
39
-
40
32
  private
41
33
 
42
- def boilerplate_analysis
43
- Hash.new.tap do |analysys|
44
- analysys[:account_language] = {
45
- count: 0,
46
- language: Language.find_by_abbreviation(@language),
47
- users: []
34
+ def add_account_language_user(user)
35
+ unless too_many_users?(@analysis[:account_language][:users])
36
+ @analysis[:account_language][:users].push(user)
37
+ end
38
+ end
39
+
40
+ def add_foreign_language_user(user)
41
+ prepare_foreign_language(user['lang'])
42
+ @analysis[:foreign_languages][user['lang']][:count] += 1
43
+ unless too_many_users?(@analysis[:foreign_languages][user['lang']][:users])
44
+ @analysis[:foreign_languages][user['lang']][:users].push(user)
45
+ end
46
+ end
47
+
48
+ def prepare_foreign_language(abbreviation)
49
+ if @analysis[:foreign_languages][abbreviation].nil?
50
+ @analysis[:foreign_languages][abbreviation] = {
51
+ :count => 0,
52
+ :language => Language.find_by_abbreviation(abbreviation),
53
+ :users => []
48
54
  }
49
- analysys[:foreign_languages_count] = 0
50
- analysys[:count] = 0
51
- analysys[:foreign_languages] = Hash.new
55
+ end
56
+ end
57
+
58
+ def prepare!
59
+ @analysis[:account_language] = {
60
+ count: 0,
61
+ language: Language.find_by_abbreviation(@language),
62
+ users: []
63
+ }
64
+ @analysis[:foreign_languages_count] = 0
65
+ @analysis[:count] = 0
66
+ @analysis[:foreign_languages] = Hash.new
67
+ end
68
+
69
+ def too_many_users?(users)
70
+ if @options[:user_limit_per_language] && users.size >= @options[:user_limit_per_language]
71
+ true
72
+ else
73
+ false
52
74
  end
53
75
  end
54
76
 
@@ -1,3 +1,3 @@
1
1
  module AlohaAnalyzer
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,8 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AlohaAnalyzer::User do
4
- subject(:user) { described_class.new(language, users) }
4
+ subject(:user) { described_class.new(language, users, options) }
5
5
  let(:language) { 'en' }
6
+ let(:options) { {} }
6
7
 
7
8
  describe '#new' do
8
9
  let(:users) { [] }
@@ -40,7 +41,7 @@ describe AlohaAnalyzer::User do
40
41
  end
41
42
 
42
43
  describe '#analyze' do
43
- subject(:analyze) { described_class.new(language, users).analyze }
44
+ subject(:analyze) { described_class.new(language, users, options).analyze }
44
45
  context 'when no users' do
45
46
  let(:users) { [] }
46
47
 
@@ -232,5 +233,25 @@ describe AlohaAnalyzer::User do
232
233
  end
233
234
  end
234
235
  end
236
+
237
+ context 'when user limit per language' do
238
+ let(:options) { { user_limit_per_language: 1} }
239
+ let(:users) {
240
+ [
241
+ {'id' => '1', 'lang' => 'en'},
242
+ {'id' => '2', 'lang' => 'fr'},
243
+ {'id' => '3', 'lang' => 'en'},
244
+ {'id' => '4', 'lang' => 'fr'},
245
+ {'id' => '5', 'lang' => 'fr'}
246
+ ]
247
+ }
248
+
249
+ it 'does not add more users per language' do
250
+ subject[:account_language][:users].size.should eq 1
251
+ subject[:account_language][:count].should eq 2
252
+ subject[:foreign_languages]['fr'][:users].size.should eq 1
253
+ subject[:foreign_languages]['fr'][:count].should eq 3
254
+ end
255
+ end
235
256
  end
236
257
  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.1.9
4
+ version: 0.2.0
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-13 00:00:00.000000000 Z
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler