class Ravelry::Pattern

`Ravelry::Pattern` corresponds to Pattern objects in Ravelry.

This class requires your configuration variables be set (see {file:README.md 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`:

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.

@example

Ravelry::Pattern.search('socks')
# => [#<Pattern>, ...]

Constants

COMMENT_OPTIONS

Whitelist and default options for fetching comments.

Attributes

author[R]
categories[R]
craft[R]
needles[R]
packs[R]
photos[R]
printings[R]
type[R]
yarn_weights[R]
yarns[R]

Public Class Methods

Public Instance Methods

build() click to toggle source

Creates all objects associated with your pattern; returns nothing; sets `attr_readers`.

@return nil

# File lib/ravelry/pattern.rb, line 211
def build
  @author = Build.author(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={}) click to toggle source

Get the list of comments on a pattern object.

To query comments for a pattern you haven't fetched yet, without fetching the pattern:

@example

pattern = Ravelry::Pattern.new
pattern.id = "000000"
pattern.comments

To query comments for a pattern you've already fetched, follow the steps above and call `pattern.comments`.

See <COMMENT_OPTIONS> for valid options.

@option options [String] :sort @option options [Integer] :page_size @option options [Array] :include @option options [Integer] :page @return [Array<Comment>] an array of `Comment`s

# File lib/ravelry/pattern.rb, line 166
def comments(options={})
  @comment_list ||= []
  return @comment_list if @comment_list.any?

  fetch_comments(options)
end
comments_count() click to toggle source

Gets comments_count from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 229
def comments_count
  data[:comments_count]
end
currency() click to toggle source

Gets currency from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 237
def currency
  data[:currency]
end
currency_symbol() click to toggle source

Gets currency_symbol from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 245
def currency_symbol
  data[:currency_symbol]
end
difficulty_average_float() click to toggle source

Returns the difficult average as a Float (this is how it is stored by Ravelry).

@return Float

# File lib/ravelry/pattern.rb, line 253
def difficulty_average_float
  data[:difficulty_average]
end
difficulty_average_integer() click to toggle source

Returns the difficulty average rounded up or down to an Integer.

@return Integer

# File lib/ravelry/pattern.rb, line 261
def difficulty_average_integer
  difficulty_average_float.round(0)
end
difficulty_count() click to toggle source

Gets difficulty_count (Integer) from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 269
def difficulty_count
  data[:difficulty_count]
end
downloadable?() click to toggle source

Returns true if the pattern can be downloaded.

@return Boolean

# File lib/ravelry/pattern.rb, line 277
def downloadable?
  data[:downloadable]
end
favorites_count() click to toggle source

Gets favorites_count (Integer) from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 285
def favorites_count
  data[:favorites_count]
end
free?() click to toggle source

Returns true if pattern is free.

@return Boolean

# File lib/ravelry/pattern.rb, line 293
def free?
  data[:free]
end
gauge() click to toggle source

Number of stitches per inch (or 4 inches).

@return Float

# File lib/ravelry/pattern.rb, line 301
def gauge
  data[:gauge]
end
gauge_description() click to toggle source

Sentence description of sts and row gauge with stitch.

@return String

# File lib/ravelry/pattern.rb, line 309
def gauge_description
  data[:gauge_description]
end
gauge_divisor() click to toggle source

Either 1 or 4 (inches).

@return Integer

# File lib/ravelry/pattern.rb, line 317
def gauge_divisor
  data[:gauge_divisor]
end
gauge_pattern() click to toggle source

Pattern for gauge listed.

@return String

# File lib/ravelry/pattern.rb, line 325
def gauge_pattern
  data[:gauge_pattern]
end
get() click to toggle source

Handles GET API call and parses JSON response.

Corresponds to Ravelry API endpoint `Patterns#show`

# File lib/ravelry/pattern.rb, line 126
def get
  @data = Utils::Request.get("patterns/#{@id}.json", :pattern)
  self
end
json() click to toggle source

Returns the original raw JSON.

# File lib/ravelry/pattern.rb, line 203
def json
  data
end
name() click to toggle source

Gets patter name from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 333
def name
  data[:name]
end
notes_html() click to toggle source

Pattern notes rendered as HTML.

@return String

# File lib/ravelry/pattern.rb, line 349
def notes_html
  data[:notes_html]
end
notes_raw() click to toggle source

Raw pattern notes. May be mixed Markdown and HTML. Generally only useful when presenting a pattern notes editor.

@return String

# File lib/ravelry/pattern.rb, line 341
def notes_raw
  data[:notes]
end
pack_count() click to toggle source

Helper that will tell you how many yarns you have in your pack.

@return Integer

# File lib/ravelry/pattern.rb, line 376
def pack_count
  data[:packs].length
end
packs_raw() click to toggle source

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:

@example

packs = pattern.packs
packs[0][:yarn_name]
# returns a formatted string with the brand and yarn name.

@return [Array<Hash>]

# File lib/ravelry/pattern.rb, line 368
def packs_raw
  data[:packs]
end
pattern_author() click to toggle source

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 {Ravelry::Author} for more information about directly accessing author information.

@return Hash

# File lib/ravelry/pattern.rb, line 388
def pattern_author
  data[:pattern_author]
end
pattern_categories_raw() click to toggle source

Returns an array of hashes with information about the categories.

This method is included so you can access the information directly.

See {Ravelry::Category} for more information about directly accessing category information.

@return [Array<Hash>]

# File lib/ravelry/pattern.rb, line 400
def pattern_categories_raw
  data[:pattern_categories]
end
pattern_needle_sizes_raw() click to toggle source

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 {Ravelry::Needle} for more information about directly accessing category information.

@return [Array<Hash>]

# File lib/ravelry/pattern.rb, line 413
def pattern_needle_sizes_raw
  data[:pattern_needle_sizes]
end
pattern_type_raw() click to toggle source

Returns an array of hashes with information about the pattern type.

This method is included so you can access the information directly.

See {Ravelry::Needle} for more information about directly accessing category information.

@return [Array<Hash>]

# File lib/ravelry/pattern.rb, line 425
def pattern_type_raw
  data[:pattern_type]
end
pdf_url() click to toggle source

Gets pdf_url from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 433
def pdf_url
  data[:pdf_url]
end
photos_raw() click to toggle source

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.

@return [Array<Hash>]

# File lib/ravelry/pattern.rb, line 453
def photos_raw
  data[:photos]
end
price() click to toggle source

Gets price from existing `data`.

@return Float

# File lib/ravelry/pattern.rb, line 461
def price
  data[:price]
end
product_id() click to toggle source

Gets product_id from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 469
def product_id
  data[:product_id]
end
projects_count() click to toggle source

Gets projects_count from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 477
def projects_count
  data[:projects_count]
end
published() click to toggle source

Gets publication date from existing `data`.

@return Date

# File lib/ravelry/pattern.rb, line 485
def published
  Date.parse(data[:published])
end
queued_projects_count() click to toggle source

Gets number of queued projects from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 493
def queued_projects_count
  data[:queued_projects_count]
end
rating_average() click to toggle source

Gets rating_average from existing `data`.

@return Float

# File lib/ravelry/pattern.rb, line 501
def rating_average
  data[:rating_average]
end
rating_count() click to toggle source

Gets number of ratings from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 509
def rating_count
  data[:rating_count]
end
ravelry_download?() click to toggle source

Returns true if pattern is a Ravelry download.

@return Boolean

# File lib/ravelry/pattern.rb, line 517
def ravelry_download?
  data[:ravelry_download]
end
row_gauge() click to toggle source

Gets row gauge from existing `data`.

@return Float

# File lib/ravelry/pattern.rb, line 525
def row_gauge
  data[:row_gauge]
end
sizes_available() click to toggle source

Gets sizes available from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 533
def sizes_available
  data[:sizes_available]
end
url() click to toggle source

Gets url from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 541
def url
  data[:url]
end
yardage() click to toggle source

Gets yardage required from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 549
def yardage
  data[:yardage]
end
yardage_description() click to toggle source

Gets nice sentence yardage description with range from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 557
def yardage_description
  data[:yardage_description]
end
yardage_max() click to toggle source

Gets max yards required from existing `data`.

@return Integer

# File lib/ravelry/pattern.rb, line 565
def yardage_max
  data[:yardage_max]
end
yarn_weight_description() click to toggle source

Gets primary yarn weight description from existing `data`.

@return String

# File lib/ravelry/pattern.rb, line 573
def yarn_weight_description
  data[:yarn_weight_description]
end

Private Instance Methods

fetch_comments(options) click to toggle source
# File lib/ravelry/pattern.rb, line 578
def fetch_comments(options)
  options.keep_if { |key| COMMENT_OPTIONS.keys.include?(key) }
  if options[:per_page]
    options[:per_page] = 100 if options[:per_page] > 100
  end

  comment_data = Utils::Request.get("patterns/#{@id}/comments.json", :comments, options)
  comment_data.map { |comment| Comment.new(comment) }
end