motion-concierge 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b738d849121e554e915fc5e5d6adb4e3ee4b0fb9
4
+ data.tar.gz: 0a40da8bfc156eec878d0cf5457f04f68ccabf98
5
+ SHA512:
6
+ metadata.gz: 5a67521c2cdb83d0e5c3ab5376dde515ffb2ade39114a97886b7d25bfb7e469eaeabaf0947d629afaf487e37d866537e4472069ead22d984648d6ca37559753a
7
+ data.tar.gz: 572585c2295fac564b6c60f7290ae5913e04a7b9ee9a9b565c8dbf12d0dee23c15e9c4122f20ece825bf26de3e8facf8ef28a31ce87b6b97652f97773a8b292c
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # motion-concierge
2
+
3
+ [![Build Status](https://travis-ci.org/OTGApps/motion-concierge.svg)](https://travis-ci.org/OTGApps/motion-concierge)
4
+
5
+ motion-concierge is your personal data concierge! Just provide a file name, and network url, and set up some basic rules regarding when to download the data and the concierge will automatically fetch your data for you from the web!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'motion-concierge'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install motion-concierge
20
+
21
+ After installing, you'll need to install the needed pods:
22
+
23
+ $ [bundle exec] rake pod:install
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ # You configure motion-concierge when the app first launches:
29
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
30
+ # Something here
31
+
32
+ # Set up motion-concierge
33
+ MotionConcierge.local_file_name = 'my_data_file.json'
34
+ MotionConcierge.remote_file_url = 'http://whatever.com/my_data_file.json'
35
+ MotionConcierge.fetch_interval = 86400 # Once a day
36
+ # You can put it in debug mode too!
37
+ MotionConcierge.debug = true
38
+ MotionConcierge.debug_fetch_interval = 30 # Every 30 seconds
39
+
40
+
41
+ # Something else here
42
+ end
43
+
44
+ # Then make sure to check for the data each time the app is launched
45
+ def applicationDidBecomeActive(application)
46
+ # Check for new data is necessary
47
+ MotionConcierge.fetch
48
+ end
49
+
50
+ ```
51
+
52
+ That's it! From now on, every day (86400 seconds) when the app is launched, or comes back into the foreground, motion-concierge will check to see if it should download new data and then fire off a notification once it's recieved the data and saved it to disk so that you can listen for the event and refresh your interface as necessary.
53
+
54
+ Here's how you'd listen for the event:
55
+
56
+ ```ruby
57
+ def listen_for_new_data
58
+ NSNotificationCenter.defaultCenter.addObserver(self, selector:"reload_data", name:"MotionConciergeNewDataReceived", object:nil)
59
+ end
60
+
61
+ def reload_data(notification)
62
+ puts "Got new data!"
63
+ end
64
+ ```
65
+
66
+ You can use the local file easily!
67
+ ```ruby
68
+ puts 'The file is located here: ' + MotionConcierge.local_file_path
69
+ puts 'The file contents are: ' + MotionConcierge.local_file_string
70
+ ```
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "This file must be required within a RubyMotion project Rakefile."
5
+ end
6
+
7
+ require 'motion-cocoapods'
8
+ require 'afmotion'
9
+
10
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
11
+ Motion::Project::App.setup do |app|
12
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
13
+ end
@@ -0,0 +1,108 @@
1
+ class MotionConcierge
2
+ class << self
3
+
4
+ def local_file_name=(file_name)
5
+ @_local_file_name = file_name
6
+ end
7
+
8
+ def local_file_name
9
+ @_local_file_name || @_remote_file_url.split("/").last
10
+ end
11
+
12
+ def remote_file_url=(url)
13
+ @_remote_file_url = url
14
+ end
15
+
16
+ def remote_file_url
17
+ @_remote_file_url
18
+ end
19
+
20
+ def fetch_interval=(interval)
21
+ @_fetch_interval = interval
22
+ end
23
+
24
+ def fetch_interval
25
+ @_fetch_interval || 86400
26
+ end
27
+
28
+ def debug_fetch_interval=(interval)
29
+ @_debug_fetch_interval = interval
30
+ end
31
+
32
+ def debug_fetch_interval
33
+ @_debug_fetch_interval || 30
34
+ end
35
+
36
+ def local_file_path
37
+ local_file_name.document_path
38
+ end
39
+
40
+ def local_file_string
41
+ NSString.stringWithContentsOfFile(local_file_path)
42
+ end
43
+
44
+ def downloaded_file_exists?
45
+ local_file_path.file_exists?
46
+ end
47
+
48
+ def check_interval
49
+ debug? ? debug_fetch_interval : fetch_interval
50
+ end
51
+
52
+ def time_offset
53
+ Time.now.to_i - check_interval
54
+ end
55
+
56
+ def should_fetch?
57
+ last_fetch < time_offset
58
+ end
59
+
60
+ def fetch
61
+ if debug?
62
+ puts "Fetching data from: #{@_remote_file_url}"
63
+ puts " Saving it to: #{local_file_name}"
64
+ puts " Every #{check_interval} seconds"
65
+ end
66
+
67
+ if should_fetch?
68
+ puts "Data is #{time_offset} seconds old.\nDownloading new data file." if debug?
69
+ AFMotion::HTTP.get(@_remote_file_url) do |result|
70
+ if result.success?
71
+ puts 'Got successful result from server.' if debug?
72
+ result.object.write_to(local_file_path)
73
+ last_fetch = Time.now.to_i
74
+ NSNotificationCenter.defaultCenter.postNotificationName("MotionConciergeNewDataReceived", object:self)
75
+ else
76
+ if debug?
77
+ puts "There was an error downloading the data from the server:"
78
+ puts result.error.localizedDescription
79
+ end
80
+ end
81
+ end
82
+ else
83
+ puts "Data is not stale. Not fetching." if debug?
84
+ end
85
+ end
86
+
87
+ def last_fetch=(last)
88
+ NSUserDefaults.standardUserDefaults.tap do |defaults|
89
+ defaults.setInteger(last, forKey:"motion_concierge_last_data_check")
90
+ defaults.synchronize
91
+ end
92
+ end
93
+
94
+ def last_fetch
95
+ NSUserDefaults.standardUserDefaults.integerForKey("motion_concierge_last_data_check") || 0
96
+ end
97
+
98
+ # Debugging
99
+
100
+ def debug=(debug)
101
+ @_debug = !!debug
102
+ end
103
+
104
+ def debug?
105
+ @_debug || false
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,31 @@
1
+ # Shamelessly extracted from sugarcube
2
+ # https://github.com/rubymotion/sugarcube/blob/master/lib/cocoa/sugarcube-files/nsdata.rb
3
+ class NSData
4
+
5
+ class << self
6
+
7
+ def read_from(path_or_url)
8
+ case path_or_url
9
+ when NSURL
10
+ self.dataWithContentsOfURL(path_or_url)
11
+ when NSString
12
+ self.dataWithContentsOfFile(path_or_url)
13
+ else
14
+ false
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ def write_to(path_or_url, atomically=true)
21
+ case path_or_url
22
+ when NSURL
23
+ self.writeToURL(path_or_url, atomically: atomically)
24
+ when NSString
25
+ self.writeToFile(path_or_url, atomically: atomically)
26
+ else
27
+ false
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,35 @@
1
+ # Shamelessly extracted from sugarcube
2
+ # https://github.com/rubymotion/sugarcube/blob/master/lib/cocoa/sugarcube-files/nsstring.rb
3
+ class NSString
4
+
5
+ def document_path
6
+ @@motionconcierge_docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
7
+ return self if self.hasPrefix(@@motionconcierge_docs)
8
+
9
+ @@motionconcierge_docs.stringByAppendingPathComponent(self)
10
+ end
11
+
12
+ def resource_path
13
+ @@motionconcierge_resources ||= NSBundle.mainBundle.resourcePath
14
+ return self if self.hasPrefix(@@motionconcierge_resources)
15
+
16
+ @@motionconcierge_resources.stringByAppendingPathComponent(self)
17
+ end
18
+
19
+ def file_exists?
20
+ path = self.hasPrefix('/') ? self : self.document_path
21
+ NSFileManager.defaultManager.fileExistsAtPath(path)
22
+ end
23
+
24
+ def remove_file!
25
+ ptr = Pointer.new(:id)
26
+ path = self.hasPrefix('/') ? self : self.document_path
27
+ NSFileManager.defaultManager.removeItemAtPath(path, error:ptr)
28
+ ptr[0]
29
+ end
30
+
31
+ def resource_exists?
32
+ self.resource_path.file_exists?
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-concierge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Rickert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: motion-cocoapods
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: afmotion
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ - - ">"
49
+ - !ruby/object:Gem::Version
50
+ version: '10.0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '10.0'
58
+ - - ">"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: motion_print
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - - ">"
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ description: motion-concierge is your personal data concierge!. Just provide a file
82
+ name, and network url, and set up some basic rules regarding when to download the
83
+ data and the concierge will automatically fetch your data for you from the web!
84
+ email:
85
+ - mjar81@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - README.md
91
+ - lib/motion-concierge.rb
92
+ - lib/project/motion-concierge.rb
93
+ - lib/project/ns_data.rb
94
+ - lib/project/ns_string.rb
95
+ homepage: https://github.com/OTGApps/motion-concierge
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.0
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: motion-concierge is your personal data concierge!
119
+ test_files: []