require "app_store/base"
require "app_store/company"
require "app_store/user_review"
require "app_store/artwork"
require "app_store/list"
require "app_store/link"
# Represents an application in the AppStore.
# Available attributes:
# * average_user_rating: average rating from all user_reviews.
# * user_rating_count: user_reviews count.
# * release_date: release date on the AppStore for the application.
# * description: application description.
# * screenshots: array of Image objects for screenshots.
# * item_id: application id.
# * version: application version.
# * company: a Company object, the one which submit the application.
# * title: application title.
# * price: price of the application on the Apple AppStore.
# * icon: Image object of the application icon.
# * size: size of the application in byte.
class AppStore::Application < AppStore::Base
ApplicationURL = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware"
attr_reader :company, :price, :size, :artworks, :icon, :icon_thumbnail, :screenshots
plist :accepted_type => 'software',
:mapping => {
'average-user-rating' => :average_user_rating,
'user-rating-count' => :user_rating_count,
'release-date' => :release_date,
'description' => :description,
'item-id' => :item_id,
'version' => :version,
'title' => :title
}
# Search an Application by its id. Accepts only one id and returns an Application instance.
def self.find_by_id(id)
plist = AppStore::Caller.get(AppStore::Caller::ApplicationURL, :id => id)
# TODO : Check if everything was right before instancianting
new :plist => plist['item-metadata']
end
# Search an Application by a text.
# Returns an array with matching application or an empty array if no result found.
def self.search(text)
plist = AppStore::Caller.get(AppStore::Caller::SearchURL, :media => 'software', :term => text)
AppStore::List.new :list => plist['items']
end
def initialize(attrs = {})
@screenshots ||= []
super
end
# Returns an AppStore::List of UserReview objects.
def user_reviews
if @user_reviews.nil?
plist = AppStore::Caller.get(@raw['view-user-reviews-url'])
@user_reviews = AppStore::List.new(:list => plist['items'])
end
@user_reviews
end
def itunes_url
"#{ApplicationURL}?id=#{item_id}"
end
def icon
if @icon.nil?
parsed = AppStore::Caller.itunes_get(AppStore::Caller::ApplicationURL, :id => item_id)
@icon = AppStore::Image.new(:plist => parsed.search('PictureView[@height="100"][@width="100"]').first)
end
@icon
end
protected
def custom_init_from_plist(plist)
# Set size and price
@price = plist['store-offers']['STDQ']['price']
@size = plist['store-offers']['STDQ']['size']
# Seek for company
@company = AppStore::Company.new(:plist => plist['company'])
# Parse artwork
@artworks = plist['artwork-urls'].collect do |plist_artwork|
# OPTIMIZE : handle default_screenshot
if plist_artwork['image-type'] and plist_artwork['default']
artwork = AppStore::Artwork.new :plist => plist_artwork
@icon_thumbnail ||= artwork.default if artwork.is_icon?
@screenshots << artwork.default unless artwork.is_icon?
artwork
end
end
@artworks.compact!
end
end