Class: Ravelry::Pattern
- Includes:
- Build
- Defined in:
- lib/ravelry/pattern.rb
Overview
‘Ravelry::Pattern` corresponds to Pattern objects in Ravelry.
This class requires your configuration variables be set (see README). API calls are authenticated using HTTP Basic Auth unless otherwise noted.
If your ‘pattern.data` is missing one of the attributes below, that method will return `nil`.
#Pattern objects
To create an empty object:
“‘ruby pattern = Ravelry::Pattern.new “`
To complete the ‘GET` request, set the `id` and run:
“‘ruby pattern.id = “000000” pattern.get “`
After calling ‘get`, you have access to all of the class methods below.
If you do not have the pattern ID, you may use the permalink:
Ravelry URL: www.ravelry.com/patterns/library/traveling-woman
Request:
“‘ruby pattern = Ravelry::Pattern.new pattern.permalink_get(’traveling-woman’) “‘
##Initializing with an id
Optionally, you can initialize with an id:
“‘ruby pattern = Ravelry::Pattern.new(id) “`
And then run your get request:
“‘ruby pattern.get “`
##Loading existing pattern data
If you have existing pattern data, you should initialize as follows:
“‘ruby pattern = Ravelry::Pattern.new pattern.data = my_data “`
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.
# Pattern data
After you have pattern data from the API, you have access to all of the pattern attributes through the class methods (see documentation). Example:
“‘ruby pattern.free? # => true “`
# Building associated objects
You will need to call special methods to create the associated objects with your pattern.
To create all associated objects at once, call the following method after initialization:
“‘ruby pattern.build “`
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.
This will create the following objects and readers from the existing ‘data`:
-
‘pattern.author` - a Author object
-
‘pattern.categories` - an array of Category objects
-
‘pattern.craft` - a Craft object
-
‘pattern.needles` - an array of Needle objects
-
‘pattern.packs` - array of Pack objects
-
‘pattern.photos` - an array of Photo objects
-
‘pattern.printings` - an array of Raverly::Printing objects
-
‘pattern.type` - a PatternType object
-
‘pattern.yarns` - array of Yarn objects
-
‘pattern.yarn_weights` - array of YarnWeight objects
See the documentation for each object’s available methods.
# Searching
To search for patterns, use the ‘Pattern.search` class method, which returns an array of patterns.
Constant Summary collapse
- COMMENT_OPTIONS =
Whitelist and default options for fetching comments.
{ sort: ['time', 'helpful', 'time_', 'helpful_'], page_size: 25, include: ['highlighted_project'], page: 1 }
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#craft ⇒ Object
readonly
Returns the value of attribute craft.
-
#needles ⇒ Object
readonly
Returns the value of attribute needles.
-
#packs ⇒ Object
readonly
Returns the value of attribute packs.
-
#photos ⇒ Object
readonly
Returns the value of attribute photos.
-
#printings ⇒ Object
readonly
Returns the value of attribute printings.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#yarn_weights ⇒ Object
readonly
Returns the value of attribute yarn_weights.
-
#yarns ⇒ Object
readonly
Returns the value of attribute yarns.
Attributes inherited from Data
Class Method Summary collapse
-
.search(query, options = {}) ⇒ Array<Pattern>
Search for patterns.
Instance Method Summary collapse
-
#build ⇒ Object
Creates all objects associated with your pattern; returns nothing; sets ‘attr_readers`.
-
#comments(options = {}) ⇒ Array<Comment>
Get the list of comments on a pattern object.
-
#comments_count ⇒ Object
Gets comments_count from existing ‘data`.
-
#currency ⇒ Object
Gets currency from existing ‘data`.
-
#currency_symbol ⇒ Object
Gets currency_symbol from existing ‘data`.
-
#difficulty_average_float ⇒ Object
Returns the difficult average as a Float (this is how it is stored by Ravelry).
-
#difficulty_average_integer ⇒ Object
Returns the difficulty average rounded up or down to an Integer.
-
#difficulty_count ⇒ Object
Gets difficulty_count (Integer) from existing ‘data`.
-
#downloadable? ⇒ Boolean
Returns true if the pattern can be downloaded.
-
#favorites_count ⇒ Object
Gets favorites_count (Integer) from existing ‘data`.
-
#free? ⇒ Boolean
Returns true if pattern is free.
-
#gauge ⇒ Object
Number of stitches per inch (or 4 inches).
-
#gauge_description ⇒ Object
Sentence description of sts and row gauge with stitch.
-
#gauge_divisor ⇒ Object
Either 1 or 4 (inches).
-
#gauge_pattern ⇒ Object
Pattern for gauge listed.
-
#get ⇒ Object
Handles GET API call and parses JSON response.
-
#json ⇒ Object
Returns the original raw JSON.
-
#name ⇒ Object
Gets patter name from existing ‘data`.
-
#notes_html ⇒ Object
Pattern notes rendered as HTML.
-
#notes_raw ⇒ Object
Raw pattern notes.
-
#pack_count ⇒ Object
Helper that will tell you how many yarns you have in your pack.
-
#packs_raw ⇒ Array<Hash>
Returns an array of hashes with tons of information about each yarn listed in the pattern.
-
#pattern_author ⇒ Object
Returns a hash with information about the pattern author.
-
#pattern_categories_raw ⇒ Array<Hash>
Returns an array of hashes with information about the categories.
-
#pattern_needle_sizes_raw ⇒ Array<Hash>
Returns an array of hashes with information about the needle sizes called for.
-
#pattern_type_raw ⇒ Array<Hash>
Returns an array of hashes with information about the pattern type.
-
#pdf_url ⇒ Object
Gets pdf_url from existing ‘data`.
-
#permalink ⇒ Object
Gets Ravelry permalink from existing ‘data`.
-
#permalink_get(permalink) ⇒ Object
Alternative method for the GET API call.
-
#photos_raw ⇒ Array<Hash>
Returns an array of hashes with information about photo objects.
-
#price ⇒ Object
Gets price from existing ‘data`.
-
#product_id ⇒ Object
Gets product_id from existing ‘data`.
-
#projects_count ⇒ Object
Gets projects_count from existing ‘data`.
-
#published ⇒ Object
Gets publication date from existing ‘data`.
-
#queued_projects_count ⇒ Object
Gets number of queued projects from existing ‘data`.
-
#rating_average ⇒ Object
Gets rating_average from existing ‘data`.
-
#rating_count ⇒ Object
Gets number of ratings from existing ‘data`.
-
#ravelry_download? ⇒ Boolean
Returns true if pattern is a Ravelry download.
-
#row_gauge ⇒ Object
Gets row gauge from existing ‘data`.
-
#sizes_available ⇒ Object
Gets sizes available from existing ‘data`.
-
#url ⇒ Object
Gets url from existing ‘data`.
-
#yardage ⇒ Object
Gets yardage required from existing ‘data`.
-
#yardage_description ⇒ Object
Gets nice sentence yardage description with range from existing ‘data`.
-
#yardage_max ⇒ Object
Gets max yards required from existing ‘data`.
-
#yarn_weight_description ⇒ Object
Gets primary yarn weight description from existing ‘data`.
Methods included from Build
author, categories, craft, needles, packs, pattern_type, photos, printings, user_sites, yarn_weights, yarns
Methods inherited from Data
Constructor Details
This class inherits a constructor from Ravelry::Data
Instance Attribute Details
#author ⇒ Object (readonly)
Returns the value of attribute author.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def @author end |
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def categories @categories end |
#craft ⇒ Object (readonly)
Returns the value of attribute craft.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def craft @craft end |
#needles ⇒ Object (readonly)
Returns the value of attribute needles.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def needles @needles end |
#packs ⇒ Object (readonly)
Returns the value of attribute packs.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def packs @packs end |
#photos ⇒ Object (readonly)
Returns the value of attribute photos.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def photos @photos end |
#printings ⇒ Object (readonly)
Returns the value of attribute printings.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def printings @printings end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def type @type end |
#yarn_weights ⇒ Object (readonly)
Returns the value of attribute yarn_weights.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def yarn_weights @yarn_weights end |
#yarns ⇒ Object (readonly)
Returns the value of attribute yarns.
120 121 122 |
# File 'lib/ravelry/pattern.rb', line 120 def yarns @yarns end |
Class Method Details
.search(query, options = {}) ⇒ Array<Pattern>
Search for patterns.
Corresponds to Ravelry API endpoint ‘Patterns#search`
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ravelry/pattern.rb', line 183 def self.search(query, ={}) params = {query: query} params.merge!() unless params[:personal_attributes].nil? params[:personal_attributes] = (params[:personal_attributes] ? 1 : 0) end patterns = Utils::Request .get("patterns/search.json", :patterns, params) patterns.map do |data| pattern = Ravelry::Pattern.new pattern.data = data pattern end end |
Instance Method Details
#build ⇒ Object
Creates all objects associated with your pattern; returns nothing; sets ‘attr_readers`.
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ravelry/pattern.rb', line 211 def build @author = Build.(data) @categories = Build.categories(data) @craft = Build.craft(data) @needles = Build.needles(data) @packs = Build.packs(data) @photos = Build.photos(data) @printings = Build.printings(data) @type = Build.pattern_type(data) @yarns = Build.yarns(data) @yarn_weights = Build.yarn_weights(data) nil end |
#comments(options = {}) ⇒ Array<Comment>
Get the list of comments on a pattern object.
To query comments for a pattern you haven’t fetched yet, without fetching the pattern:
To query comments for a pattern you’ve already fetched, follow the steps above and call ‘pattern.comments`.
See <COMMENT_OPTIONS> for valid options.
166 167 168 169 170 171 |
# File 'lib/ravelry/pattern.rb', line 166 def comments(={}) @comment_list ||= [] return @comment_list if @comment_list.any? fetch_comments() end |
#comments_count ⇒ Object
Gets comments_count from existing ‘data`.
229 230 231 |
# File 'lib/ravelry/pattern.rb', line 229 def comments_count data[:comments_count] end |
#currency ⇒ Object
Gets currency from existing ‘data`.
237 238 239 |
# File 'lib/ravelry/pattern.rb', line 237 def currency data[:currency] end |
#currency_symbol ⇒ Object
Gets currency_symbol from existing ‘data`.
245 246 247 |
# File 'lib/ravelry/pattern.rb', line 245 def currency_symbol data[:currency_symbol] end |
#difficulty_average_float ⇒ Object
Returns the difficult average as a Float (this is how it is stored by Ravelry).
253 254 255 |
# File 'lib/ravelry/pattern.rb', line 253 def difficulty_average_float data[:difficulty_average] end |
#difficulty_average_integer ⇒ Object
Returns the difficulty average rounded up or down to an Integer.
261 262 263 |
# File 'lib/ravelry/pattern.rb', line 261 def difficulty_average_integer difficulty_average_float.round(0) end |
#difficulty_count ⇒ Object
Gets difficulty_count (Integer) from existing ‘data`.
269 270 271 |
# File 'lib/ravelry/pattern.rb', line 269 def difficulty_count data[:difficulty_count] end |
#downloadable? ⇒ Boolean
Returns true if the pattern can be downloaded.
277 278 279 |
# File 'lib/ravelry/pattern.rb', line 277 def downloadable? data[:downloadable] end |
#favorites_count ⇒ Object
Gets favorites_count (Integer) from existing ‘data`.
285 286 287 |
# File 'lib/ravelry/pattern.rb', line 285 def favorites_count data[:favorites_count] end |
#free? ⇒ Boolean
Returns true if pattern is free.
293 294 295 |
# File 'lib/ravelry/pattern.rb', line 293 def free? data[:free] end |
#gauge ⇒ Object
Number of stitches per inch (or 4 inches).
301 302 303 |
# File 'lib/ravelry/pattern.rb', line 301 def gauge data[:gauge] end |
#gauge_description ⇒ Object
Sentence description of sts and row gauge with stitch.
309 310 311 |
# File 'lib/ravelry/pattern.rb', line 309 def gauge_description data[:gauge_description] end |
#gauge_divisor ⇒ Object
Either 1 or 4 (inches).
317 318 319 |
# File 'lib/ravelry/pattern.rb', line 317 def gauge_divisor data[:gauge_divisor] end |
#gauge_pattern ⇒ Object
Pattern for gauge listed.
325 326 327 |
# File 'lib/ravelry/pattern.rb', line 325 def gauge_pattern data[:gauge_pattern] end |
#get ⇒ Object
Handles GET API call and parses JSON response.
Corresponds to Ravelry API endpoint ‘Patterns#show`
126 127 128 129 |
# File 'lib/ravelry/pattern.rb', line 126 def get @data = Utils::Request.get("patterns/#{@id}.json", :pattern) self end |
#json ⇒ Object
Returns the original raw JSON.
203 204 205 |
# File 'lib/ravelry/pattern.rb', line 203 def json data end |
#name ⇒ Object
Gets patter name from existing ‘data`.
333 334 335 |
# File 'lib/ravelry/pattern.rb', line 333 def name data[:name] end |
#notes_html ⇒ Object
Pattern notes rendered as HTML.
349 350 351 |
# File 'lib/ravelry/pattern.rb', line 349 def notes_html data[:notes_html] end |
#notes_raw ⇒ Object
Raw pattern notes. May be mixed Markdown and HTML. Generally only useful when presenting a pattern notes editor.
341 342 343 |
# File 'lib/ravelry/pattern.rb', line 341 def notes_raw data[:notes] end |
#pack_count ⇒ Object
Helper that will tell you how many yarns you have in your pack.
376 377 378 |
# File 'lib/ravelry/pattern.rb', line 376 def pack_count data[:packs].length end |
#packs_raw ⇒ Array<Hash>
Returns an array of hashes with tons of information about each yarn listed in the pattern. See #build for a complete list of helper methods.
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.
If you’re really determined to go through this manually, check out the [Ravelry documentation](www.ravelry.com/api#Pack_result).
If iterating through the ‘packs` hash, you’ll likely want to do something like this:
368 369 370 |
# File 'lib/ravelry/pattern.rb', line 368 def packs_raw data[:packs] end |
#pattern_author ⇒ Object
Returns a hash with information about the pattern author.
I’ve included this method in case you want to have more control over how your author information is displayed.
See Author for more information about directly accessing author information.
388 389 390 |
# File 'lib/ravelry/pattern.rb', line 388 def data[:pattern_author] end |
#pattern_categories_raw ⇒ Array<Hash>
Returns an array of hashes with information about the categories.
This method is included so you can access the information directly.
See Category for more information about directly accessing category information.
400 401 402 |
# File 'lib/ravelry/pattern.rb', line 400 def pattern_categories_raw data[:pattern_categories] end |
#pattern_needle_sizes_raw ⇒ Array<Hash>
Returns an array of hashes with information about the needle sizes called for. Knitting only.
This method is included so you can access the information directly.
See Needle for more information about directly accessing category information.
413 414 415 |
# File 'lib/ravelry/pattern.rb', line 413 def pattern_needle_sizes_raw data[:pattern_needle_sizes] end |
#pattern_type_raw ⇒ Array<Hash>
Returns an array of hashes with information about the pattern type.
This method is included so you can access the information directly.
See Needle for more information about directly accessing category information.
425 426 427 |
# File 'lib/ravelry/pattern.rb', line 425 def pattern_type_raw data[:pattern_type] end |
#pdf_url ⇒ Object
Gets pdf_url from existing ‘data`.
433 434 435 |
# File 'lib/ravelry/pattern.rb', line 433 def pdf_url data[:pdf_url] end |
#permalink ⇒ Object
Gets Ravelry permalink from existing ‘data`.
441 442 443 |
# File 'lib/ravelry/pattern.rb', line 441 def permalink data[:permalink] end |
#permalink_get(permalink) ⇒ Object
Alternative method for the GET API call.
Corresponds to Ravelry API endpoint ‘Patterns#show`
Uses the pattern’s Ravelry permalink instead of ID. Useful if you don’t know the ID of a pattern, but have the permalink.
142 143 144 145 |
# File 'lib/ravelry/pattern.rb', line 142 def permalink_get(permalink) @data = Utils::Request.get("patterns/#{permalink}.json", :pattern) self end |
#photos_raw ⇒ Array<Hash>
Returns an array of hashes with information about photo objects.
This method is included so you can access the information directly.
See Ravelry::Photo for more information about directly accessing category information.
453 454 455 |
# File 'lib/ravelry/pattern.rb', line 453 def photos_raw data[:photos] end |
#price ⇒ Object
Gets price from existing ‘data`.
461 462 463 |
# File 'lib/ravelry/pattern.rb', line 461 def price data[:price] end |
#product_id ⇒ Object
Gets product_id from existing ‘data`.
469 470 471 |
# File 'lib/ravelry/pattern.rb', line 469 def product_id data[:product_id] end |
#projects_count ⇒ Object
Gets projects_count from existing ‘data`.
477 478 479 |
# File 'lib/ravelry/pattern.rb', line 477 def projects_count data[:projects_count] end |
#published ⇒ Object
Gets publication date from existing ‘data`.
485 486 487 |
# File 'lib/ravelry/pattern.rb', line 485 def published Date.parse(data[:published]) end |
#queued_projects_count ⇒ Object
Gets number of queued projects from existing ‘data`.
493 494 495 |
# File 'lib/ravelry/pattern.rb', line 493 def queued_projects_count data[:queued_projects_count] end |
#rating_average ⇒ Object
Gets rating_average from existing ‘data`.
501 502 503 |
# File 'lib/ravelry/pattern.rb', line 501 def data[:rating_average] end |
#rating_count ⇒ Object
Gets number of ratings from existing ‘data`.
509 510 511 |
# File 'lib/ravelry/pattern.rb', line 509 def data[:rating_count] end |
#ravelry_download? ⇒ Boolean
Returns true if pattern is a Ravelry download.
517 518 519 |
# File 'lib/ravelry/pattern.rb', line 517 def ravelry_download? data[:ravelry_download] end |
#row_gauge ⇒ Object
Gets row gauge from existing ‘data`.
525 526 527 |
# File 'lib/ravelry/pattern.rb', line 525 def row_gauge data[:row_gauge] end |
#sizes_available ⇒ Object
Gets sizes available from existing ‘data`.
533 534 535 |
# File 'lib/ravelry/pattern.rb', line 533 def sizes_available data[:sizes_available] end |
#url ⇒ Object
Gets url from existing ‘data`.
541 542 543 |
# File 'lib/ravelry/pattern.rb', line 541 def url data[:url] end |
#yardage ⇒ Object
Gets yardage required from existing ‘data`.
549 550 551 |
# File 'lib/ravelry/pattern.rb', line 549 def yardage data[:yardage] end |
#yardage_description ⇒ Object
Gets nice sentence yardage description with range from existing ‘data`.
557 558 559 |
# File 'lib/ravelry/pattern.rb', line 557 def yardage_description data[:yardage_description] end |
#yardage_max ⇒ Object
Gets max yards required from existing ‘data`.
565 566 567 |
# File 'lib/ravelry/pattern.rb', line 565 def yardage_max data[:yardage_max] end |
#yarn_weight_description ⇒ Object
Gets primary yarn weight description from existing ‘data`.
573 574 575 |
# File 'lib/ravelry/pattern.rb', line 573 def yarn_weight_description data[:yarn_weight_description] end |