Class: MotionConcierge
- Inherits:
-
Object
- Object
- MotionConcierge
- Defined in:
- lib/project/motion-concierge.rb
Class Method Summary collapse
- .check_interval ⇒ Object
-
.debug=(debug) ⇒ Object
Debugging.
- .debug? ⇒ Boolean
- .debug_fetch_interval ⇒ Object
- .debug_fetch_interval=(interval) ⇒ Object
- .downloaded_file_exists? ⇒ Boolean
- .fetch ⇒ Object
- .fetch_interval ⇒ Object
- .fetch_interval=(interval) ⇒ Object
- .last_fetch ⇒ Object
- .last_fetch=(last) ⇒ Object
- .local_file_name ⇒ Object
- .local_file_name=(file_name) ⇒ Object
- .local_file_path ⇒ Object
- .local_file_string ⇒ Object
- .remote_file_url ⇒ Object
- .remote_file_url=(url) ⇒ Object
- .should_fetch? ⇒ Boolean
- .time_offset ⇒ Object
Class Method Details
.check_interval ⇒ Object
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
104 105 106 |
# File 'lib/project/motion-concierge.rb', line 104 def debug? @_debug || false end |
.debug_fetch_interval ⇒ Object
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
44 45 46 |
# File 'lib/project/motion-concierge.rb', line 44 def downloaded_file_exists? local_file_path.file_exists? end |
.fetch ⇒ Object
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_interval ⇒ Object
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_fetch ⇒ Object
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_name ⇒ Object
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_path ⇒ Object
36 37 38 |
# File 'lib/project/motion-concierge.rb', line 36 def local_file_path local_file_name.document_path end |
.local_file_string ⇒ Object
40 41 42 |
# File 'lib/project/motion-concierge.rb', line 40 def local_file_string NSString.stringWithContentsOfFile(local_file_path) end |
.remote_file_url ⇒ Object
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
56 57 58 |
# File 'lib/project/motion-concierge.rb', line 56 def should_fetch? last_fetch < time_offset end |
.time_offset ⇒ Object
52 53 54 |
# File 'lib/project/motion-concierge.rb', line 52 def time_offset Time.now.to_i - check_interval end |