metal_archives 0.3.0 → 0.4.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: 29c629e0d26e84fff55b4c57c2423e8f46c95547
4
- data.tar.gz: 200e49292fd001e2b035f105c5f518323f7659e3
3
+ metadata.gz: 5e5c9e464d998ad27ebce937a23a7b9924ccf871
4
+ data.tar.gz: 758ebce5c4bb742680622b18c0fb5b3138a077c3
5
5
  SHA512:
6
- metadata.gz: 6a2443f5b2f8bcdc4dd5d7f8cc96648b1182225d9ba2e27289fb4782fb5b8cbd283ee8995e3b50cb1dbc1c76e7a67edddffb92d8fda8f5d703d03a4dd4a16426
7
- data.tar.gz: b824e554a0deac921487b096c93227989d9d1a440969114b6b2cf5fc35acbe7f831d4b7fbed2a8316d18b8e50d4ab298bd54931df1b21b847eaaec12e0052ef7
6
+ metadata.gz: b5c26112b01a3de24352096e0f3dc2b6a7394cd7a9ae0bf4f7607f8ec057668ca6cc5bd30dda216547128dff8c38f141463472645b2623b1507c13b9bf8fd5e2
7
+ data.tar.gz: 1848ff7bef54f75f913e7212b7c18b9e97c216ec3e8ce86f5b3b90e8c632614d9408b5cf43089b76de045e2591e472264b9d9bc9aa99e00f0f7c94f661dd5f69
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  html/
10
10
 
11
11
  Gemfile.lock
12
+ .idea
data/README.md CHANGED
@@ -35,8 +35,8 @@ MetalArchives.configure do |c|
35
35
  c.request_rate = 1
36
36
  c.request_timeout = 3
37
37
 
38
- # Print debugging information
39
- c.debug = false
38
+ # Custom logger (optional)
39
+ c.logger = Logger.new File.new('metal_archives.log')
40
40
  end
41
41
  ```
42
42
 
@@ -61,17 +61,19 @@ require 'countries'
61
61
 
62
62
  @bands_containing_hell = MetalArchives::Band.search_by :name => '*hell*'
63
63
  @non_melodic_death_bands = MetalArchives::Band.search_by :genre => 'death -melodic'
64
- ```
65
64
 
66
- Refer to the model's RDoc documentation for full documentation.
65
+ # Methods returning multiple results return a MetalArchives::Collection.
66
+ # Collection wraps a paginated resource, and can be used to iterate over huge queries.
67
+ @non_melodic_death_bands.first(100).each do |band|
68
+ puts band.name
69
+ end
70
+ ```
67
71
 
68
- ## Debugging
72
+ Refer to the model's [RDoc documentation](https://floriandejonckheere.github.io/metal_archives/html/).
69
73
 
70
- Turn on `debug` in the configuration block to enable logging HTTP requests and responses.
74
+ ## Lazy loading
71
75
 
72
- ```
73
- $ irb -r metal_archives
74
- ```
76
+ By default when an model (Artist, Band, ...) is created, no data is fetched. This leads to instantiation of a model with an invalid ID not throwing any errors. Calling any attribute other than `id` will cause all data to be fetched and any errors to be thrown. Refer to the respective methods to find out what errors are thrown in what circumstances.
75
77
 
76
78
  ## Testing
77
79
  ```
@@ -66,9 +66,9 @@ module MetalArchives
66
66
  attr_accessor :request_timeout
67
67
 
68
68
  ##
69
- # Print debug information
69
+ # Logger instance
70
70
  #
71
- attr_accessor :debug
71
+ attr_accessor :logger
72
72
 
73
73
  ##
74
74
  # Default configuration values
@@ -76,6 +76,7 @@ module MetalArchives
76
76
  def initialize
77
77
  @throttle_rate = 1
78
78
  @throttle_wait = 3
79
+ @logger = Logger.new STDOUT
79
80
  end
80
81
  end
81
82
  end
@@ -9,12 +9,12 @@ module Errors
9
9
  class Error < StandardError; end
10
10
 
11
11
  ##
