Class: AppStore::Emigrant::App
- Inherits:
-
Object
- Object
- AppStore::Emigrant::App
- Defined in:
- lib/app-store-emigrant/app.rb
Overview
Represents a single iTunes mobile application
Defined Under Namespace
Classes: DoesNotExist, Invalid
Constant Summary collapse
- VALID_EXTENSIONS =
List of valid extensions for an application
[ '.ipa' ]
- FILENAME_VERSION_REGEX =
Regular expression to match a version number in filenames
Regexp.new('([0-9.]+)(?:' + VALID_EXTENSIONS.join('|').gsub!('.', '\.') + ')')
Instance Attribute Summary collapse
-
#clouddata ⇒ Object
Lazily queries Apple’s iTunes Store API for latest cloud data Note: Clouddata may be nil if the application was removed from the store.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#cache! ⇒ Object
Forcefully caches this application’s metadata.
-
#cached? ⇒ Boolean
Whether this application’s metadata is cached.
-
#cloudversion ⇒ Object
Version available in the cloud.
-
#filename ⇒ Object
Filename of this application (including extension).
-
#id ⇒ Object
Unique application id.
-
#initialize(path) ⇒ App
constructor
Initializes application from given path.
-
#metadata ⇒ Object
Lazily loads local metadata for this application from its iTunesMetadata.plist.
-
#name ⇒ Object
Name of this application.
-
#outdated? ⇒ Boolean
Whether this application is outdated.
-
#plist ⇒ Object
Property list name.
-
#valid? ⇒ Boolean
Whether this application is valid (validates metadata, id and name).
-
#version ⇒ Object
Version of this application.
Constructor Details
#initialize(path) ⇒ App
Initializes application from given path
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/app-store-emigrant/app.rb', line 27 def initialize path @path = Pathname.new path @path = @path. unless @path.absolute? # Ensure application exists unless @path.file? raise DoesNotExist, "Given path is not a valid application: #{@path}" end # Ensure application has valid extension unless VALID_EXTENSIONS.include? @path.extname raise Invalid, "Given application does not have a valid extension: #{@path}" end @metadata = nil @clouddata = nil end |
Instance Attribute Details
#clouddata ⇒ Object
Lazily queries Apple’s iTunes Store API for latest cloud data Note: Clouddata may be nil if the application was removed from the store
123 124 125 126 127 128 129 130 |
# File 'lib/app-store-emigrant/app.rb', line 123 def clouddata unless @clouddata response = Net::HTTP.get('itunes.apple.com', '/lookup?id=' + id.to_s) @clouddata = JSON.parse(response)['results'].first end @clouddata end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/app-store-emigrant/app.rb', line 23 def path @path end |
Instance Method Details
#cache! ⇒ Object
Forcefully caches this application’s metadata
97 98 99 |
# File 'lib/app-store-emigrant/app.rb', line 97 def cache! end |
#cached? ⇒ Boolean
Whether this application’s metadata is cached
92 93 94 |
# File 'lib/app-store-emigrant/app.rb', line 92 def cached? Cache.has? plist end |
#cloudversion ⇒ Object
Version available in the cloud
133 134 135 |
# File 'lib/app-store-emigrant/app.rb', line 133 def cloudversion clouddata && clouddata['version'] end |
#filename ⇒ Object
Filename of this application (including extension)
46 47 48 49 50 51 52 |
# File 'lib/app-store-emigrant/app.rb', line 46 def filename unless @filename @filename = @path.basename.to_s end @filename end |
#id ⇒ Object
Unique application id
55 56 57 |
# File 'lib/app-store-emigrant/app.rb', line 55 def id ['itemId'] rescue nil end |
#metadata ⇒ Object
Lazily loads local metadata for this application from its iTunesMetadata.plist
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/app-store-emigrant/app.rb', line 102 def unless @metadata unless cached? begin Zip::ZipFile.open(@path) do |zip| zip.extract('iTunesMetadata.plist', Cache.path_to(plist)) end rescue Zip::ZipError => e raise Invalid, e. end end @metadata = CFPropertyList.native_types(CFPropertyList::List.new(:file => Cache.path_to(plist)).value) end @metadata end |
#name ⇒ Object
Name of this application
60 61 62 |
# File 'lib/app-store-emigrant/app.rb', line 60 def name ['itemName'] rescue nil end |
#outdated? ⇒ Boolean
Whether this application is outdated
138 139 140 |
# File 'lib/app-store-emigrant/app.rb', line 138 def outdated? return cloudversion && version != cloudversion end |
#plist ⇒ Object
Property list name
87 88 89 |
# File 'lib/app-store-emigrant/app.rb', line 87 def plist Pathname.new(filename).basename('.ipa').to_s + '.plist' end |
#valid? ⇒ Boolean
Whether this application is valid (validates metadata, id and name)
65 66 67 |
# File 'lib/app-store-emigrant/app.rb', line 65 def valid? && id && name rescue false end |
#version ⇒ Object
Version of this application
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/app-store-emigrant/app.rb', line 70 def version unless @version # Extract version information (if available) @version = ['bundleShortVersionString'] || ['bundleVersion'] rescue nil # Otherwise, use the filename unless @version @version = filename[FILENAME_VERSION_REGEX, 1] end end @version end |