metal_archives 0.8.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +59 -7
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +14 -0
  5. data/.travis.yml +11 -0
  6. data/Gemfile +2 -0
  7. data/{LICENSE → LICENSE.md} +0 -0
  8. data/README.md +77 -9
  9. data/Rakefile +5 -3
  10. data/lib/metal_archives.rb +8 -0
  11. data/lib/metal_archives/configuration.rb +28 -7
  12. data/lib/metal_archives/error.rb +37 -30
  13. data/lib/metal_archives/http_client.rb +21 -42
  14. data/lib/metal_archives/middleware/headers.rb +38 -0
  15. data/lib/metal_archives/middleware/rewrite_endpoint.rb +38 -0
  16. data/lib/metal_archives/models/artist.rb +51 -65
  17. data/lib/metal_archives/models/band.rb +41 -39
  18. data/lib/metal_archives/models/base_model.rb +88 -59
  19. data/lib/metal_archives/models/label.rb +7 -6
  20. data/lib/metal_archives/parsers/artist.rb +110 -99
  21. data/lib/metal_archives/parsers/band.rb +168 -156
  22. data/lib/metal_archives/parsers/label.rb +54 -52
  23. data/lib/metal_archives/parsers/parser.rb +73 -71
  24. data/lib/metal_archives/utils/collection.rb +7 -1
  25. data/lib/metal_archives/utils/lru_cache.rb +11 -4
  26. data/lib/metal_archives/utils/nil_date.rb +54 -0
  27. data/lib/metal_archives/utils/range.rb +16 -8
  28. data/lib/metal_archives/version.rb +3 -1
  29. data/metal_archives.gemspec +21 -11
  30. data/spec/configuration_spec.rb +101 -0
  31. data/spec/factories/artist_factory.rb +37 -0
  32. data/spec/factories/band_factory.rb +60 -0
  33. data/spec/factories/nil_date_factory.rb +9 -0
  34. data/spec/factories/range_factory.rb +8 -0
  35. data/spec/models/artist_spec.rb +142 -0
  36. data/spec/models/band_spec.rb +179 -0
  37. data/spec/models/base_model_spec.rb +217 -0
  38. data/spec/parser_spec.rb +19 -0
  39. data/spec/spec_helper.rb +111 -0
  40. data/spec/support/factory_girl.rb +5 -0
  41. data/spec/support/metal_archives.rb +26 -0
  42. data/spec/utils/collection_spec.rb +72 -0
  43. data/spec/utils/lru_cache_spec.rb +53 -0
  44. data/spec/utils/nil_date_spec.rb +98 -0
  45. data/spec/utils/range_spec.rb +62 -0
  46. metadata +142 -57
  47. data/test/base_model_test.rb +0 -111
  48. data/test/configuration_test.rb +0 -57
  49. data/test/parser_test.rb +0 -37
  50. data/test/property/artist_property_test.rb +0 -43
  51. data/test/property/band_property_test.rb +0 -94
  52. data/test/query/artist_query_test.rb +0 -109
  53. data/test/query/band_query_test.rb +0 -152
  54. data/test/test_helper.rb +0 -25
  55. data/test/utils/collection_test.rb +0 -51
  56. data/test/utils/lru_cache_test.rb +0 -22
  57. data/test/utils/range_test.rb +0 -42
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe MetalArchives::Parsers::Parser do
4
+ it 'parses countries' do
5
+ expect(described_class.parse_country('United States')).to eq ISO3166::Country['US']
6
+ expect(described_class.parse_country('Germany')).to eq ISO3166::Country['DE']
7
+ expect(described_class.parse_country('Belgium')).to eq ISO3166::Country['BE']
8
+ end
9
+
10
+ it 'parses genres' do
11
+ expect(described_class.parse_genre('Death, Power, Black')).to match_array ['Black', 'Death', 'Power']
12
+ expect(described_class.parse_genre('Death, Power, Black')).to match_array ['Black', 'Death', 'Power']
13
+ expect(described_class.parse_genre('Death (early), Heavy/Power Metal, Black (later)')).to match_array ['Black', 'Death', 'Heavy', 'Power']
14
+ expect(described_class.parse_genre(' Death , Power Metal, Power, Power')).to match_array ['Death', 'Power']
15
+ expect(described_class.parse_genre('Heavy/Speed Power Metal')).to match_array ['Heavy Power', 'Speed Power']
16
+ expect(described_class.parse_genre('Traditional Heavy/Power Metal')).to match_array ['Traditional Heavy', 'Traditional Power']
17
+ expect(described_class.parse_genre('Traditional/Classical Heavy/Power Metal')).to match_array ['Traditional Heavy', 'Traditional Power', 'Classical Heavy', 'Classical Power']
18
+ end
19
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+
19
+ require 'factory_girl'
20
+ require 'faker'
21
+ require 'coveralls'
22
+ Coveralls.wear!
23
+
24
+ require 'metal_archives'
25
+
26
+ Dir[File.join(__dir__, 'support/**/*.rb')].each { |f| require f }
27
+ Dir[File.join(__dir__, 'factories/**/*.rb')].each { |f| require f }
28
+
29
+ RSpec.configure do |config|
30
+ # rspec-expectations config goes here. You can use an alternate
31
+ # assertion/expectation library such as wrong or the stdlib/minitest
32
+ # assertions if you prefer.
33
+ config.expect_with :rspec do |expectations|
34
+ # This option will default to `true` in RSpec 4. It makes the `description`
35
+ # and `failure_message` of custom matchers include text for helper methods
36
+ # defined using `chain`, e.g.:
37
+ # be_bigger_than(2).and_smaller_than(4).description
38
+ # # => "be bigger than 2 and smaller than 4"
39
+ # ...rather than:
40
+ # # => "be bigger than 2"
41
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
42
+ end
43
+
44
+ # rspec-mocks config goes here. You can use an alternate test double
45
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
46
+ config.mock_with :rspec do |mocks|
47
+ # Prevents you from mocking or stubbing a method that does not exist on
48
+ # a real object. This is generally recommended, and will default to
49
+ # `true` in RSpec 4.
50
+ mocks.verify_partial_doubles = true
51
+ end
52
+
53
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
54
+ # have no way to turn it off -- the option exists only for backwards
55
+ # compatibility in RSpec 3). It causes shared context metadata to be
56
+ # inherited by the metadata hash of host groups and examples, rather than
57
+ # triggering implicit auto-inclusion in groups with matching metadata.
58
+ config.shared_context_metadata_behavior = :apply_to_host_groups
59
+
60
+ # The settings below are suggested to provide a good initial experience
61
+ # with RSpec, but feel free to customize to your heart's content.
62
+ # # This allows you to limit a spec run to individual examples or groups
63
+ # # you care about by tagging them with `:focus` metadata. When nothing
64
+ # # is tagged with `:focus`, all examples get run. RSpec also provides
65
+ # # aliases for `it`, `describe`, and `context` that include `:focus`
66
+ # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
67
+ # config.filter_run_when_matching :focus
68
+ #
69
+ # # Allows RSpec to persist some state between runs in order to support
70
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
71
+ # # you configure your source control system to ignore this file.
72
+ # config.example_status_persistence_file_path = "spec/examples.txt"
73
+ #
74
+ # # Limits the available syntax to the non-monkey patched syntax that is
75
+ # # recommended. For more details, see:
76
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
77
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
78
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
79
+ # config.disable_monkey_patching!
80
+ #
81
+ # # This setting enables warnings. It's recommended, but in some cases may
82
+ # # be too noisy due to issues in dependencies.
83
+ # config.warnings = true
84
+ #
85
+ # # Many RSpec users commonly either run the entire suite or an individual
86
+ # # file, and it's useful to allow more verbose output when running an
87
+ # # individual spec file.
88
+ # if config.files_to_run.one?
89
+ # # Use the documentation formatter for detailed output,
90
+ # # unless a formatter has already been configured
91
+ # # (e.g. via a command-line flag).
92
+ # config.default_formatter = "doc"
93
+ # end
94
+ #
95
+ # # Print the 10 slowest examples and example groups at the
96
+ # # end of the spec run, to help surface which specs are running
97
+ # # particularly slow.
98
+ # config.profile_examples = 10
99
+ #
100
+ # # Run specs in random order to surface order dependencies. If you find an
101
+ # # order dependency and want to debug it, you can fix the order by providing
102
+ # # the seed, which is printed after each run.
103
+ # # --seed 1234
104
+ # config.order = :random
105
+ #
106
+ # # Seed global randomization in this process using the `--seed` CLI option.
107
+ # # Setting this allows you to use `--seed` to deterministically reproduce
108
+ # # test failures related to randomization by passing the same `--seed` value
109
+ # # as the one that triggered the failure.
110
+ # Kernel.srand config.seed
111
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.configure do |config|
4
+ config.include FactoryGirl::Syntax::Methods
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'metal_archives'
4
+
5
+ MetalArchives.configure do |c|
6
+ ## Application identity (required)
7
+ c.app_name = 'MetalArchivesGemTestSuite'
8
+ c.app_version = MetalArchives::VERSION
9
+ c.app_contact = 'user@example.com'
10
+
11
+ ## Custom cache size per object class (optional, overrides defaults)
12
+ # c.cache_size = 100
13
+
14
+ ## Metal Archives endpoint (optional, overrides default)
15
+ c.endpoint = ENV['MA_ENDPOINT'] if ENV['MA_ENDPOINT']
16
+
17
+ if ENV['MA_ENDPOINT']
18
+ puts "Using '#{ENV['MA_ENDPOINT']}' as MA endpoint"
19
+ end
20
+
21
+ ## Custom logger (optional)
22
+ # c.logger = Logger.new 'metal_archives.log'
23
+
24
+ ## Verbose output
25
+ # c.debug = false
26
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe MetalArchives::Collection do
4
+ it 'iterates' do
5
+ l = lambda do
6
+ @i ||= 0
7
+
8
+ next [] if @i >= 100
9
+ items = (@i .. (@i + 9)).to_a
10
+ @i += 10
11
+
12
+ items
13
+ end
14
+
15
+ c = MetalArchives::Collection.new l
16
+
17
+ i = 0
18
+ c.each do |item|
19
+ expect(item).to eq i
20
+ i += 1
21
+ end
22
+ end
23
+
24
+ it 'returns' do
25
+ l = lambda do
26
+ @i ||= 0
27
+
28
+ raise StandardError if @i >= 100
29
+ items = (@i .. (@i + 9)).to_a
30
+ @i += 10
31
+
32
+ items
33
+ end
34
+
35
+ c = MetalArchives::Collection.new l
36
+
37
+ i = 0
38
+ c.each do |item|
39
+ break if i == 99
40
+ expect(item).to eq i
41
+ i += 1
42
+ end
43
+
44
+ expect(i).to eq 99
45
+ end
46
+
47
+ describe 'empty?' do
48
+ it 'is not empty' do
49
+ l = lambda do
50
+ @i ||= 0
51
+
52
+ next [] if @i >= 100
53
+ items = (@i .. (@i + 9)).to_a
54
+ @i += 10
55
+
56
+ items
57
+ end
58
+
59
+ c = MetalArchives::Collection.new l
60
+
61
+ i = 0
62
+
63
+ expect(c).not_to be_empty
64
+ end
65
+
66
+ it 'is empty' do
67
+ c = MetalArchives::Collection.new -> { [] }
68
+
69
+ expect(c).to be_empty
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe MetalArchives::LRUCache do
4
+ let(:cache) { MetalArchives::LRUCache.new 3 }
5
+
6
+ it 'stores and retrieves objects' do
7
+ cache[:key] = 'MyString'
8
+
9
+ expect(cache[:key]).to eq 'MyString'
10
+ end
11
+
12
+ it 'clears cache' do
13
+ cache[:key] = 'MyString'
14
+ cache.clear
15
+
16
+ expect(cache[:key]).to be_nil
17
+ end
18
+
19
+ it 'peeks' do
20
+ expect(cache).not_to include :key
21
+
22
+ cache[:key] = 'MyString'
23
+
24
+ expect(cache).to include :key
25
+ end
26
+
27
+ it 'deletes' do
28
+ cache[:key] = 'MyString'
29
+
30
+ expect(cache).to include :key
31
+
32
+ cache.delete :key
33
+
34
+ expect(cache).not_to include :key
35
+ end
36
+
37
+ it 'implements LRU caching' do
38
+ cache[:a] = 'one'
39
+ cache[:b] = 'two'
40
+ cache[:c] = 'three'
41
+
42
+ expect(cache.instance_variable_get '@size').to eq 3
43
+
44
+ cache[:d] = 'four'
45
+
46
+ expect(cache.instance_variable_get '@size').to eq 3
47
+
48
+ expect(cache[:b]).to eq 'two'
49
+ expect(cache[:c]).to eq 'three'
50
+ expect(cache[:d]).to eq 'four'
51
+ expect(cache[:a]).to be_nil
52
+ end
53
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe MetalArchives::NilDate do
4
+ let(:date) { build :nil_date }
5
+ let(:date_without_day) { build :nil_date, :day => nil }
6
+ let(:date_without_month) { build :nil_date, :month => nil }
7
+
8
+ it 'has the correct properties' do
9
+ expect(date).to respond_to :year
10
+ expect(date).to respond_to :year=
11
+ expect(date).to respond_to :year?
12
+
13
+ expect(date).to respond_to :month
14
+ expect(date).to respond_to :month=
15
+ expect(date).to respond_to :month?
16
+
17
+ expect(date).to respond_to :day
18
+ expect(date).to respond_to :day=
19
+ expect(date).to respond_to :day?
20
+ end
21
+
22
+ it 'returns the correct values' do
23
+ expect(date.year).not_to be_nil
24
+ expect(date.year).to be_an Integer
25
+ expect(date.year?).to be true
26
+
27
+ expect(date.month).not_to be_nil
28
+ expect(date.month).to be_an Integer
29
+ expect(date.month?).to be true
30
+
31
+ expect(date.day).not_to be_nil
32
+ expect(date.day).to be_an Integer
33
+ expect(date.day?).to be true
34
+ end
35
+
36
+ it 'does not require a day' do
37
+ expect(date_without_day.day).to be_nil
38
+ expect(date_without_day.day?).to be false
39
+ end
40
+
41
+ it 'does not require a month' do
42
+ expect(date_without_month.month).to be_nil
43
+ expect(date_without_month.month?).to be false
44
+ end
45
+
46
+ it 'returns a date' do
47
+ expect(date.date).to be_a Date
48
+ expect(date.date.year).to eq date.year
49
+ expect(date.date.month).to eq date.month
50
+ expect(date.date.day).to eq date.day
51
+ end
52
+
53
+ describe 'parsing' do
54
+ it 'parses empty dates' do
55
+ date = described_class.parse ''
56
+
57
+ expect(date.year?).to be false
58
+ expect(date.month?).to be false
59
+ expect(date.day?).to be false
60
+ end
61
+
62
+ it 'parses dates with year only' do
63
+ date = described_class.parse '2012'
64
+
65
+ expect(date.year?).to be true
66
+ expect(date.year).to eq 2012
67
+ expect(date.month?).to be false
68
+ expect(date.day?).to be false
69
+ end
70
+
71
+ it 'parses parses dates with year and month' do
72
+ date = described_class.parse '2012-10'
73
+
74
+ expect(date.year?).to be true
75
+ expect(date.year).to eq 2012
76
+ expect(date.month?).to be true
77
+ expect(date.month).to eq 10
78
+ expect(date.day?).to be false
79
+ end
80
+
81
+ it 'parses full dates' do
82
+ date = described_class.parse '2012-10-02'
83
+
84
+ expect(date.year?).to be true
85
+ expect(date.year).to eq 2012
86
+ expect(date.month?).to be true
87
+ expect(date.month).to eq 10
88
+ expect(date.day?).to be true
89
+ expect(date.day).to eq 2
90
+ end
91
+
92
+ it 'does not parse on invalid dates' do
93
+ expect(-> { described_class.parse 'October 2nd, 2012' }).to raise_error MetalArchives::Errors::ArgumentError
94
+ expect(-> { described_class.parse 'a-b-c' }).to raise_error MetalArchives::Errors::ArgumentError
95
+ expect(-> { described_class.parse "This isn't a date" }).to raise_error MetalArchives::Errors::ArgumentError
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe MetalArchives::Range do
4
+ let(:range) { build :range }
5
+ let(:begin_range) { build :range, :end => nil }
6
+ let(:end_range) { build :range, :begin => nil }
7
+
8
+ it 'has the correct properties' do
9
+ expect(range).to respond_to :begin
10
+ expect(range).to respond_to :begin=
11
+ expect(range).to respond_to :begin?
12
+
13
+ expect(range).to respond_to :end
14
+ expect(range).to respond_to :end=
15
+ expect(range).to respond_to :end?
16
+ end
17
+
18
+ it 'returns the correct values' do
19
+ expect(range.begin).not_to be_nil
20
+ expect(range.begin).to be_a Date
21
+ expect(range.begin?).to be true
22
+
23
+ expect(range.end).not_to be_nil
24
+ expect(range.end).to be_a Date
25
+ expect(range.end?).to be true
26
+ end
27
+
28
+ it 'does not require an end date' do
29
+ expect(begin_range.end).to be_nil
30
+ expect(begin_range.end?).to be false
31
+ end
32
+
33
+ it 'does not require a begin date' do
34
+ expect(end_range.begin).to be_nil
35
+ expect(end_range.begin?).to be false
36
+ end
37
+
38
+ it 'implements comparable' do
39
+ range1 = described_class.new 1, 3
40
+ range1a = described_class.new 1, 3
41
+ range2 = described_class.new nil, 3
42
+ range2a = described_class.new nil, 3
43
+ range3 = described_class.new 1, nil
44
+ range3a = described_class.new 1, nil
45
+
46
+ range4 = described_class.new 1, 4
47
+ range5 = described_class.new 0, 3
48
+
49
+ expect(range1 <=> range1a).to eq 0
50
+ expect(range2 <=> range2a).to eq 0
51
+ expect(range3 <=> range3a).to eq 0
52
+
53
+ expect(range1 <=> range2).to be_nil
54
+ expect(range2 <=> range3).to be_nil
55
+ expect(range3 <=> range1).to be_nil
56
+
57
+ expect(range1 <=> range4).to eq -1
58
+ expect(range1 <=> range5).to eq -1
59
+ expect(range4 <=> range1).to eq 1
60
+ expect(range5 <=> range1).to eq 1
61
+ end
62
+ end