12
- # Error in data
12
+ # No or invalid ID
13
13
  #
14
- class DataError < Error; end
14
+ class InvalidIDError < Error; end
15
15
 
16
16
  ##
17
- # No or invalid configuration found
17
+ # No or invalid configuration
18
18
  #
19
19
  class InvalidConfigurationError < Error; end
20
20
 
@@ -15,6 +15,7 @@ module MetalArchives
15
15
  def get(*params)
16
16
  response = client.get *params
17
17
 
18
+ raise Errors::InvalidIDError, response.status if response.status == 404
18
19
  raise Errors::APIError, response.status if response.status >= 400
19
20
 
20
21
  response
@@ -31,14 +32,15 @@ module MetalArchives
31
32
 
32
33
  @faraday ||= Faraday.new do |f|
33
34
  f.request :url_encoded # form-encode POST params
34
- f.response :logger if !!MetalArchives.config.debug # log requests to STDOUT
35
+ f.response :logger, MetalArchives.config.logger
35
36
 
36
37
  f.use MetalArchives::Middleware
37
38
  f.use Faraday::HttpCache,
38
39
  :store => MetalArchives.config.cache_store if !!MetalArchives.config.enable_cache
39
40
  f.use :throttler,
40
41
  :rate => MetalArchives.config.request_rate,
41
- :wait => MetalArchives.config.request_timeout
42
+ :wait => MetalArchives.config.request_timeout,
43
+ :logger => MetalArchives.config.logger
42
44
 
43
45
  f.adapter Faraday.default_adapter
44
46
  end
@@ -19,6 +19,9 @@ module MetalArchives
19
19
  #
20
20
  # Returns +String+
21
21
  #
22
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
23
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
24
+ #
22
25
  property :name
23
26
 
24
27
  ##
@@ -26,6 +29,9 @@ module MetalArchives
26
29
  #
27
30
  # Returns +Array+ of +String+
28
31
  #
32
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
33
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
34
+ #
29
35
  property :aliases, :multiple => true
30
36
 
31
37
  ##
@@ -33,6 +39,9 @@ module MetalArchives
33
39
  #
34
40
  # Returns +ISO3166::Country+
35
41
  #
42
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
43
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
44
+ #
36
45
  property :country, :type => ISO3166::Country
37
46
 
38
47
  ##
@@ -40,6 +49,9 @@ module MetalArchives
40
49
  #
41
50
  # Returns +String+
42
51
  #
52
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
53
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
54
+ #
43
55
  property :location
44
56
 
45
57
  ##
@@ -47,6 +59,9 @@ module MetalArchives
47
59
  #
48
60
  # Returns +Date+
49
61
  #
62
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
63
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
64
+ #
50
65
  property :date_of_birth, :type => Date
51
66
 
52
67
  ##
@@ -54,6 +69,9 @@ module MetalArchives
54
69
  #
55
70
  # Returns +Date+
56
71
  #
72
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
73
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
74
+ #
57
75
  property :date_of_death, :type => Date
58
76
 
59
77
  ##
@@ -61,6 +79,9 @@ module MetalArchives
61
79
  #
62
80
  # Returns +String+
63
81
  #
82
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
83
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
84
+ #
64
85
  property :cause_of_death
65
86
 
66
87
  ##
@@ -68,6 +89,9 @@ module MetalArchives
68
89
  #
69
90
  # Returns +Symbol+, either +:male+ or +:female+
70
91
  #
92
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
93
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
94
+ #
71
95
  enum :gender, :values => [:male, :female]
72
96
 
73
97
  ##
@@ -75,6 +99,9 @@ module MetalArchives
75
99
  #
76
100
  # Returns raw HTML +String+
77
101
  #
102
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
103
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
104
+ #
78
105
  property :biography
79
106
 
80
107
  ##
@@ -82,6 +109,9 @@ module MetalArchives
82
109
  #
83
110
  # Returns raw HTML +String+
84
111
  #
112
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
113
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
114
+ #
85
115
  property :trivia
86
116
 
87
117
  ##
