Class: AppStore::Category
Overview
A category like categories on the AppStore. Available attributes:
-
item-count: total items count for this category. -
title: title for the category.
Examples
Fetch featured categories
@categories = AppStore::Category.featured
@categories.class # => Array
@categories.length # => 20
Use category
@category = @categories.first
@category.title # => "Games"
@category.item_count # => 1573
Category by id
@category = AppStore::Category.find_by_id(6014)
@category.title # => "Games"
Iterate through subcategories and applications
This example will display all categories with all applications available in the current AppStore
def go_deeper(category)
puts "Category #{category.title}"
category.items.each do |item|
if item.is_a?(AppStore::Category)
go_deeper item
else
puts " => #{item.title} has id #{item.item_id}"
end
end
end
AppStore::Category.featured.each {|category| go_deeper category}
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.featured(options = {}) ⇒ Object
Returns an array of featured categories (main categories).
-
.find_by_id(id, options = {}) ⇒ Object
Search a Category by its
id.
Instance Method Summary collapse
-
#item_id ⇒ Object
Returns id for this category.
-
#items ⇒ Object
Returns an instance of List which contains items elements.
Methods inherited from Base
Methods included from Helper::Plist
Constructor Details
This class inherits a constructor from AppStore::Base
Class Method Details
.featured(options = {}) ⇒ Object
Returns an array of featured categories (main categories). It is the same list as the one displayed in the iPhone AppStore.
46 47 48 49 50 |
# File 'lib/app_store/category.rb', line 46 def self.featured( = {}) client = [:client] || AppStore::Client.new plist = client.get(AppStore::Client::FeaturedCategoriesURL) plist['items'].collect { |item| new :client => client, :plist => item } end |
.find_by_id(id, options = {}) ⇒ Object
Search a Category by its id. Accepts only one id and returns a Category instance.
53 54 55 56 57 58 |
# File 'lib/app_store/category.rb', line 53 def self.find_by_id(id, = {}) client = [:client] || AppStore::Client.new new :item_id => id, :client => client, :plist => client.get(AppStore::Client::CategoryURL, :id => id) end |
Instance Method Details
#item_id ⇒ Object
Returns id for this category
61 62 63 |
# File 'lib/app_store/category.rb', line 61 def item_id @item_id ||= @raw['url'].match("mobile-software-applications/id([0-9]+)")[1] end |
#items ⇒ Object
Returns an instance of List which contains items elements. Each element in the list can be either another category (subcategory) or a Link to an application.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/app_store/category.rb', line 67 def items if @items.nil? plist = @raw['items'] ? @raw : @client.get(@raw['url']) @items = AppStore::List.new( :client => @client, :list => plist['items'], :element_type => 'link', :element_initializer => lambda {|element| (element['link-type'] == 'software' ? AppStore::Link : AppStore::Category).new(:client => @client, :plist => element) } ) end @items end |