app_store 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +91 -0
- data/Rakefile +64 -0
- data/VERSION +1 -0
- data/lib/app_store/application.rb +100 -0
- data/lib/app_store/artwork.rb +19 -0
- data/lib/app_store/base.rb +22 -0
- data/lib/app_store/caller.rb +45 -0
- data/lib/app_store/category.rb +38 -0
- data/lib/app_store/company.rb +9 -0
- data/lib/app_store/helper.rb +2 -0
- data/lib/app_store/helpers/plist.rb +22 -0
- data/lib/app_store/image.rb +10 -0
- data/lib/app_store/link.rb +20 -0
- data/lib/app_store/list.rb +94 -0
- data/lib/app_store/user_review.rb +17 -0
- data/lib/app_store.rb +12 -0
- data/test/application_test.rb +180 -0
- data/test/artwork_test.rb +66 -0
- data/test/base_test.rb +25 -0
- data/test/caller_test.rb +15 -0
- data/test/image_test.rb +60 -0
- data/test/output.txt +17922 -0
- data/test/plist_test.rb +24 -0
- data/test/raw_answers/application_284417350.txt +205 -0
- data/test/raw_answers/application_289616509.txt +208 -0
- data/test/raw_answers/application_300719251.txt +133 -0
- data/test/raw_answers/application_316771002.txt +233 -0
- data/test/raw_answers/application_not_found.txt +19 -0
- data/test/raw_answers/search_remote.txt +255 -0
- data/test/test_helper.rb +15 -0
- metadata +143 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Fabien Jakimowicz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
= AppStore
|
2
|
+
|
3
|
+
== DESCRIPTION
|
4
|
+
AppStore is an unofficial implementation of the Apple AppStore API.
|
5
|
+
It provides way to search for applications, navigate through categories.
|
6
|
+
|
7
|
+
== SYNOPSIS
|
8
|
+
=== Examples
|
9
|
+
* Simple search
|
10
|
+
@applications = AppStore::Application.search('upnp')
|
11
|
+
@applications.class # => Array
|
12
|
+
@applications.length # => 8
|
13
|
+
@applications.first.title # => "PlugPlayer"
|
14
|
+
@applications.first.price # => 4.99
|
15
|
+
|
16
|
+
* Find an application by its id
|
17
|
+
@fontpicker = AppStore::Application.find_by_id(327076783)
|
18
|
+
@fontpicker.class # => AppStore::Application
|
19
|
+
@fontpicker.title # => "FontPicker"
|
20
|
+
@fontpicker.price # => 0.0
|
21
|
+
@fontpicker.company.title # => "Etienne Segonzac"
|
22
|
+
|
23
|
+
* User reviews
|
24
|
+
@remote = AppStore::Application.find_by_id(284417350)
|
25
|
+
@remote.title # => "Remote"
|
26
|
+
@remote.user_reviews.length # => 10
|
27
|
+
|
28
|
+
@review = @remote.user_reviews.first
|
29
|
+
|
30
|
+
@review.user_name # => "Ebolavoodoo on Aug 27, 2009"
|
31
|
+
@review.average_user_rating # => 1.0
|
32
|
+
@review.text # => "Simply amazing. My new favorite app. Instantly responds. Easy to navigate and control. For those who say it doesn't work. Stinks to be you."
|
33
|
+
|
34
|
+
* Categories
|
35
|
+
# Fetch featured categories
|
36
|
+
@categories = AppStore::Category.featured
|
37
|
+
@categories.class # => Array
|
38
|
+
@categories.length # => 20
|
39
|
+
|
40
|
+
# Use category
|
41
|
+
@category = @categories.first
|
42
|
+
@category.title # => "Games"
|
43
|
+
@category.item_count # => 1573
|
44
|
+
|
45
|
+
# Go through subcategories and applications
|
46
|
+
@subcategory = @category.items.last
|
47
|
+
@subcategory.class # => AppStore::Category
|
48
|
+
@subcategory.title # => "Word"
|
49
|
+
@subcategory.items.length # => 26
|
50
|
+
@subcategory.items.first.class # => AppStore::Application
|
51
|
+
@subcategory.items.last.class # => AppStore::Category
|
52
|
+
@subcategory.items.last.title # => "Twenty Five More..."
|
53
|
+
|
54
|
+
== REQUIREMENTS
|
55
|
+
|
56
|
+
* mechanize gem
|
57
|
+
* plist gem
|
58
|
+
|
59
|
+
== INSTALL
|
60
|
+
gem install app_store
|
61
|
+
|
62
|
+
== TODO
|
63
|
+
* handle medias (screenshots, icons, ...)
|
64
|
+
* optimize navigation through lists
|
65
|
+
* handle 'links' to avoid calling AppStore API for each items in a list
|
66
|
+
* use FakeMechanize to unit test each calls
|
67
|
+
* handle multiple store/language
|
68
|
+
|
69
|
+
== LICENSE
|
70
|
+
|
71
|
+
(The MIT License)
|
72
|
+
|
73
|
+
Copyright (c) 2009, Fabien Jakimowicz <fabien@jakimowicz.com>
|
74
|
+
|
75
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
76
|
+
this software and associated documentation files (the "Software"), to deal in
|
77
|
+
the Software without restriction, including without limitation the rights to
|
78
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
79
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
80
|
+
so, subject to the following conditions:
|
81
|
+
|
82
|
+
The above copyright notice and this permission notice shall be included in all
|
83
|
+
copies or substantial portions of the Software.
|
84
|
+
|
85
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
86
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
87
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
88
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
89
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
90
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
91
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "app_store"
|
8
|
+
gem.summary = "AppStore is an unofficial implementation of the Apple AppStore API"
|
9
|
+
gem.description = "AppStore allows you to fetch informations about applications (title, description, size, reviews, ...), make searches, go through categories ..."
|
10
|
+
gem.email = "fabien@jakimowicz.com"
|
11
|
+
gem.homepage = "http://github.com/jakimowicz/app_store"
|
12
|
+
gem.authors = ["Fabien Jakimowicz"]
|
13
|
+
gem.rubyforge_project = "app_store"
|
14
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
15
|
+
gem.add_development_dependency "fake_mechanize"
|
16
|
+
gem.add_dependency "nokogiri"
|
17
|
+
gem.add_dependency "mechanize"
|
18
|
+
gem.add_dependency "plist"
|
19
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
|
+
end
|
21
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
22
|
+
rubyforge.doc_task = "rdoc"
|
23
|
+
end
|
24
|
+
rescue LoadError
|
25
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/*_test.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
task :rcov do
|
44
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
task :test => :check_dependencies
|
49
|
+
|
50
|
+
task :default => :test
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
if File.exist?('VERSION')
|
55
|
+
version = File.read('VERSION')
|
56
|
+
else
|
57
|
+
version = ""
|
58
|
+
end
|
59
|
+
|
60
|
+
rdoc.rdoc_dir = 'rdoc'
|
61
|
+
rdoc.title = "AppStore #{version}"
|
62
|
+
rdoc.rdoc_files.include('README*')
|
63
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require "app_store/base"
|
2
|
+
require "app_store/company"
|
3
|
+
require "app_store/user_review"
|
4
|
+
require "app_store/artwork"
|
5
|
+
require "app_store/list"
|
6
|
+
require "app_store/link"
|
7
|
+
|
8
|
+
# Represents an application in the AppStore.
|
9
|
+
# Available attributes:
|
10
|
+
# * <tt>average_user_rating</tt>: average rating from all user_reviews.
|
11
|
+
# * <tt>user_rating_count</tt>: user_reviews count.
|
12
|
+
# * <tt>release_date</tt>: release date on the AppStore for the application.
|
13
|
+
# * <tt>description</tt>: application description.
|
14
|
+
# * <tt>screenshots</tt>: array of Image objects for screenshots.
|
15
|
+
# * <tt>item_id</tt>: application id.
|
16
|
+
# * <tt>version</tt>: application version.
|
17
|
+
# * <tt>company</tt>: a Company object, the one which submit the application.
|
18
|
+
# * <tt>title</tt>: application title.
|
19
|
+
# * <tt>price</tt>: price of the application on the Apple AppStore.
|
20
|
+
# * <tt>icon</tt>: Image object of the application icon.
|
21
|
+
# * <tt>size</tt>: size of the application in byte.
|
22
|
+
class AppStore::Application < AppStore::Base
|
23
|
+
ApplicationURL = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware"
|
24
|
+
|
25
|
+
attr_reader :company, :price, :size, :artworks, :icon, :icon_thumbnail, :screenshots
|
26
|
+
|
27
|
+
plist :accepted_type => 'software',
|
28
|
+
:mapping => {
|
29
|
+
'average-user-rating' => :average_user_rating,
|
30
|
+
'user-rating-count' => :user_rating_count,
|
31
|
+
'release-date' => :release_date,
|
32
|
+
'description' => :description,
|
33
|
+
'item-id' => :item_id,
|
34
|
+
'version' => :version,
|
35
|
+
'title' => :title
|
36
|
+
}
|
37
|
+
|
38
|
+
# Search an Application by its <tt>id</tt>. Accepts only one <tt>id</tt> and returns an Application instance.
|
39
|
+
def self.find_by_id(id)
|
40
|
+
plist = AppStore::Caller.get(AppStore::Caller::ApplicationURL, :id => id)
|
41
|
+
# TODO : Check if everything was right before instancianting
|
42
|
+
new :plist => plist['item-metadata']
|
43
|
+
end
|
44
|
+
|
45
|
+
# Search an Application by a <tt>text</tt>.
|
46
|
+
# Returns an array with matching application or an empty array if no result found.
|
47
|
+
def self.search(text)
|
48
|
+
plist = AppStore::Caller.get(AppStore::Caller::SearchURL, :media => 'software', :term => text)
|
49
|
+
AppStore::List.new :list => plist['items']
|
50
|
+
end
|
51
|
+
|
52
|
+
def initialize(attrs = {})
|
53
|
+
@screenshots ||= []
|
54
|
+
super
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns an AppStore::List of UserReview objects.
|
58
|
+
def user_reviews
|
59
|
+
if @user_reviews.nil?
|
60
|
+
plist = AppStore::Caller.get(@raw['view-user-reviews-url'])
|
61
|
+
@user_reviews = AppStore::List.new(:list => plist['items'])
|
62
|
+
end
|
63
|
+
@user_reviews
|
64
|
+
end
|
65
|
+
|
66
|
+
def itunes_url
|
67
|
+
"#{ApplicationURL}?id=#{item_id}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def icon
|
71
|
+
if @icon.nil?
|
72
|
+
parsed = AppStore::Caller.itunes_get(AppStore::Caller::ApplicationURL, :id => item_id)
|
73
|
+
@icon = AppStore::Image.new(:plist => parsed.search('PictureView[@height="100"][@width="100"]').first)
|
74
|
+
end
|
75
|
+
|
76
|
+
@icon
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
def custom_init_from_plist(plist)
|
81
|
+
# Set size and price
|
82
|
+
@price = plist['store-offers']['STDQ']['price']
|
83
|
+
@size = plist['store-offers']['STDQ']['size']
|
84
|
+
|
85
|
+
# Seek for company
|
86
|
+
@company = AppStore::Company.new(:plist => plist['company'])
|
87
|
+
|
88
|
+
# Parse artwork
|
89
|
+
@artworks = plist['artwork-urls'].collect do |plist_artwork|
|
90
|
+
# OPTIMIZE : handle default_screenshot
|
91
|
+
if plist_artwork['image-type'] and plist_artwork['default']
|
92
|
+
artwork = AppStore::Artwork.new :plist => plist_artwork
|
93
|
+
@icon_thumbnail ||= artwork.default if artwork.is_icon?
|
94
|
+
@screenshots << artwork.default unless artwork.is_icon?
|
95
|
+
artwork
|
96
|
+
end
|
97
|
+
end
|
98
|
+
@artworks.compact!
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "app_store/base"
|
2
|
+
require "app_store/image"
|
3
|
+
|
4
|
+
class AppStore::Artwork < AppStore::Base
|
5
|
+
attr_reader :default, :thumbnail
|
6
|
+
|
7
|
+
plist :mapping => { 'image-type' => :image_type }
|
8
|
+
|
9
|
+
def is_icon?
|
10
|
+
image_type == 'software-icon'
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
def custom_init_from_plist(plist)
|
15
|
+
@default = AppStore::Image.new(:plist => plist['default']) if plist['default']
|
16
|
+
@thumbnail = AppStore::Image.new(:plist => plist['thumbnail']) if plist['thumbnail']
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "app_store/caller"
|
2
|
+
require "app_store/helpers/plist"
|
3
|
+
|
4
|
+
# Implements basic operations for AppStore objects : initialization, plist parsing, ...
|
5
|
+
class AppStore::Base
|
6
|
+
attr_reader :raw
|
7
|
+
|
8
|
+
extend AppStore::Helper::Plist
|
9
|
+
|
10
|
+
# Instanciate a new object.
|
11
|
+
# <tt>attrs</tt> accepts:
|
12
|
+
# * <tt>:plist</tt>: a plist object to be parsed
|
13
|
+
def initialize(attrs = {})
|
14
|
+
init_from_plist attrs[:plist] if attrs[:plist]
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
def init_from_plist(plist)
|
19
|
+
custom_init_from_plist plist if respond_to?(:custom_init_from_plist)
|
20
|
+
@raw = plist
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "nokogiri"
|
3
|
+
require "mechanize"
|
4
|
+
require "plist"
|
5
|
+
|
6
|
+
require "app_store"
|
7
|
+
|
8
|
+
# Caller regroups all the calling and xml parsing mechanism to call the AppStore.
|
9
|
+
module AppStore::Caller
|
10
|
+
extend self
|
11
|
+
|
12
|
+
FeaturedCategoriesURL = "http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewFeaturedSoftwareCategories"
|
13
|
+
ApplicationURL = "http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware"
|
14
|
+
CategoryURL = "http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewGenre"
|
15
|
+
SearchURL = "http://ax.search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search"
|
16
|
+
|
17
|
+
# Call the Apple AppStore using given <tt>url</tt> and passing <tt>params</tt> with an HTTP Get method.
|
18
|
+
def iphone_get(url, params = {})
|
19
|
+
# About the X-Apple-Store-Front header: this is used to select which store and which language.
|
20
|
+
# Format is XXXXXX-Y,Z where XXXXXX is the store number (us, french, ...), Y the language and Z the return format.
|
21
|
+
# If you omit the language, the default one for the store is used.
|
22
|
+
# Return format can be either "1" or "2" : "1" returns data to be directly displayed and "2" is a more structured format.
|
23
|
+
answer = iphone_agent.get(:url => url, :headers => {"X-Apple-Store-Front" => '143441,2'}, :params => params)
|
24
|
+
raise AppStore::RequestError if answer.code.to_s != '200'
|
25
|
+
Plist::parse_xml answer.body
|
26
|
+
end
|
27
|
+
|
28
|
+
alias :get :iphone_get
|
29
|
+
|
30
|
+
def itunes_get(url, params = {})
|
31
|
+
answer = itunes_agent.get(url, params)
|
32
|
+
raise AppStore::RequestError if answer.code.to_s != '200'
|
33
|
+
Nokogiri.parse answer.body
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
def iphone_agent
|
38
|
+
@iphone_agent ||= WWW::Mechanize.new { |a| a.user_agent = 'iTunes-iPhone/3.0 (2)' }
|
39
|
+
end
|
40
|
+
|
41
|
+
def itunes_agent
|
42
|
+
@itunes_agent ||= WWW::Mechanize.new
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "app_store/base"
|
2
|
+
require "app_store/application"
|
3
|
+
|
4
|
+
# A category like categories on the AppStore.
|
5
|
+
# Available attributes:
|
6
|
+
# * <tt>item-count</tt>: total items count for this category.
|
7
|
+
# * <tt>title</tt>: title for the category.
|
8
|
+
class AppStore::Category < AppStore::Base
|
9
|
+
plist :mapping => {
|
10
|
+
'item-count' => :item_count,
|
11
|
+
'title' => :title
|
12
|
+
}
|
13
|
+
|
14
|
+
# Returns an array of featured categories (main categories).
|
15
|
+
# It is the same list as the one displayed in the iPhone AppStore.
|
16
|
+
def self.featured
|
17
|
+
plist = AppStore::Caller.get(AppStore::Caller::FeaturedCategoriesURL)
|
18
|
+
plist['items'].collect { |item| new :plist => item }
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns an array of items contained in the category, with a maximum of 25 items (Apple limitation).
|
22
|
+
# If there are more than 25 items in the category, an extra item is added at the end of the list
|
23
|
+
# as a link to the next 25 entries.
|
24
|
+
# Each element can be either a Category or an Application.
|
25
|
+
def items
|
26
|
+
if @items.nil?
|
27
|
+
plist = AppStore::Caller.get(@raw['url'])
|
28
|
+
@items = AppStore::List.new(
|
29
|
+
:list => plist['items'],
|
30
|
+
:element_type => 'link',
|
31
|
+
:element_initializer => lambda {|element|
|
32
|
+
(element['link-type'] == 'software' ? AppStore::Link : AppStore::Category).new(:plist => element)
|
33
|
+
}
|
34
|
+
)
|
35
|
+
end
|
36
|
+
@items
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "app_store"
|
2
|
+
|
3
|
+
# Represents a company (the application developper)
|
4
|
+
# The following attributes are available:
|
5
|
+
# * <tt>title</tt>: title of the company, or name of the developper.
|
6
|
+
# * <tt>url</tt>: website url.
|
7
|
+
class AppStore::Company < AppStore::Base
|
8
|
+
plist :mapping => { 'title' => :title, 'url' => :url }
|
9
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "app_store/helper"
|
2
|
+
|
3
|
+
module AppStore::Helper::Plist
|
4
|
+
def plist(params = {})
|
5
|
+
mapping = params[:mapping].collect do |plist_key, attr_name|
|
6
|
+
# First, make attribute readable outside the instance
|
7
|
+
class_eval "attr_reader :#{attr_name}"
|
8
|
+
|
9
|
+
# Then, build the content of init_from_plist function
|
10
|
+
"@#{attr_name} = plist['#{plist_key}']"
|
11
|
+
end rescue []
|
12
|
+
|
13
|
+
class_eval <<-EOV, __FILE__, __LINE__
|
14
|
+
protected
|
15
|
+
def init_from_plist(plist)
|
16
|
+
#{"raise AppStore::ParseError unless plist['type'] == '#{params[:accepted_type]}'" if params[:accepted_type]}
|
17
|
+
super
|
18
|
+
#{mapping.join("\n")}
|
19
|
+
end
|
20
|
+
EOV
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "app_store/base"
|
2
|
+
require "app_store/caller"
|
3
|
+
|
4
|
+
class AppStore::Link < AppStore::Base
|
5
|
+
plist :mapping => {
|
6
|
+
'link-type' => :item_type,
|
7
|
+
'item-id' => :item_id,
|
8
|
+
'title' => :title,
|
9
|
+
'url' => :url
|
10
|
+
}
|
11
|
+
|
12
|
+
def destination
|
13
|
+
@destination ||= case @item_type
|
14
|
+
when 'software'
|
15
|
+
AppStore::Application.new :plist => AppStore::Caller.get(@url)['item-metadata']
|
16
|
+
else
|
17
|
+
raise 'unsupported'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'app_store'
|
2
|
+
|
3
|
+
# Represents a list based on data from Apple AppStore.
|
4
|
+
# If a list contains too much elements (> 25), the Apple AppStore
|
5
|
+
# sends only 24 elements followed by a link for the next 24 elements.
|
6
|
+
# This class represents an abstraction of Apple AppStore lists, have
|
7
|
+
# a real count attribute and is enumerable over the entire list.
|
8
|
+
class AppStore::List
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
# All the elements already gathered.
|
12
|
+
attr_reader :elements
|
13
|
+
|
14
|
+
# A required attribute <tt>:element_initializer</tt> can be used
|
15
|
+
# to specify a block used for the initialization of each element
|
16
|
+
# of the list
|
17
|
+
def initialize(attrs = {})
|
18
|
+
@element_initializer = attrs[:element_initializer]
|
19
|
+
@element_type = attrs[:element_type]
|
20
|
+
@elements ||= []
|
21
|
+
|
22
|
+
process_new_elements attrs[:list]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the real elements count, not a count based on the elements
|
26
|
+
# already fetched.
|
27
|
+
def count
|
28
|
+
@count ||= @elements.count
|
29
|
+
end
|
30
|
+
|
31
|
+
def each(&block)
|
32
|
+
collect(&block)
|
33
|
+
@elements
|
34
|
+
end
|
35
|
+
|
36
|
+
def collect
|
37
|
+
# First, iterate through already fetched elements
|
38
|
+
result = @elements.collect {|element| yield element}
|
39
|
+
|
40
|
+
# Then, iterate until we have no more links to follow
|
41
|
+
while (last_elements = fetch_next_elements) do
|
42
|
+
result += last_elements.collect {|element| yield element}
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns full array
|
46
|
+
result
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
# Fetch next elements using link, append them to @elements
|
51
|
+
# Return last fetched elements if any, nil otherwise
|
52
|
+
def fetch_next_elements
|
53
|
+
return nil if @link_to_next_elements.nil?
|
54
|
+
process_new_elements(AppStore::Caller.get(@link_to_next_elements)['items'])
|
55
|
+
end
|
56
|
+
|
57
|
+
def process_new_elements(new_elements)
|
58
|
+
result = []
|
59
|
+
@link_to_next_elements = nil
|
60
|
+
|
61
|
+
new_elements.each do |element|
|
62
|
+
case element['type']
|
63
|
+
when @element_type
|
64
|
+
result << initialize_element_and_append(element)
|
65
|
+
when 'software'
|
66
|
+
result << append_element(AppStore::Application.new(:plist => element))
|
67
|
+
when 'review'
|
68
|
+
result << append_element(AppStore::UserReview.new(:plist => element))
|
69
|
+
when 'link'
|
70
|
+
result << append_element(AppStore::Link.new(:plist => element))
|
71
|
+
when 'more'
|
72
|
+
@count = element['total-items']
|
73
|
+
@link_to_next_elements = element['url']
|
74
|
+
when 'review-header'
|
75
|
+
;
|
76
|
+
else
|
77
|
+
raise "unsupported type" unless @element_initializer
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
result
|
82
|
+
end
|
83
|
+
|
84
|
+
# Initialize given <tt>element</tt> using @element_initializer block if given,
|
85
|
+
# append block execution result to @elements and return it.
|
86
|
+
def initialize_element_and_append(element)
|
87
|
+
append_element @element_initializer[element]
|
88
|
+
end
|
89
|
+
|
90
|
+
def append_element(element)
|
91
|
+
@elements << element
|
92
|
+
element
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "app_store/base"
|
2
|
+
|
3
|
+
# Each Application can have multiple user reviews.
|
4
|
+
# Available attributes:
|
5
|
+
# * <tt>average_user_rating</tt>: rating given by the user for the application.
|
6
|
+
# * <tt>user_name</tt>: name of the user who made the rating.
|
7
|
+
# * <tt>title</tt>: title of the rating (probably some internal apple stuff).
|
8
|
+
# * <tt>text</tt>: comment leaved by the user.
|
9
|
+
class AppStore::UserReview < AppStore::Base
|
10
|
+
plist :accepted_type => 'review',
|
11
|
+
:mapping => {
|
12
|
+
'average-user-rating' => :average_user_rating,
|
13
|
+
'user-name' => :user_name, # TODO : parse user_name to separate username and comment date
|
14
|
+
'title' => :title,
|
15
|
+
'text' => :text
|
16
|
+
}
|
17
|
+
end
|
data/lib/app_store.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module AppStore
|
2
|
+
# Error while comunicating with the AppStore
|
3
|
+
class RequestError < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
# A parse error is raised if data returned by the AppStore was not expected
|
7
|
+
class ParseError < StandardError
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require "app_store/application"
|
12
|
+
require "app_store/category"
|