@@ -89,6 +119,9 @@ module MetalArchives
89
119
  #
90
120
  # Returns +Array+ of +Hash+ containing the following keys
91
121
  #
122
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
123
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
124
+ #
92
125
  # [+similar+]
93
126
  # - +:url+: +String+
94
127
  # - +:type+: +Symbol+, either +:official+, +:unofficial+ or +:unlisted_bands+
@@ -105,7 +138,8 @@ module MetalArchives
105
138
  ##
106
139
  # Fetch the data and assemble the model
107
140
  #
108
- # Raises rdoc-ref:MetalArchives::Errors::APIError
141
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when receiving a status code == 404
142
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
109
143
  #
110
144
  def assemble # :nodoc:
111
145
  ## Base attributes
@@ -132,8 +166,7 @@ module MetalArchives
132
166
 
133
167
  properties[:links] = Parsers::Artist.parse_links_html response.body
134
168
 
135
- ## Use constructor to fill properties
136
- initialize properties
169
+ properties
137
170
  end
138
171
 
139
172
  class << self
@@ -149,10 +182,30 @@ module MetalArchives
149
182
  Artist.new :id => id
150
183
  end
151
184
 
185
+ ##
186
+ # Find by ID (no lazy loading)
187
+ #
188
+ # Returns rdoc-ref:Band
189
+ #
190
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
191
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
192
+ #
193
+ # [+id+]
194
+ # +Integer+
195
+ #
196
+ def find!(id)
197
+ obj = find id
198
+ obj.send :fetch
199
+
200
+ obj
201
+ end
202
+
152
203
  ##
153
204
  # Find by attributes
154
205
  #
155
- # Returns rdoc-ref:Artist or nil when ID is invalid
206
+ # Returns rdoc-ref:Artist or nil when no results
207
+ #
208
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400
156
209
  #
157
210
  # [+query+]
158
211
  # Hash containing one or more of the following keys:
@@ -171,8 +224,6 @@ module MetalArchives
171
224
  id = Nokogiri::HTML(data.first).xpath('//a/@href').first.value.gsub('\\', '').split('/').last.gsub(/\D/, '').to_i
172
225
 
173
226
  Artist.new :id => id
174
- rescue Errors::APIError
175
- nil
176
227
  end
177
228
 
178
229
  ##
@@ -180,6 +231,8 @@ module MetalArchives
180
231
  #
181
232
  # Returns rdoc-ref:Collection of rdoc-ref:Artist
182
233
  #
234
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400
235
+ #
183
236
  # [+name+]
184
237
  # +String+
185
238
  #
@@ -19,6 +19,9 @@ module MetalArchives
19
19
  #
20
20
  # Returns +String+
21
21
  #
22
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
23
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
24
+ #
22
25
  property :name
23
26
 
24
27
  ##
@@ -26,6 +29,9 @@ module MetalArchives
26
29
  #
27
30
  # Returns +Array+ of +String+
28
31
  #
32
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
33
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
34
+ #
29
35
  property :aliases, :multiple => true
30
36
 
31
37
  ##
@@ -33,6 +39,9 @@ module MetalArchives
33
39
  #
34
40
  # Returns +ISO3166::Country+
35
41
  #
42
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
43
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
44
+ #
36
45
  property :country, :type => ISO3166::Country
37
46
 
38
47
  ##
@@ -40,6 +49,9 @@ module MetalArchives
40
49
  #
41
50
  # Returns +String+
42
51
  #
52
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
53
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
54
+ #
43
55
  property :location
44
56
 
45
57
  ##
@@ -47,6 +59,9 @@ module MetalArchives
47
59
  #
48
60
  # Returns +Date+
49
61
  #
62
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
63
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
64
+ #
50
65
  property :date_formed, :type => Date
51
66
 
52
67
  ##
@@ -54,6 +69,9 @@ module MetalArchives
54
69
  #
55
70
  # Returns +Array+ of rdoc-ref:Range
56
71
  #
72
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
73
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
74
+ #
57
75
  property :date_active, :type => MetalArchives::Range, :multiple => true
