Class: MotionConcierge

Inherits:
Object
  • Object
show all
Defined in:
lib/project/motion-concierge.rb

Class Method Summary collapse

Class Method Details

.check_intervalObject



48
49
50
# File 'lib/project/motion-concierge.rb', line 48

def check_interval
  debug? ? debug_fetch_interval : fetch_interval
end

.debug=(debug) ⇒ Object

Debugging



100
101
102
# File 'lib/project/motion-concierge.rb', line 100

def debug=(debug)
  @_debug = !!debug
end

.debug?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/project/motion-concierge.rb', line 104

def debug?
  @_debug || false
end

.debug_fetch_intervalObject



32
33
34
# File 'lib/project/motion-concierge.rb', line 32

def debug_fetch_interval
  @_debug_fetch_interval || 30
end

.debug_fetch_interval=(interval) ⇒ Object



28
29
30
# File 'lib/project/motion-concierge.rb', line 28

def debug_fetch_interval=(interval)
  @_debug_fetch_interval = interval
end

.downloaded_file_exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/project/motion-concierge.rb', line 44

def downloaded_file_exists?
  local_file_path.file_exists?
end

.fetchObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/project/motion-concierge.rb', line 60

def fetch
  if debug?
    puts "Fetching data from: #{@_remote_file_url}"
    puts " Saving it to: #{local_file_name}"
    puts " Every #{check_interval} seconds"
  end

  if should_fetch?
    puts "Data is #{time_offset} seconds old.\nDownloading new data file." if debug?
    AFMotion::HTTP.get(@_remote_file_url) do |result|
      if result.success?
        puts 'Got successful result from server.' if debug?
        result.object.write_to(local_file_path)
        last_fetch = Time.now.to_i
        NSNotificationCenter.defaultCenter.postNotificationName("MotionConciergeNewDataReceived", object:self)
      else
        if debug?
          puts "There was an error downloading the data from the server:"
          puts result.error.localizedDescription
        end
      end
    end
  else
    puts "Data is not stale. Not fetching." if debug?
  end
end

.fetch_intervalObject



24
25
26
# File 'lib/project/motion-concierge.rb', line 24

def fetch_interval
  @_fetch_interval || 86400
end

.fetch_interval=(interval) ⇒ Object



20
21
22
# File 'lib/project/motion-concierge.rb', line 20

def fetch_interval=(interval)
  @_fetch_interval = interval
end

.last_fetchObject



94
95
96
# File 'lib/project/motion-concierge.rb', line 94

def last_fetch
  NSUserDefaults.standardUserDefaults.integerForKey("motion_concierge_last_data_check") || 0
end

.last_fetch=(last) ⇒ Object



87
88
89
90
91
92
# File 'lib/project/motion-concierge.rb', line 87

def last_fetch=(last)
  NSUserDefaults.standardUserDefaults.tap do |defaults|
    defaults.setInteger(last, forKey:"motion_concierge_last_data_check")
    defaults.synchronize
  end
end

.local_file_nameObject



8
9
10
# File 'lib/project/motion-concierge.rb', line 8

def local_file_name
  @_local_file_name || @_remote_file_url.split("/").last
end

.local_file_name=(file_name) ⇒ Object



4
5
6
# File 'lib/project/motion-concierge.rb', line 4

def local_file_name=(file_name)
  @_local_file_name = file_name
end

.local_file_pathObject



36
37
38
# File 'lib/project/motion-concierge.rb', line 36

def local_file_path
  local_file_name.document_path
end

.local_file_stringObject



40
41
42
# File 'lib/project/motion-concierge.rb', line 40

def local_file_string
  NSString.stringWithContentsOfFile(local_file_path)
end

.remote_file_urlObject



16
17
18
# File 'lib/project/motion-concierge.rb', line 16

def remote_file_url
  @_remote_file_url
end

.remote_file_url=(url) ⇒ Object



12
13
14
# File 'lib/project/motion-concierge.rb', line 12

def remote_file_url=(url)
  @_remote_file_url = url
end

.should_fetch?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/project/motion-concierge.rb', line 56

def should_fetch?
  last_fetch < time_offset
end

.time_offsetObject



52
53
54
# File 'lib/project/motion-concierge.rb', line 52

def time_offset
  Time.now.to_i - check_interval
end