Class: AppStore::Emigrant::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/app-store-emigrant/library.rb

Overview

Represents a single iTunes mobile applications library

Defined Under Namespace

Classes: DoesNotExist

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Library

Initializes library from given path



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/app-store-emigrant/library.rb', line 15

def initialize path
  @path = Pathname.new path
  @path = @path.expand_path unless @path.absolute?

  # Ensure library is a valid directory
  unless @path.directory?
    raise DoesNotExist, "Given path is not a valid mobile applications library: #{@path}"
  end

  @apps = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/app-store-emigrant/library.rb', line 12

def path
  @path
end

Class Method Details

.defaultObject

Returns the default library (if any) for this system See Apple’s support documents as to where libraries can be found



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/app-store-emigrant/library.rb', line 95

def self.default

  # Use the homedir provided through the environment
  homedir = ENV['HOME']

  # List all locations
  locations = [

    # Mac OSX and Windows Vista
    "#{homedir}/Music/iTunes/iTunes Media/Mobile Applications",

    # Windows 7
    "#{homedir}/My Music/iTunes/iTunes Media/Mobile Applications",

    # Windows XP
    "#{homedir}/My Documents/My Music/iTunes/iTunes Media/Mobile Applications",

    # Mac OSX and Windows Vista (prior to iTunes 9)
    "#{homedir}/Music/iTunes/Mobile Applications",

    # Windows 7 (prior to iTunes 9)
    "#{homedir}/My Music/iTunes/Mobile Applications",

    # Windows XP (prior to iTunes 9)
    "#{homedir}/My Documents/My Music/iTunes/Mobile Applications",

  ]

  # Raise exception if no default library could be found
  path = Dir.glob(locations).first
  unless path
    raise DoesNotExist, 'Could not locate default iTunes mobile applications library'
  end

  # Return an instance of this default library
  Library.new path
end

Instance Method Details

#appsObject

Retrieves a list of applications



28
29
30
31
32
33
34
# File 'lib/app-store-emigrant/library.rb', line 28

def apps
  unless @apps
    load!
  end

  @apps
end

#clouddata!Object

Populates applications’ cloud data in bulk



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/app-store-emigrant/library.rb', line 60

def clouddata!

  # Collect all application ids, skipping any invalid ones
  ids = apps.collect do |app|
    app.id
  end.compact

  # Queries Apple's iTunes Store API for latest cloud data using undocumented bulk method
  response = Net::HTTP.get('itunes.apple.com', '/lookup?id=' + ids.join(','))
  results = JSON.parse(response)['results']
  results.each do |result|
    if app = get(result['trackId'] || -1)
      app.clouddata = result
    end
  end
end

#find(snippet) ⇒ Object

Searches for applications containing given snippet in filename



85
86
87
88
89
# File 'lib/app-store-emigrant/library.rb', line 85

def find snippet
  apps.select do |app|
    app.filename.include? snippet
  end
end

#get(id) ⇒ Object

Searches for application with given id



78
79
80
81
82
# File 'lib/app-store-emigrant/library.rb', line 78

def get id
  apps.select do |app|
    app.id == id
  end.first
end

#load!Object

Forcefully loads applications from disk



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/app-store-emigrant/library.rb', line 47

def load!
  @apps = []
  @path.children.each do |item|
    if item.file?
      begin
        @apps << App.new(item)
      rescue App::Invalid; end
    end
  end
  self
end

#outdated_appsObject

Retrieves a list of outdated applications



42
43
44
# File 'lib/app-store-emigrant/library.rb', line 42

def outdated_apps
  apps.select { |app| app.valid? && app.outdated? }
end

#valid_appsObject

Retrieves a list of valid applications



37
38
39
# File 'lib/app-store-emigrant/library.rb', line 37

def valid_apps
  apps.select { |app| app.valid? }
end