58
76
 
59
77
  ##
@@ -61,6 +79,9 @@ module MetalArchives
61
79
  #
62
80
  # Returns +Array+ of +String+
63
81
  #
82
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
83
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
84
+ #
64
85
  property :genres, :multiple => true
65
86
 
66
87
  ##
@@ -68,6 +89,9 @@ module MetalArchives
68
89
  #
69
90
  # Returns +Array+ of +String+
70
91
  #
92
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
93
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
94
+ #
71
95
  property :lyrical_themes, :multiple => true
72
96
 
73
97
  ##
@@ -75,6 +99,9 @@ module MetalArchives
75
99
  #
76
100
  # Returns rdoc-ref:Label
77
101
  #
102
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
103
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
104
+ #
78
105
  property :label, :type => MetalArchives::Label
79
106
 
80
107
  ##
@@ -82,6 +109,9 @@ module MetalArchives
82
109
  #
83
110
  # Returns boolean
84
111
  #
112
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
113
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
114
+ #
85
115
  enum :independent, :values => [true, false]
86
116
 
87
117
  ##
@@ -89,6 +119,9 @@ module MetalArchives
89
119
  #
90
120
  # Returns raw HTML +String+
91
121
  #
122
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
123
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
124
+ #
92
125
  property :comment
93
126
 
94
127
  ##
@@ -96,6 +129,9 @@ module MetalArchives
96
129
  #
97
130
  # Returns +:active+, +:split_up+, +:on_hold+, +:unknown+, +:changed_name+ or +:disputed+
98
131
  #
132
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
133
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
134
+ #
99
135
  enum :status, :values => [:active, :split_up, :on_hold, :unknown, :changed_name, :disputed]
100
136
 
101
137
  # TODO: releases
@@ -106,6 +142,9 @@ module MetalArchives
106
142
  #
107
143
  # Returns +Array+ of +Hash+ containing the following keys
108
144
  #
145
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
146
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
147
+ #
109
148
  # [+similar+]
110
149
  # - +:band+: rdoc-ref:Band
111
150
  # - +:score+: +Integer+
@@ -117,6 +156,9 @@ module MetalArchives
117
156
  #
118
157
  # Returns +String+
119
158
  #
159
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
160
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
161
+ #
120
162
  property :logo
121
163
 
122
164
  ##
@@ -124,6 +166,9 @@ module MetalArchives
124
166
  #
125
167
  # Returns +String+
126
168
  #
169
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
170
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
171
+ #
127
172
  property :photo
128
173
 
129
174
  ##
@@ -131,6 +176,9 @@ module MetalArchives
131
176
  #
132
177
  # Returns +Array+ of +Hash+ containing the following keys
133
178
  #
179
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
180
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
181
+ #
134
182
  # [+similar+]
135
183
  # - +:url+: +String+
136
184
  # - +:type+: +Symbol+, either +:official+ or +:merchandise+
@@ -142,7 +190,8 @@ module MetalArchives
142
190
  ##
143
191
  # Fetch the data and assemble the model
144
192
  #
145
- # Raises rdoc-ref:MetalArchives::Errors::APIError
193
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when receiving a status code == 404
194
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
146
195
  #
147
196
  def assemble # :nodoc:
148
197
  ## Base attributes
@@ -169,16 +218,13 @@ module MetalArchives
169
218
 
170
219
  properties[:links] = Parsers::Band.parse_related_links_html response.body
171
220
 
172
- ## Use constructor to fill properties
173
- initialize properties
221
+ properties
174
222
  end
175
223
 
176
224
  class << self
177
225
  ##
178
226
  # Find by ID
179
227
  #
180
- # Refer to {MA's FAQ}[http://www.metal-archives.com/content/help?index=3#tab_db] for search tips.
181
- #
182
228
  # Returns rdoc-ref:Band, even when ID is invalid (because the data is lazily fetched)
183
229
  #
184
230
  # [+id+]
@@ -188,12 +234,32 @@ module MetalArchives
188
234
  Band.new :id => id
189
235
  end
190
236
 
