Class: AppStore::Emigrant::App

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.expand_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

#clouddataObject

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

#pathObject (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

Returns:

  • (Boolean)


92
93
94
# File 'lib/app-store-emigrant/app.rb', line 92

def cached?
  Cache.has? plist
end

#cloudversionObject

Version available in the cloud



133
134
135
# File 'lib/app-store-emigrant/app.rb', line 133

def cloudversion
  clouddata && clouddata['version']
end

#filenameObject

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

#idObject

Unique application id



55
56
57
# File 'lib/app-store-emigrant/app.rb', line 55

def id
  ['itemId'] rescue nil
end

#metadataObject

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.message
      end
    end

    @metadata = CFPropertyList.native_types(CFPropertyList::List.new(:file => Cache.path_to(plist)).value)
  end

  @metadata
end

#nameObject

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

Returns:

  • (Boolean)


138
139
140
# File 'lib/app-store-emigrant/app.rb', line 138

def outdated?
  return cloudversion && version != cloudversion
end

#plistObject

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)

Returns:

  • (Boolean)


65
66
67
# File 'lib/app-store-emigrant/app.rb', line 65

def valid?
   && id && name rescue false
end

#versionObject

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