okei 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +61 -0
  3. data/CHANGELOG.rdoc +13 -0
  4. data/LICENSE.rdoc +21 -0
  5. data/README.rdoc +91 -0
  6. data/Rakefile +52 -0
  7. data/app/controllers/okei/api/v1/responder.rb +30 -0
  8. data/app/controllers/okei/api/v1/units_controller.rb +41 -0
  9. data/app/models/okei/line.rb +63 -0
  10. data/app/models/okei/unit.rb +17 -0
  11. data/app/models/okei/word.rb +26 -0
  12. data/app/models/okei/words.rb +40 -0
  13. data/app/use_cases/okei/find_unit.rb +65 -0
  14. data/app/use_cases/okei/get_unit.rb +50 -0
  15. data/app/use_cases/okei/get_units.rb +45 -0
  16. data/app/views/okei/api/v1/units/_unit.json.jbuilder +6 -0
  17. data/app/views/okei/api/v1/units/errors.json.jbuilder +5 -0
  18. data/app/views/okei/api/v1/units/index.json.jbuilder +5 -0
  19. data/app/views/okei/api/v1/units/show.json.jbuilder +3 -0
  20. data/config/locales/ru.yml +12 -0
  21. data/config/routes.rb +12 -0
  22. data/db/migrate/20141004212500_create_okei_units.rb +20 -0
  23. data/db/migrate/20141004212501_populate_okei_units.rb +14 -0
  24. data/db/migrate/20141004212502_create_okei_words.rb +11 -0
  25. data/db/migrate/20141004212503_populate_okei_words.rb +15 -0
  26. data/db/seeds/units.json +4534 -0
  27. data/db/seeds/words.json +2462 -0
  28. data/lib/okei.rb +7 -0
  29. data/lib/okei/engine.rb +12 -0
  30. data/lib/okei/version.rb +4 -0
  31. data/lib/tasks/okei_tasks.rake +56 -0
  32. data/spec/controllers/okei/api/v1/responder_spec.rb +69 -0
  33. data/spec/controllers/okei/api/v1/units_controller_spec.rb +88 -0
  34. data/spec/dummy/README.rdoc +28 -0
  35. data/spec/dummy/Rakefile +6 -0
  36. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  37. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  38. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  39. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy/app/views/layouts/application.html.erb +13 -0
  41. data/spec/dummy/bin/bundle +3 -0
  42. data/spec/dummy/bin/rails +4 -0
  43. data/spec/dummy/bin/rake +4 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +29 -0
  46. data/spec/dummy/config/boot.rb +5 -0
  47. data/spec/dummy/config/database.yml +25 -0
  48. data/spec/dummy/config/environment.rb +5 -0
  49. data/spec/dummy/config/environments/development.rb +37 -0
  50. data/spec/dummy/config/environments/production.rb +78 -0
  51. data/spec/dummy/config/environments/test.rb +39 -0
  52. data/spec/dummy/config/initializers/assets.rb +8 -0
  53. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/spec/dummy/config/initializers/inflections.rb +16 -0
  57. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  58. data/spec/dummy/config/initializers/session_store.rb +3 -0
  59. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  60. data/spec/dummy/config/locales/en.yml +23 -0
  61. data/spec/dummy/config/routes.rb +4 -0
  62. data/spec/dummy/config/secrets.yml +22 -0
  63. data/spec/dummy/db/schema.rb +39 -0
  64. data/spec/dummy/db/test.sqlite3 +0 -0
  65. data/spec/dummy/log/test.log +49831 -0
  66. data/spec/dummy/public/404.html +67 -0
  67. data/spec/dummy/public/422.html +67 -0
  68. data/spec/dummy/public/500.html +66 -0
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/examples/json_schemas/error.json +14 -0
  71. data/spec/examples/json_schemas/get_errors.json +18 -0
  72. data/spec/examples/json_schemas/get_unit.json +13 -0
  73. data/spec/examples/json_schemas/get_units.json +17 -0
  74. data/spec/examples/json_schemas/unit.json +20 -0
  75. data/spec/factories/units.rb +18 -0
  76. data/spec/factories/words.rb +7 -0
  77. data/spec/models/okei/line_spec.rb +61 -0
  78. data/spec/models/okei/unit_spec.rb +170 -0
  79. data/spec/models/okei/word_spec.rb +75 -0
  80. data/spec/models/okei/words_spec.rb +64 -0
  81. data/spec/requests/okei/api/v1/find_unit_spec.rb +65 -0
  82. data/spec/requests/okei/api/v1/get_unit_spec.rb +57 -0
  83. data/spec/requests/okei/api/v1/get_units_spec.rb +64 -0
  84. data/spec/routing/okei/api/v1/units_routing_spec.rb +56 -0
  85. data/spec/spec_helper.rb +9 -0
  86. data/spec/support/initializers/caching.rb +20 -0
  87. data/spec/support/initializers/coveralls.rb +4 -0
  88. data/spec/support/initializers/database_cleaner.rb +24 -0
  89. data/spec/support/initializers/factory_girl.rb +6 -0
  90. data/spec/support/initializers/factory_girl_rails.rb +1 -0
  91. data/spec/support/initializers/focus.rb +5 -0
  92. data/spec/support/initializers/garbage_collection.rb +11 -0
  93. data/spec/support/initializers/i18n.rb +1 -0
  94. data/spec/support/initializers/migrations.rb +3 -0
  95. data/spec/support/initializers/rails.rb +6 -0
  96. data/spec/support/initializers/random_order.rb +4 -0
  97. data/spec/support/initializers/rspec.rb +10 -0
  98. data/spec/support/initializers/timecop.rb +1 -0
  99. data/spec/support/matchers/controllers.rb +14 -0
  100. data/spec/support/matchers/json_schema.rb +10 -0
  101. data/spec/use_cases/okei/find_unit_spec.rb +77 -0
  102. data/spec/use_cases/okei/get_unit_spec.rb +64 -0
  103. data/spec/use_cases/okei/get_units_spec.rb +65 -0
  104. data/spec/views/okei/api/v1/units/_unit.json_spec.rb +24 -0
  105. metadata +400 -0
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe Word do
6
+
7
+ describe "#synonym=" do
8
+
9
+ subject(:word) { Word.new }
10
+
11
+ it "sets #synonym" do
12
+ value = "МЕТР"
13
+ expect { word.synonym = value }.to change { word.synonym }.to value
14
+ end
15
+ end
16
+
17
+ describe "#text=" do
18
+
19
+ subject(:word) { Word.new }
20
+
21
+ it "sets #text" do
22
+ value = "М"
23
+ expect { word.text = value }.to change { word.text }.to value
24
+ end
25
+ end
26
+
27
+ describe "#valid?" do
28
+
29
+ subject(:word) { build :word }
30
+
31
+ before { expect(word).to be_valid }
32
+
33
+ it "fails when #synonym is absent" do
34
+ word.synonym = ""
35
+ expect(word).not_to be_valid
36
+ end
37
+
38
+ it "fails when #synonym is taken" do
39
+ create :word, synonym: word.synonym
40
+ expect(word).not_to be_valid
41
+ end
42
+
43
+ it "fails when #text is absent" do
44
+ word.text = ""
45
+ expect(word).not_to be_valid
46
+ end
47
+
48
+ it "passes when #text is taken" do
49
+ create :word, text: word.text
50
+ expect(word).to be_valid
51
+ end
52
+ end
53
+
54
+ describe ".translate" do
55
+
56
+ let!(:line) { "ТЕКСТ" }
57
+
58
+ context "when word exists" do
59
+
60
+ let!(:word) { create :word, synonym: line }
61
+
62
+ it "translates line" do
63
+ expect(Word.translate line).to eq word.text
64
+ end
65
+ end
66
+
67
+ context "when word is absent" do
68
+
69
+ it "returns the source" do
70
+ expect(Word.translate line).to eq line
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe Words do
6
+
7
+ describe ".new" do
8
+
9
+ RSpec::Matchers.define :have_words do |list|
10
+ match do |text|
11
+ expect(Words.new text).to eq list
12
+ end
13
+ failure_message do |text|
14
+ "#{ text } should have words #{ list }." \
15
+ " #{ Words.new text } given."
16
+ end
17
+ end
18
+
19
+ it "returns list of words" do
20
+ expect(Words.new).to be_kind_of Array
21
+ end
22
+
23
+ specify { expect("УСЛ МЕТР/СЕК^2").to have_words %w(УСЛ МЕТР / СЕК ^ 2) }
24
+ specify { expect("100 М").to have_words %w(100 М) }
25
+ specify { expect("2ШТ").to have_words %w(2 ШТ) }
26
+ specify { expect("1000КМ").to have_words %w(1000 КМ) }
27
+ specify { expect("0.1КМ").to have_words %w(0.1 КМ) }
28
+ specify { expect("М1").to have_words %w(М 1) }
29
+ specify { expect("М.1").to have_words %w(М 1) }
30
+ specify { expect("М4").to have_words %w(М 4) }
31
+ specify { expect("М.4").to have_words %w(М 4) }
32
+ specify { expect("ТЫС. Ч").to have_words %w(ТЫС Ч) }
33
+ specify { expect("ТЫС. Ч.").to have_words %w(ТЫС Ч) }
34
+ specify { expect(".ТЫС .Ч").to have_words %w(ТЫС Ч) }
35
+ specify { expect("90%").to have_words %w(90 %) }
36
+ end
37
+
38
+ describe "#map" do
39
+
40
+ subject(:words) { Words.new("M") }
41
+
42
+ it "should return a Words object" do
43
+ expect(subject.map { |word| word }).to be_kind_of Words
44
+ end
45
+ end
46
+
47
+ describe "#to_s" do
48
+
49
+ RSpec::Matchers.define :shape_into do |text|
50
+ match do |words|
51
+ expect(Words.new(words).to_s).to eq text
52
+ end
53
+ failure_message do |words|
54
+ "'#{ words }' should shape into '#{ text }'." \
55
+ " '#{ Words.new(words) }' given."
56
+ end
57
+ end
58
+
59
+ specify { expect("К ^ М ^ 2").to shape_into "КМ2" }
60
+ specify { expect("КМ / С").to shape_into "КМ/С" }
61
+ specify { expect("КИЛОМ").to shape_into "КИЛОМ" }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe "Find unit" do
6
+
7
+ # ==========================================================================
8
+ # Prepare context
9
+ # ==========================================================================
10
+
11
+ let!(:unit) { create :unit, code: "ТЫС ЧЕЛ-ЧАС" }
12
+ before do
13
+ {
14
+ "1000" => "ТЫС",
15
+ "ЧЕЛОВЕКО" => "ЧЕЛ",
16
+ "ЧАСОВ" => "ЧАС",
17
+ "ТЫС ЧЕЛ ЧАС" => "ТЫС ЧЕЛ-ЧАС"
18
+ }.each { |synonym, text| create :word, synonym: synonym, text: text }
19
+ end
20
+
21
+ # ==========================================================================
22
+ # Prepare variables
23
+ # ==========================================================================
24
+
25
+ def call_request(text)
26
+ get "okei/api/v1/units/search", text: text
27
+ end
28
+
29
+ # ==========================================================================
30
+ # Run tests
31
+ # ==========================================================================
32
+
33
+ context "with proper text" do
34
+
35
+ before { call_request "1000 человеко-часов" }
36
+
37
+ it "responds with 200" do
38
+ expect(response.status).to eq 200
39
+ end
40
+
41
+ it "responds with proper json" do
42
+ expect(response.body).to match_json_schema(:get_unit)
43
+ end
44
+
45
+ it "responds with requested unit" do
46
+ received = JSON.parse(response.body)["unit"]
47
+ %w(uuid name code num int_code base factor measure)
48
+ .each { |key| expect(received[key]).to eq unit.send(key) }
49
+ end
50
+ end
51
+
52
+ context "with wrong text" do
53
+
54
+ before { call_request "wrong" }
55
+
56
+ it "responds with 404" do
57
+ expect(response.status).to eq 404
58
+ end
59
+
60
+ it "responds with proper json" do
61
+ expect(response.body).to match_json_schema(:get_errors)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe "Getting unit" do
6
+
7
+ # ==========================================================================
8
+ # Prepare environment
9
+ # ==========================================================================
10
+
11
+ let!(:unit) { create :unit }
12
+
13
+ # ==========================================================================
14
+ # Prepare variables
15
+ # ==========================================================================
16
+
17
+ def call_request(uuid)
18
+ get "okei/api/v1/units/#{ uuid }"
19
+ end
20
+
21
+ # ==========================================================================
22
+ # Run tests
23
+ # ==========================================================================
24
+
25
+ context "with proper uuid" do
26
+
27
+ before { call_request unit.uuid }
28
+
29
+ it "responds with 200" do
30
+ expect(response.status).to eq 200
31
+ end
32
+
33
+ it "responds with proper json" do
34
+ expect(response.body).to match_json_schema(:get_unit)
35
+ end
36
+
37
+ it "responds with requested unit" do
38
+ received = JSON.parse(response.body)["unit"]
39
+ %w(uuid name code num int_code base factor measure)
40
+ .each { |key| expect(received[key]).to eq unit.send(key) }
41
+ end
42
+ end
43
+
44
+ context "with wrong uuid" do
45
+
46
+ before { call_request "00000000-0000-0000-0000-000000000000" }
47
+
48
+ it "responds with 404" do
49
+ expect(response.status).to eq 404
50
+ end
51
+
52
+ it "responds with proper json" do
53
+ expect(response.body).to match_json_schema(:get_errors)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe "GET api/v1/units" do
6
+
7
+ # ==========================================================================
8
+ # Prepare environment
9
+ # ==========================================================================
10
+
11
+ let!(:nmile) { create :unit, code: "МИЛЬ", measure: "ДЛИНА" }
12
+ let!(:mole) { create :unit, code: "МОЛЬ", measure: "ПРОЧИЕ" }
13
+
14
+ # ==========================================================================
15
+ # Prepare variables
16
+ # ==========================================================================
17
+
18
+ def call_request(params = {})
19
+ get "okei/api/v1/units", params
20
+ end
21
+
22
+ let(:uuids) do
23
+ JSON.parse(response.body)["units"].map { |unit| unit["uuid"] }
24
+ end
25
+
26
+ # ==========================================================================
27
+ # Run tests
28
+ # ==========================================================================
29
+
30
+ context "without params" do
31
+
32
+ before { call_request }
33
+
34
+ it "responds with 200" do
35
+ expect(response.status).to eq 200
36
+ end
37
+
38
+ it "receives proper json" do
39
+ expect(response.body).to match_json_schema(:get_units)
40
+ end
41
+
42
+ it "receivesll units" do
43
+ expect(uuids).to eq [nmile.uuid, mole.uuid]
44
+ end
45
+ end
46
+
47
+ context "with measure" do
48
+
49
+ before { call_request "measure" => "ДЛИНА" }
50
+
51
+ it "responds with 200" do
52
+ expect(response.status).to eq 200
53
+ end
54
+
55
+ it "receives proper json" do
56
+ expect(response.body).to match_json_schema(:get_units)
57
+ end
58
+
59
+ it "receives matched units" do
60
+ expect(uuids).to eq [nmile.uuid]
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Okei
5
+ describe "units" do
6
+
7
+ routes { Okei::Engine.routes }
8
+
9
+ let(:root) { "api/v1/units" }
10
+
11
+ describe "#index" do
12
+
13
+ let(:action) do
14
+ { controller: "okei/#{ root }", action: "index" }
15
+ end
16
+
17
+ subject(:request) { { get: "/#{ root }" } }
18
+
19
+ it "is routable in json" do
20
+ expect(request).to route_to action.merge(format: "json")
21
+ end
22
+ end
23
+
24
+ describe "#show" do
25
+
26
+ let(:action) do
27
+ {
28
+ controller: "okei/#{ root }",
29
+ action: "show",
30
+ uuid: "f5f944e1-66c1-4432-82bb-5f8235dcd5d7"
31
+ }
32
+ end
33
+
34
+ subject(:request) do
35
+ { get: "/#{ root }/f5f944e1-66c1-4432-82bb-5f8235dcd5d7" }
36
+ end
37
+
38
+ it "is routable in json" do
39
+ expect(request).to route_to action.merge(format: "json")
40
+ end
41
+ end
42
+
43
+ describe "#find" do
44
+
45
+ let(:action) do
46
+ { controller: "okei/#{ root }", action: "search" }
47
+ end
48
+
49
+ subject(:request) { { get: "/#{ root }/search" } }
50
+
51
+ it "is routable in json" do
52
+ expect(request).to route_to action.merge(format: "json")
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ ENV["RAILS_ENV"] ||= "test"
2
+
3
+ root = File.dirname File.dirname(__FILE__)
4
+
5
+ # Application files
6
+ require File.expand_path("../dummy/config/environment", __FILE__)
7
+
8
+ # Support files and Rspec settings
9
+ Dir[File.join(root, "spec/support/**/*.rb")].each { |file| require file }
@@ -0,0 +1,20 @@
1
+ require "rails"
2
+ require "rspec/rails"
3
+
4
+ RSpec.configure do |config|
5
+
6
+ config.around(:each, :caching) do |example|
7
+ caching = ActionController::Base.perform_caching
8
+ ActionController::Base.perform_caching = example.metadata[:caching]
9
+ example.run
10
+ ActionController::Base.perform_caching = caching
11
+ end
12
+
13
+ config.before(:each) do
14
+ Rails.cache.clear
15
+ end
16
+
17
+ config.after(:all) do
18
+ Rails.cache.clear
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # Check test coverage
2
+ require "coveralls"
3
+
4
+ Coveralls.wear!
@@ -0,0 +1,24 @@
1
+ require "database_cleaner"
2
+ require "rspec/rails"
3
+
4
+ RSpec.configure do |config|
5
+
6
+ # stop wrapping each of test examples within a transaction
7
+ config.use_transactional_fixtures = false
8
+
9
+ config.before :suite do
10
+ DatabaseCleaner.clean_with :truncation
11
+ end
12
+
13
+ config.before :each do
14
+ DatabaseCleaner.strategy = :transaction
15
+ end
16
+
17
+ config.before :each do
18
+ DatabaseCleaner.start
19
+ end
20
+
21
+ config.after :each do
22
+ DatabaseCleaner.clean
23
+ end
24
+ end