237
+ ##
238
+ # Find by ID (no lazy loading)
239
+ #
240
+ # Returns rdoc-ref:Band
241
+ #
242
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no or invalid id
243
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
244
+ #
245
+ # [+id+]
246
+ # +Integer+
247
+ #
248
+ def find!(id)
249
+ obj = find id
250
+ obj.send :fetch
251
+
252
+ obj
253
+ end
254
+
191
255
  ##
192
256
  # Find by attributes
193
257
  #
194
258
  # Refer to {MA's FAQ}[http://www.metal-archives.com/content/help?index=3#tab_db] for search tips.
195
259
  #
196
- # Returns rdoc-ref:Band or nil when ID is invalid
260
+ # Returns rdoc-ref:Band or nil when no results
261
+ #
262
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400
197
263
  #
198
264
  # [+query+]
199
265
  # Hash containing one or more of the following keys:
@@ -233,6 +299,8 @@ module MetalArchives
233
299
  #
234
300
  # Returns rdoc-ref:Collection of rdoc-ref:Band
235
301
  #
302
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400
303
+ #
236
304
  # [+query+]
237
305
  # Hash containing one or more of the following keys:
238
306
  # - +:name+: +String+
@@ -285,6 +353,8 @@ module MetalArchives
285
353
  #
286
354
  # Returns (possibly empty) +Array+ of rdoc-ref:Band
287
355
  #
356
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400
357
+ #
288
358
  # [+name+]
289
359
  # +String+
290
360
  #
@@ -14,18 +14,28 @@ module MetalArchives
14
14
  end
15
15
  end
16
16
 
17
+ ##
18
+ # Returns true if two objects have the same type and id
19
+ #
20
+ def ==(obj)
21
+ obj.instance_of? self.class and self.id == obj.id
22
+ end
23
+
17
24
  protected
18
25
  ##
19
26
  # Eagerly fetch the data
20
27
  #
21
- # Raises rdoc-ref:MetalArchives::Errors::APIError
28
+ # Raises rdoc-ref:MetalArchives::Errors::InvalidIDError when no id
29
+ # Raises rdoc-ref:MetalArchives::Errors::NotImplementedError when no :assemble method is implemented
30
+ # Raises rdoc-ref:MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
22
31
  #
23
32
  def fetch
24
- raise Errors::DataError, 'no id present' unless !!id
33
+ raise Errors::InvalidIDError, 'no id present' unless !!id
25
34
 
26
35
  raise Errors::NotImplementedError, 'no :assemble method in model' unless self.respond_to? :assemble, true
27
36
 
28
- assemble
37
+ # Use constructor to set attributes
38
+ initialize assemble
29
39
  end
30
40
 
31
41
  class << self
@@ -72,6 +72,8 @@ module Parsers
72
72
  props[:aliases] << alt unless props[:name] == alt
73
73
 
74
74
  props
75
+ rescue => e
76
+ raise ParserError, e
75
77
  end
76
78
 
77
79
  def parse_links_html(response)
@@ -100,6 +100,8 @@ module Parsers
100
100
  end
101
101
 
102
102
  props
103
+ rescue => e
104
+ raise ParserError, e
103
105
  end
104
106
 
105
107
  def parse_similar_bands_html(response)
@@ -2,5 +2,5 @@ module MetalArchives
2
2
  ##
3
3
  # MetalArchives API version
4
4
  #
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
@@ -18,7 +18,7 @@ class ModelTwo < MetalArchives::BaseModel
18
18
  end
19
19
 
20
20
  ##
21
- # Sample model
21
+ # Sample complete model
22
22
  #
23
23
  class ModelThree < MetalArchives::BaseModel
24
24
  property :id
@@ -29,6 +29,18 @@ class ModelThree < MetalArchives::BaseModel
29
29
  end
30
30
  end
31
31
 
32
+ ##
33
+ # Sample complete model
34
+ #
35
+ class ModelFour < MetalArchives::BaseModel
36
+ property :id
37
+ property :property_one
38
+
39
+ def assemble
40
+ instance_variable_set "@property_one", 'Property One'
41
+ end
42
+ end
43
+
32
44
  ##
