ravelry 0.0.1

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 (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.yardopts +1 -0
  6. data/CODE_OF_CONDUCT.md +22 -0
  7. data/Gemfile +16 -0
  8. data/Gemfile.lock +42 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +112 -0
  11. data/VERSION +1 -0
  12. data/lib/ravelry.rb +39 -0
  13. data/lib/ravelry/author.rb +27 -0
  14. data/lib/ravelry/category.rb +57 -0
  15. data/lib/ravelry/configuration.rb +11 -0
  16. data/lib/ravelry/craft.rb +19 -0
  17. data/lib/ravelry/data.rb +25 -0
  18. data/lib/ravelry/pack.rb +135 -0
  19. data/lib/ravelry/pattern.rb +390 -0
  20. data/lib/ravelry/pattern_needle.rb +54 -0
  21. data/lib/ravelry/pattern_type.rb +34 -0
  22. data/lib/ravelry/photo.rb +69 -0
  23. data/lib/ravelry/printing.rb +70 -0
  24. data/lib/ravelry/utils/build.rb +128 -0
  25. data/lib/ravelry/utils/utilities.rb +14 -0
  26. data/lib/ravelry/version.rb +3 -0
  27. data/lib/ravelry/yarn.rb +39 -0
  28. data/lib/ravelry/yarn_weight.rb +48 -0
  29. data/ravelry.gemspec +27 -0
  30. data/spec/helpers/helpers.rb +106 -0
  31. data/spec/helpers/pack_helpers.rb +107 -0
  32. data/spec/helpers/pattern_helpers.rb +190 -0
  33. data/spec/helpers/yarn_helpers.rb +27 -0
  34. data/spec/helpers/yarn_weight_helpers.rb +25 -0
  35. data/spec/ravelry/author_spec.rb +33 -0
  36. data/spec/ravelry/category_spec.rb +46 -0
  37. data/spec/ravelry/craft_spec.rb +22 -0
  38. data/spec/ravelry/data_spec.rb +27 -0
  39. data/spec/ravelry/pack_spec.rb +114 -0
  40. data/spec/ravelry/pattern_needle_spec.rb +52 -0
  41. data/spec/ravelry/pattern_spec.rb +301 -0
  42. data/spec/ravelry/pattern_type_spec.rb +28 -0
  43. data/spec/ravelry/photo_spec.rb +56 -0
  44. data/spec/ravelry/printing_spec.rb +65 -0
  45. data/spec/ravelry/utils/build_spec.rb +187 -0
  46. data/spec/ravelry/yarn_spec.rb +30 -0
  47. data/spec/ravelry/yarn_weight_spec.rb +42 -0
  48. data/spec/ravelry_spec.rb +3 -0
  49. data/spec/spec_helper.rb +30 -0
  50. metadata +154 -0
@@ -0,0 +1,390 @@
1
+ module Ravelry
2
+
3
+ # `Ravelry::Pattern` corresponds to Pattern objects in Ravelry.
4
+ #
5
+ # This class requires your environment variables be set (see {file:README.md README}). API calls are authenticated using HTTP Basic Auth unless otherwise noted.
6
+ #
7
+ # If your `pattern.data` is missing one of the attributes below, that method will return `nil`.
8
+ #
9
+ # #Pattern objects
10
+ #
11
+ # To create an empty object:
12
+ #
13
+ # ```ruby
14
+ # pattern = Ravelry::Pattern.new
15
+ # ```
16
+ #
17
+ # To complete the `GET` request, set the `id` and run:
18
+ #
19
+ # ```ruby
20
+ # pattern.id = "000000"
21
+ # pattern.get
22
+ # ```
23
+ #
24
+ # After calling `get`, you have access to all of the class methods below.
25
+ #
26
+ # ##Initializing with an id
27
+ #
28
+ # Optionally, you can initialize with an id:
29
+ #
30
+ # ```ruby
31
+ # pattern = Ravelry::Pattern.new(id)
32
+ # ```
33
+ #
34
+ # And then run your get request:
35
+ #
36
+ # ```ruby
37
+ # pattern.get
38
+ # ```
39
+ #
40
+ # ##Loading existing pattern data
41
+ #
42
+ # If you have existing pattern data, you should initialize as follows:
43
+ #
44
+ # ```ruby
45
+ # pattern = Ravelry::Pattern.new
46
+ # pattern.data = my_data
47
+ # ```
48
+ #
49
+ # You now have access to all class methods for your pattern. Be warned: if you run `get` again, you will override your data with fresh information from the API call.
50
+ #
51
+ # # Pattern data
52
+ #
53
+ # After you have pattern data from the API, you have access to all of the pattern attributes through the class methods (see documentation). Example:
54
+ #
55
+ # ```ruby
56
+ # pattern.free?
57
+ # # => true
58
+ # ```
59
+ #
60
+ # # Building associated objects
61
+ #
62
+ # You will need to call special methods to create the associated objects with your pattern.
63
+ #
64
+ # To create all associated objects at once, call the following method after initialization:
65
+ #
66
+ # ```ruby
67
+ # pattern.build_all_objects
68
+ # ```
69
+ #
70
+ # Note that this does not perform an API call: it creates the objects using the data returned from the initial `get` for your pattern object.
71
+ #
72
+ # This will create the following objects and readers from the existing `data`:
73
+ #
74
+ # * `pattern.author` - a {Ravelry::Author} object
75
+ # * `pattern.categories` - an array of {Ravelry::Category} objects
76
+ # * `pattern.craft` - a {Ravelry::Craft} object
77
+ # * `pattern.needles` - an array of {Ravelry::PatternNeedle} objects
78
+ # * `pattern.packs` - array of {Ravelry::Pack} objects
79
+ # * `pattern.photos` - an array of {Ravelry::Photo} objects
80
+ # * `pattern.printings` - an array of {Raverly::Printing} objects
81
+ # * `pattern.type` - a {Ravelry::PatternType} object
82
+ # * `pattern.yarns` - array of {Ravelry::Yarn} objects
83
+ # * `pattern.yarn_weights` - array of {Ravelry::YarnWeight} objects
84
+ #
85
+ # See the documentation for each object's available methods.
86
+ #
87
+ class Pattern < Data
88
+
89
+ include Build
90
+
91
+ attr_reader :author, :categories, :craft, :needles, :packs, :photos, :printings, :type, :yarns, :yarn_weights
92
+
93
+ # Handles GET API call and parses JSON response.
94
+ #
95
+ def get
96
+ request = Typhoeus::Request.get("https://api.ravelry.com/patterns/#{@id}.json", userpwd: "#{ENV['RAV_ACCESS']}:#{ENV['RAV_PERSONAL']}")
97
+ result = JSON.parse(request.response_body, {symbolize_names: true})
98
+ @data = result[:pattern]
99
+ end
100
+
101
+ # Creates all objects associated with your pattern; returns nothing; sets `attr_readers`.
102
+ #
103
+ def build
104
+ @author = Build.author(data)
105
+ @categories = Build.categories(data)
106
+ @craft = Build.craft(data)
107
+ @needles = Build.needles(data)
108
+ @packs = Build.packs(data)
109
+ @photos = Build.photos(data)
110
+ @printings = Build.printings(data)
111
+ @type = Build.pattern_type(data)
112
+ @yarns = Build.yarns(data)
113
+ @yarn_weights = Build.yarn_weights(data)
114
+ nil
115
+ end
116
+
117
+ # Gets comments_count from existing `data`.
118
+ #
119
+ def comments_count
120
+ data[:comments_count]
121
+ end
122
+
123
+ # Gets currency from existing `data`.
124
+ #
125
+ def currency
126
+ data[:currency]
127
+ end
128
+
129
+ # Gets currency_symbol from existing `data`.
130
+ #
131
+ def currency_symbol
132
+ data[:currency_symbol]
133
+ end
134
+
135
+ # Returns the difficult average as a Float (this is how it is stored by Ravelry).
136
+ #
137
+ def difficulty_average_float
138
+ data[:difficulty_average]
139
+ end
140
+
141
+ # Returns the difficulty average rounded up or down to an Integer.
142
+ #
143
+ def difficulty_average_integer
144
+ difficulty_average_float.round(0)
145
+ end
146
+
147
+ # Gets difficulty_count (Integer) from existing `data`.
148
+ #
149
+ def difficulty_count
150
+ data[:difficulty_count]
151
+ end
152
+
153
+ # Returns true if the pattern can be downloaded.
154
+ #
155
+ def downloadable?
156
+ data[:downloadable]
157
+ end
158
+
159
+ # Gets favorites_count (Integer) from existing `data`.
160
+ #
161
+ def favorites_count
162
+ data[:favorites_count]
163
+ end
164
+
165
+ # Returns true if pattern is free (Boolean).
166
+ #
167
+ def free?
168
+ data[:free]
169
+ end
170
+
171
+ # Number of stitches per inch (or 4 inches) (Float).
172
+ #
173
+ def gauge
174
+ data[:gauge]
175
+ end
176
+
177
+ # Sentence description of sts and row gauge with stitch.
178
+ #
179
+ def gauge_description
180
+ data[:gauge_description]
181
+ end
182
+
183
+ # Either 1 or 4 (inches) (Integer).
184
+ #
185
+ def gauge_divisor
186
+ data[:gauge_divisor]
187
+ end
188
+
189
+ # Pattern for gauge listed.
190
+ #
191
+ def gauge_pattern
192
+ data[:gauge_pattern]
193
+ end
194
+
195
+ # Gets patter name from existing `data`.
196
+ #
197
+ def name
198
+ data[:name]
199
+ end
200
+
201
+ # Raw pattern notes. May be mixed Markdown and HTML. Generally only useful when presenting a pattern notes editor.
202
+ #
203
+ def notes_raw
204
+ data[:notes]
205
+ end
206
+
207
+ # Pattern notes rendered as HTML.
208
+ #
209
+ def notes_html
210
+ data[:notes_html]
211
+ end
212
+
213
+ # Returns an array of hashes with tons of information about each yarn listed in the pattern. See {#build_packs} for a complete list of helper methods.
214
+ #
215
+ # I've included this method in case you want to have more control over how your pack information is displayed. It's likely that you'll want to use the other pack methods. While you sacrifice some fine tuning control, you also don't have to worry about dealing with a messy nested hash.
216
+ #
217
+ # If you're really determined to go through this manually, check out the [Ravelry documentation](http://www.ravelry.com/api#Pack_result).
218
+ #
219
+ # If iterating through the `packs` hash, you'll likely want to do something like this:
220
+ #
221
+ # `packs = pattern.packs`
222
+ #
223
+ # **`packs[0][:yarn_name]`** returns a formatted string with the brand and yarn name.
224
+ #
225
+ # *Example: "Wooly Wonka Fibers Artio Sock"*
226
+ #
227
+ def packs_raw
228
+ data[:packs]
229
+ end
230
+
231
+ # Helper that will tell you how many yarns you have in your pack.
232
+ #
233
+ def pack_count
234
+ data[:packs].length
235
+ end
236
+
237
+ # Returns a hash with information about the pattern author.
238
+ #
239
+ # I've included this method in case you want to have more control over how your author information is displayed.
240
+ #
241
+ # See {#build_authors} for more information about directly accessing author information.
242
+ #
243
+ def pattern_author
244
+ data[:pattern_author]
245
+ end
246
+
247
+ # Returns an array of hashes with information about the categories.
248
+ #
249
+ # This method is included so you can access the information directly.
250
+ #
251
+ # See {#build_categories} for more information about directly accessing category information.
252
+ #
253
+ def pattern_categories_raw
254
+ data[:pattern_categories]
255
+ end
256
+
257
+
258
+ # Returns an array of hashes with information about the needle sizes called for. Knitting only.
259
+ #
260
+ # This method is included so you can access the information directly.
261
+ #
262
+ # See {#build_needles} for more information about directly accessing category information.
263
+ #
264
+ def pattern_needle_sizes_raw
265
+ data[:pattern_needle_sizes]
266
+ end
267
+
268
+ # Returns an array of hashes with information about the pattern type.
269
+ #
270
+ # This method is included so you can access the information directly.
271
+ #
272
+ # See {#build_pattern_type} for more information about directly accessing category information.
273
+ #
274
+ def pattern_type_raw
275
+ data[:pattern_type]
276
+ end
277
+
278
+ # Gets pdf_url from existing `data`.
279
+ #
280
+ def pdf_url
281
+ data[:pdf_url]
282
+ end
283
+
284
+ # Gets Ravelry permalink from existing `data`.
285
+ #
286
+ def permalink
287
+ data[:permalink]
288
+ end
289
+
290
+ # Returns an array of hashes with information about photo objects.
291
+ #
292
+ # This method is included so you can access the information directly.
293
+ #
294
+ # See {#build_photos} for more information about directly accessing category information.
295
+ #
296
+ def photos_raw
297
+ data[:photos]
298
+ end
299
+
300
+ # Gets price from existing `data` (Float).
301
+ #
302
+ def price
303
+ data[:price]
304
+ end
305
+
306
+ # Gets product_id from existing `data`.
307
+ #
308
+ def product_id
309
+ data[:product_id]
310
+ end
311
+
312
+ # Gets projects_count from existing `data` (Integer).
313
+ #
314
+ def projects_count
315
+ data[:projects_count]
316
+ end
317
+
318
+ # Gets publication date from existing `data` (Date).
319
+ #
320
+ def published
321
+ Date.parse(data[:published])
322
+ end
323
+
324
+ # Gets number of queued projects from existing `data` (Integer).
325
+ #
326
+ def queued_projects_count
327
+ data[:queued_projects_count]
328
+ end
329
+
330
+ # Gets rating_average from existing `data` (Float).
331
+ #
332
+ def rating_average
333
+ data[:rating_average]
334
+ end
335
+
336
+ # Gets number of ratings from existing `data` (Integer).
337
+ #
338
+ def rating_count
339
+ data[:rating_count]
340
+ end
341
+
342
+ # Returns true if pattern is a Ravelry download (Boolean).
343
+ #
344
+ def ravelry_download?
345
+ data[:ravelry_download]
346
+ end
347
+
348
+ # Gets row gauge from existing `data` (Float).
349
+ #
350
+ def row_gauge
351
+ data[:row_gauge]
352
+ end
353
+
354
+ # Gets sizes available from existing `data`.
355
+ #
356
+ def sizes_available
357
+ data[:sizes_available]
358
+ end
359
+
360
+ # Gets url from existing `data`.
361
+ #
362
+ def url
363
+ data[:url]
364
+ end
365
+
366
+ # Gets yardage required from existing `data` (Integer).
367
+ #
368
+ def yardage
369
+ data[:yardage]
370
+ end
371
+
372
+ # Gets nice sentence yardage description with range from existing `data`.
373
+ #
374
+ def yardage_description
375
+ data[:yardage_description]
376
+ end
377
+
378
+ # Gets max yards required from existing `data` (Integer).
379
+ #
380
+ def yardage_max
381
+ data[:yardage_max]
382
+ end
383
+
384
+ # Gets primary yarn weight description from existing `data`.
385
+ #
386
+ def yarn_weight_description
387
+ data[:yarn_weight_description]
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,54 @@
1
+ module Ravelry
2
+
3
+ # The information used to create `Ravelry::PatternNeedle` objects comes from {Ravelry::Pattern} objects.
4
+ #
5
+ # You should not create `PatternNeedle` objects manually; they are all created–and owned–by a {Ravelry::Pattern}.
6
+ #
7
+ # Note that there are other API endpoints for Needles; this might not be the one that you're looking for.
8
+ #
9
+ # See {Ravelry::Pattern} for more information about `Pattern` objects.
10
+ #
11
+ class PatternNeedle
12
+
13
+ attr_reader :type, :hook, :us, :metric, :knitting
14
+
15
+ def initialize(needle)
16
+ @needle = needle
17
+ # Crochet hook size.
18
+ #
19
+ @hook = needle[:hook]
20
+ # US size for knitting needles (Integer or Float).
21
+ #
22
+ @us = needle[:us]
23
+ # Metric size for knitting needles (Integer or Float).
24
+ #
25
+ @metric = needle[:metric]
26
+ # Combination of US and metric.
27
+ # Example: "US 4 - 3.5 mm"
28
+ #
29
+ @knitting = needle[:name]
30
+ end
31
+
32
+ # Sets @type to 'knitting' or 'crochet'.
33
+ #
34
+ def type
35
+ @type = 'knitting' if @needle[:knitting]
36
+ @type = 'crochet' if @needle[:crochet]
37
+ @type
38
+ end
39
+
40
+ # Takes the US size and turns it into a sentence.
41
+ # Example: "US 4"
42
+ #
43
+ def us_string
44
+ "US #{@us}".strip
45
+ end
46
+
47
+ # Takes the metric size and turns it into a sentence.
48
+ # Example: "3.5 mm"
49
+ #
50
+ def metric_string
51
+ "#{@metric} mm"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,34 @@
1
+ module Ravelry
2
+
3
+ # The information used to create `Ravelry::PatternType` objects comes from {Ravelry::Pattern} objects.
4
+ #
5
+ # See {Ravelry::Pattern} for more information about `Pattern` objects.
6
+ #
7
+ # You should not create `PatternType` objects manually; they are all created–and owned–by a {Ravelry::Pattern}.
8
+ #
9
+ # Note that there are other API endpoints for pattern categorization; this might not be the one that you're looking for.
10
+ #
11
+ # See {Ravelry::Pattern} for more information about `Pattern` objects.
12
+ #
13
+ class PatternType
14
+
15
+ attr_reader :permalink, :name
16
+
17
+ def initialize(type)
18
+ @type = type
19
+
20
+ # Ravelry vanity permalink for the type. Note: not a full URL.
21
+ #
22
+ @permalink = type[:permalink]
23
+
24
+ # Name of the type.
25
+ #
26
+ @name = type[:name]
27
+ end
28
+
29
+ # Boolean value; determins if category qualifies as clothing.
30
+ def clothing?
31
+ @type[:clothing]
32
+ end
33
+ end
34
+ end