music_ids 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +68 -0
- data/lib/music_ids/id.rb +9 -8
- data/lib/music_ids/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86e0063188644c2b834ba47d11edebc8de761c37
|
4
|
+
data.tar.gz: 47738cdae0a47c725ea4eadb9cadb723aeb5b735
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ea07f4aac3e25370b947e51cbc531a5a05702e6ab767cd5166770741f3e85bb70df9c35ea292dd69e6708f636c8b76124dc1dbf8e47d94a25ecb7fb27d880ab
|
7
|
+
data.tar.gz: 0a7fc56bb2266614b103e5ee5ec04fa6f34b80415b1f3fbd6a0c3deb3f66c1c69d5096e5a5f2a27a606072145354a7907fc243c5c693c759e23152b05ad181f5
|
data/README.md
CHANGED
@@ -90,6 +90,74 @@ JSON.generate({isrc: isrc, grid: grid})
|
|
90
90
|
#=> "{\"isrc\":\"FRZ039800212\",\"grid\":\"A12425GABC1234002M\"}"
|
91
91
|
```
|
92
92
|
|
93
|
+
Problems with your ID will raise an ArgumentError on parsing
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
require 'music_ids'
|
97
|
+
|
98
|
+
begin
|
99
|
+
isrc = MusicIds::ISRC.parse('FRZ03')
|
100
|
+
rescue ArgumentError => e
|
101
|
+
e.message
|
102
|
+
end
|
103
|
+
#=> "'FRZ03' is not the right length to be a MusicIds::ISRC"
|
104
|
+
|
105
|
+
begin
|
106
|
+
grid = MusicIds::GRid.parse('A12425G')
|
107
|
+
rescue ArgumentError => e
|
108
|
+
e.message
|
109
|
+
end
|
110
|
+
#=> "'A12425G' is not the right length to be a MusicIds::GRid"
|
111
|
+
```
|
112
|
+
|
113
|
+
If you have to sometimes deal with bad metadata, then you may want a more
|
114
|
+
forgiving approach. Enter 'relaxed' parsing. You can pass `relaxed: true` as an
|
115
|
+
options hash to `.parse`, or you can call `.relaxed` directly. A 'bad' instance
|
116
|
+
returned by relaxed mode parsing reports that it's not ok with `#ok?` and won't
|
117
|
+
return any components, and will only return the input string it was passed and
|
118
|
+
not the full or prefixed versions you can get for a well-formed ISRC.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
require 'music_ids'
|
122
|
+
|
123
|
+
bad_isrc = MusicIds::ISRC.parse('bad', relaxed: true)
|
124
|
+
bad_isrc.ok? #=> false
|
125
|
+
bad_isrc.to_s #=> 'bad'
|
126
|
+
bad_isrc.country #=> nil
|
127
|
+
bad_isrc.as(:data) #=> 'bad'
|
128
|
+
bad_isrc.as(:full) #=> 'bad'
|
129
|
+
bad_isrc.as(:prefixed) #=> 'bad'
|
130
|
+
|
131
|
+
other_bad_isrc = MusicIds::ISRC.relaxed('bad')
|
132
|
+
other_bad_isrc.ok? #=> false
|
133
|
+
other_bad_isrc.to_s #=> 'bad'
|
134
|
+
|
135
|
+
bad_grid = MusicIds::GRid.parse('bad', relaxed: true)
|
136
|
+
bad_grid.ok? #=> false
|
137
|
+
bad_grid.to_s #=> 'bad'
|
138
|
+
bad_grid.scheme #=> nil
|
139
|
+
bad_grid.as(:data) #=> 'bad'
|
140
|
+
bad_grid.as(:full) #=> 'bad'
|
141
|
+
bad_grid.as(:prefixed) #=> 'bad'
|
142
|
+
|
143
|
+
other_bad_grid = MusicIds::GRid.relaxed('bad')
|
144
|
+
other_bad_grid.ok? #=> false
|
145
|
+
other_bad_grid.to_s #=> 'bad'
|
146
|
+
|
147
|
+
MusicIds::ISRC.relaxed(nil) #=> nil
|
148
|
+
MusicIds::GRid.relaxed(nil) #=> nil
|
149
|
+
|
150
|
+
# Of course, parsing valid IDs in relaxed mode works just fine:
|
151
|
+
MusicIds::ISRC.relaxed('FRZ039800212').ok? #=> true
|
152
|
+
MusicIds::GRid.relaxed('A12425GABC1234002M').ok? #=> true
|
153
|
+
|
154
|
+
# And strict parsed instances can tell you they're okay
|
155
|
+
MusicIds::ISRC.parse('FRZ039800212').ok? #=> true
|
156
|
+
MusicIds::GRid.parse('A12425GABC1234002M').ok? #=> true
|
157
|
+
```
|
158
|
+
|
159
|
+
For more details, see the [RDoc](http://www.rubydoc.info/gems/music_ids).
|
160
|
+
|
93
161
|
## Development
|
94
162
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
95
163
|
`bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/music_ids/id.rb
CHANGED
@@ -8,7 +8,13 @@ module MusicIds
|
|
8
8
|
# @option opts [true, false] :relaxed (false) Whether to parse in relaxed mode
|
9
9
|
# @return [ISRC] the ISRC instance
|
10
10
|
def parse(input, opts = {})
|
11
|
-
opts[:relaxed] ?
|
11
|
+
opts[:relaxed] ? relaxed(input) : parse_strict(input)
|
12
|
+
end
|
13
|
+
|
14
|
+
def relaxed(input)
|
15
|
+
parse_string(input) { |input|
|
16
|
+
new(input, ok: false) unless input.nil?
|
17
|
+
}
|
12
18
|
end
|
13
19
|
|
14
20
|
private
|
@@ -21,17 +27,12 @@ module MusicIds
|
|
21
27
|
|
22
28
|
def parse_strict(input)
|
23
29
|
parse_string(input) { |input|
|
24
|
-
raise ArgumentError, "'#{input}' is not the right length to be a #{self.
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
def parse_relaxed(input)
|
29
|
-
parse_string(input) { |input|
|
30
|
-
new(input, ok: false)
|
30
|
+
raise ArgumentError, "'#{input}' is not the right length to be a #{self.name}"
|
31
31
|
}
|
32
32
|
end
|
33
33
|
|
34
34
|
def parse_string(input)
|
35
|
+
return yield(input) if input.nil?
|
35
36
|
normalised = input.to_s.upcase
|
36
37
|
if match = well_formed_id_matcher.match(normalised)
|
37
38
|
new(match[1].gsub('-', ''))
|
data/lib/music_ids/version.rb
CHANGED