33
45
  # BaseModel tests
34
46
  #
@@ -84,4 +96,16 @@ class BaseModelTest < Test::Unit::TestCase
84
96
  assert model.instance_variable_defined? '@property_one'
85
97
  assert_equal 'Property One', model.property_one
86
98
  end
99
+
100
+ def test_equality
101
+ m1 = ModelThree.new :id => 'id_one'
102
+ m2 = ModelThree.new :id => 'id_one'
103
+ m3 = ModelThree.new :id => 'id_two'
104
+ m4 = ModelFour.new :id => 'id_one'
105
+
106
+ assert_equal m1, m2
107
+ assert_not_equal m2, m3
108
+ assert_not_equal m1, m3
109
+ assert_not_equal m1, m4
110
+ end
87
111
  end
@@ -12,6 +12,7 @@ class ArtistQueryTest < Test::Unit::TestCase
12
12
 
13
13
  assert_not_nil artist
14
14
  assert_instance_of MetalArchives::Artist, artist
15
+ assert_equal 60908, artist.id
15
16
  assert_equal 'Alberto Rionda', artist.name
16
17
  assert_equal ISO3166::Country['ES'], artist.country
17
18
 
@@ -20,6 +21,28 @@ class ArtistQueryTest < Test::Unit::TestCase
20
21
  assert_instance_of MetalArchives::Artist, artist
21
22
  end
22
23
 
24
+ def test_find!
25
+ artist = MetalArchives::Artist.find! 60908
26
+
27
+ assert_not_nil artist
28
+ assert_instance_of MetalArchives::Artist, artist
29
+ assert_equal 60908, artist.id
30
+ assert_equal 'Alberto Rionda', artist.name
31
+ assert_equal ISO3166::Country['ES'], artist.country
32
+
33
+ assert_raise MetalArchives::Errors::InvalidIDError do
34
+ MetalArchives::Artist.find! nil
35
+ end
36
+
37
+ assert_raise MetalArchives::Errors::InvalidIDError do
38
+ MetalArchives::Artist.find! 0
39
+ end
40
+
41
+ assert_raise MetalArchives::Errors::APIError do
42
+ MetalArchives::Artist.find! -1
43
+ end
44
+ end
45
+
23
46
  def test_find_by
24
47
  artist = MetalArchives::Artist.find_by :name => 'Alberto Rionda'
25
48
 
@@ -44,4 +67,29 @@ class ArtistQueryTest < Test::Unit::TestCase
44
67
 
45
68
  assert !MetalArchives::Artist.search('SomeNonExistantName').any?
46
69
  end
70
+
71
+ def test_errors
72
+ assert_nothing_raised do
73
+ MetalArchives::Artist.new :id => nil
74
+ MetalArchives::Artist.new :id => 0
75
+ MetalArchives::Artist.new :id => -1
76
+ MetalArchives::Artist.find_by :name => 'SomeNonExistantName'
77
+ MetalArchives::Artist.search 'SomeNonExistantName'
78
+ end
79
+
80
+ assert_raise MetalArchives::Errors::InvalidIDError do
81
+ MetalArchives::Artist.new(:id => nil).send :assemble
82
+ end
83
+
84
+ assert_raise MetalArchives::Errors::InvalidIDError do
85
+ MetalArchives::Artist.new(:id => 0).send :assemble
86
+ end
87
+
88
+ assert_raise MetalArchives::Errors::APIError do
89
+ MetalArchives::Artist.new(:id => -1).send :assemble
90
+ end
91
+
92
+ assert_nil MetalArchives::Artist.find_by :name => 'SomeNonExistantName'
93
+ assert !MetalArchives::Artist.search('SomeNonExistantName').any?
94
+ end
47
95
  end
@@ -12,6 +12,7 @@ class BandQueryTest < Test::Unit::TestCase
12
12
 
13
13
  assert_not_nil band
14
14
  assert_instance_of MetalArchives::Band, band
