lib/ravelry/pattern.rb in ravelry-0.0.7 vs lib/ravelry/pattern.rb in ravelry-0.0.8
- old
+ new
@@ -20,10 +20,21 @@
# 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: http://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:
#
@@ -94,10 +105,17 @@
# # => [#<Pattern>, ...]
# ```
#
class Pattern < Data
+ COMMENT_OPTIONS = {
+ sort: ['time', 'helpful', 'time_', 'helpful_'],
+ page_size: 25,
+ include: ['highlighted_project'],
+ page: 1
+ }
+
include Build
attr_reader :author, :categories, :craft, :needles, :packs, :photos, :printings, :type, :yarns, :yarn_weights
# Handles GET API call and parses JSON response.
@@ -106,10 +124,50 @@
#
def get
@data = Utils::Request.get("patterns/#{@id}.json", :pattern)
end
+ # 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.
+ #
+ # **Example**
+ #
+ # Ravelry URL: http://www.ravelry.com/patterns/library/traveling-woman
+ #
+ # Request:
+ #
+ # ```ruby
+ # pattern = Ravelry::Pattern.new
+ # pattern.permalink_get('traveling-woman')
+ # ```
+ #
+ def permalink_get(permalink)
+ @data = Utils::Request.get("patterns/#{permalink}.json", :pattern)
+ end
+
+ # Get the list of comments on a pattern object.
+ #
+ # To query comments for a pattern you haven't fetched yet, without fetching the pattern:
+ #
+ # ```ruby
+ # 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`.
+ #
+ def comments(options={})
+ @comment_list ||= []
+ return @comment_list if @comment_list.any?
+
+ fetch_comments(options)
+ end
+
# Search for patterns.
#
# Corresponds to Ravelry API endpoint `Patterns#search`
#
# @param query [String] required
@@ -273,42 +331,42 @@
# 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 {#build_authors} for more information about directly accessing author information.
+ # See {Ravelry::Author} for more information about directly accessing author information.
#
def pattern_author
data[:pattern_author]
end
# Returns an array of hashes with information about the categories.
#
# This method is included so you can access the information directly.
#
- # See {#build_categories} for more information about directly accessing category information.
+ # See {Ravelry::Category} for more information about directly accessing category information.
#
def pattern_categories_raw
data[:pattern_categories]
end
# 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 {#build_needles} for more information about directly accessing category information.
+ # See {Ravelry::Needle} for more information about directly accessing category information.
#
def pattern_needle_sizes_raw
data[:pattern_needle_sizes]
end
# Returns an array of hashes with information about the pattern type.
#
# This method is included so you can access the information directly.
#
- # See {#build_pattern_type} for more information about directly accessing category information.
+ # See {Ravelry::Needle} for more information about directly accessing category information.
#
def pattern_type_raw
data[:pattern_type]
end
@@ -326,11 +384,11 @@
# Returns an array of hashes with information about photo objects.
#
# This method is included so you can access the information directly.
#
- # See {#build_photos} for more information about directly accessing category information.
+ # See {Ravelry::Photo} for more information about directly accessing category information.
#
def photos_raw
data[:photos]
end
@@ -420,8 +478,19 @@
# Gets primary yarn weight description from existing `data`.
#
def yarn_weight_description
data[:yarn_weight_description]
+ end
+
+ private
+ 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
end
end