Class: AppStore::Category

Inherits:
Base
  • Object
show all
Defined in:
lib/app_store/category.rb

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

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #store_front_name

Methods included from Helper::Plist

#plist

Constructor Details

This class inherits a constructor from AppStore::Base

Class Method Details

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(options = {})
  client = options[: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, options = {})
  client = options[:client] || AppStore::Client.new
  new :item_id  => id,
      :client   => client,
      :plist    => client.get(AppStore::Client::CategoryURL, :id => id)
end

Instance Method Details

#item_idObject

Returns id for this category


61
62
63
# File 'lib/app_store/category.rb', line 61

def item_id
  @item_id ||= @raw['url'].match(/^http:\/\/.*\/.*id[=]{0,1}([0-9]+)[&?].*$/)[1]
end

#itemsObject

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