15
+ assert_equal 3540361100, band.id
15
16
  assert_equal 'Alquimia', band.name
16
17
  assert_equal ISO3166::Country['ES'], band.country
17
18
 
@@ -23,6 +24,31 @@ class BandQueryTest < Test::Unit::TestCase
23
24
  assert_instance_of MetalArchives::Band, band
24
25
  end
25
26
 
27
+ def test_find!
28
+ band = MetalArchives::Band.find! 3540361100
29
+
30
+ assert_not_nil band
31
+ assert_instance_of MetalArchives::Band, band
32
+ assert_equal 3540361100, band.id
33
+ assert_equal 'Alquimia', band.name
34
+ assert_equal ISO3166::Country['ES'], band.country
35
+
36
+ assert_match 'http', band.logo
37
+ assert_match 'http', band.photo
38
+
39
+ assert_raise MetalArchives::Errors::InvalidIDError do
40
+ MetalArchives::Band.find! nil
41
+ end
42
+
43
+ assert_raise MetalArchives::Errors::InvalidIDError do
44
+ MetalArchives::Band.find! 0
45
+ end
46
+
47
+ assert_raise MetalArchives::Errors::APIError do
48
+ MetalArchives::Band.find! -1
49
+ end
50
+ end
51
+
26
52
  def test_find_by
27
53
  band = MetalArchives::Band.find_by :name => 'Falconer'
28
54
 
@@ -82,4 +108,31 @@ class BandQueryTest < Test::Unit::TestCase
82
108
 
83
109
  assert_equal 274, MetalArchives::Band.search_by(:country => ISO3166::Country['CN']).count
84
110
  end
111
+
112
+ def test_errors
113
+ assert_nothing_raised do
114
+ MetalArchives::Band.new :id => nil
115
+ MetalArchives::Band.new :id => 0
116
+ MetalArchives::Band.new :id => -1
117
+ MetalArchives::Band.find_by :name => 'SomeNonExistantName'
118
+ MetalArchives::Band.search_by :name => 'SomeNonExistantName'
119
+ MetalArchives::Band.search 'SomeNonExistantName'
120
+ end
121
+
122
+ assert_raise MetalArchives::Errors::InvalidIDError do
123
+ MetalArchives::Band.new(:id => nil).send :assemble
124
+ end
125
+
126
+ assert_raise MetalArchives::Errors::InvalidIDError do
127
+ MetalArchives::Band.new(:id => 0).send :assemble
128
+ end
129
+
130
+ assert_raise MetalArchives::Errors::APIError do
131
+ MetalArchives::Band.new(:id => -1).send :assemble
132
+ end
133
+
134
+ assert_nil MetalArchives::Band.find_by :name => 'SomeNonExistantName'
135
+ assert !MetalArchives::Band.search_by(:name => 'SomeNonExistantName').any?
136
+ assert !MetalArchives::Band.search('SomeNonExistantName').any?
137
+ end
85
138
  end
data/test/test_helper.rb CHANGED
@@ -4,6 +4,10 @@ require 'test/unit'
4
4
  require 'metal_archives'
5
5
 
6
6
  # Configuration
7
+ puts "Debug log is written to /tmp/test-#{Process.pid}.log"
8
+ logger = Logger.new File.open('/tmp/test-#{Process.pid}.log', 'a')
9
+ logger.level = Logger::DEBUG
10
+
7
11
  MetalArchives.configure do |c|
8
12
  c.app_name = 'MetalArchivesGemTestSuite'
9
13
  c.app_version = MetalArchives::VERSION
@@ -17,10 +21,6 @@ MetalArchives.configure do |c|
17
21
  c.request_rate = 1
18
22
  c.request_timeout = 3
19
23
 
20
- # Print debugging information
21
- c.debug = false
22
- end
23
-
24
- def data_for(filename)
25
- File.read(File.join(File.dirname(__FILE__) + "/data/#{filename}"))
24
+ # Custom logger (optional)
25
+ c.logger = logger
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metal_archives
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dejonckheere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-12 00:00:00.000000000 Z
11
+ date: 2016-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug