motion-krooshal 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,178 @@
1
+ =begin
2
+ krooshal/installation.rb (v0.5)
3
+
4
+ Copyright (c) 2013 Krooshal (http://krooshal.com/)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ =end
24
+
25
+ module Krooshal
26
+ class Installation
27
+ def log_info(msg)
28
+ NSLog("KrooshalSDK [INFO] #{msg}")
29
+ end
30
+ def log_error(msg)
31
+ NSLog("KrooshalSDK [ERROR] #{msg}")
32
+ end
33
+ def url_open(url_part, method, params, on_load = nil, on_error = nil)
34
+ url = 'https://api.krooshal.com/1/installations'
35
+ installation_id = get_installation_id
36
+ if installation_id
37
+ log_info "Prior installation detected (#{installation_id})"
38
+ url = "#{url}/#{installation_id}"
39
+ else
40
+ log_info 'No prior installation detected'
41
+ end
42
+ url = "#{url}/#{url_part}" unless is_nil_or_empty?(url_part)
43
+ options = {headers: {'X-Krooshal-Api-Key' => @api_key, 'Content-Type' => 'application/json'}}
44
+ options[:payload] = BW::JSON.generate(params) if params
45
+ options[:action] = lambda {|response, me|
46
+ log_info "Got #{response.status_code}"
47
+ body = nil
48
+ begin
49
+ body = BW::JSON.parse(response.body.to_str) if response.body and not is_nil_or_empty?(response.body.to_str)
50
+ log_info "With body #{body}"
51
+ rescue BW::JSON::ParserError
52
+ end
53
+ if (200..299).include?(response.status_code)
54
+ on_load.call(response.status_code, body) if on_load
55
+ else
56
+ on_error.call(response.status_code, body) if on_error
57
+ end
58
+ }
59
+ log_info "#{method}ing #{url} with #{params}"
60
+ BW::HTTP::Query.new(url, method, options)
61
+ end
62
+ def install(api_key, on_install)
63
+ if is_nil_or_empty?(api_key)
64
+ log_error 'Empty api_key provided'
65
+ return
66
+ end
67
+ device = UIDevice.currentDevice
68
+ bundle = NSBundle.mainBundle
69
+
70
+ environment = {
71
+ osType: device.systemName,
72
+ osVersion: device.systemVersion,
73
+ deviceType: device.model,
74
+ deviceMaker: :Apple,
75
+ appVersion: bundle.infoDictionary.objectForKey('CFBundleShortVersionString'),
76
+ appBundle: bundle.bundleIdentifier,
77
+ timeZone: NSTimeZone.localTimeZone.abbreviation.lowercaseString,
78
+ language: NSLocale.currentLocale.objectForKey(NSLocaleLanguageCode),
79
+ stack: :rubymotion
80
+ }
81
+ @api_key = api_key
82
+ url_open('', :POST, environment, lambda {|status, body|
83
+ set_installation_id body['installationId'] if body and not is_nil_or_empty?(body['installationId'])
84
+ on_install.call if on_install
85
+ }, lambda {|status, body|
86
+ if status == 404 and get_installation_id
87
+ set_installation_id = nil
88
+ install(api_key, on_install)
89
+ else
90
+ log_error("Check api_key")
91
+ end
92
+ })
93
+ end
94
+ def get_installation_id
95
+ value = get_property(:installation_id)
96
+ return value
97
+ end
98
+ def tag(*tags)
99
+ url_open('tags', :POST, {tags: tags})
100
+ end
101
+ def check_for_update
102
+ return if @@update_running
103
+ @@update_running = true
104
+ url_open('status', :GET, nil, lambda{|status, body|
105
+ @@update_running = false
106
+ return unless body
107
+ actions = body['actions']
108
+ return unless actions and actions.length > 0
109
+ @@update_running = true
110
+ buttons = actions.collect do |action|
111
+ action['label']
112
+ end
113
+ @@update_running = actions
114
+ alert = UIAlertView.new
115
+ alert.title = body['title']
116
+ alert.message = body['message']
117
+ buttons.each do |button|
118
+ alert.addButtonWithTitle button
119
+ end
120
+ alert.delegate = self
121
+ alert.show
122
+ }, lambda{|status, body|
123
+ @@update_running = false
124
+ })
125
+ end
126
+ def alertView(alertView, didDismissWithButtonIndex: indexPath)
127
+ data = @@update_running.clone
128
+ @@update_running = false
129
+ if data and data.length >= indexPath
130
+ action = data[indexPath]
131
+ type = action['type']
132
+ url_open('actions', :POST, {action: type})
133
+ target = action['target']
134
+ return if is_nil_or_empty?(target)
135
+ target_type = target['type']
136
+ return if is_nil_or_empty?(target_type)
137
+ if target_type != 'url'
138
+ log_error "Unrecognized targetType: #{target_type}"
139
+ return
140
+ end
141
+ target_url = target['data']
142
+ return if is_nil_or_empty?(target_url)
143
+ url = NSURL.URLWithString(target_url)
144
+ unless UIApplication.sharedApplication.canOpenURL(url)
145
+ log_error "Device cannot open #{target_url}"
146
+ return
147
+ end
148
+ UIApplication.sharedApplication.openURL(url)
149
+ end
150
+ end
151
+ private
152
+ def is_nil_or_empty?(value)
153
+ not value or value.to_s.length < 1
154
+ end
155
+ def set_installation_id(value)
156
+ set_property(:installation_id, value)
157
+ end
158
+ def get_property_key(key)
159
+ "Krooshal_#{@api_key}_#{key}"
160
+ end
161
+ def get_property(key)
162
+ key = get_property_key(key)
163
+ NSUserDefaults.standardUserDefaults.stringForKey(key)
164
+ end
165
+ def set_property(key, value)
166
+ key = get_property_key(key)
167
+ NSUserDefaults.standardUserDefaults.setValue(value, forKey: key)
168
+ end
169
+ @@update_running = false
170
+ class << self
171
+ def get_current
172
+ @@current_installation
173
+ end
174
+ private
175
+ @@current_installation = Installation.new
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,33 @@
1
+ =begin
2
+ motion-krooshal.rb (v0.5)
3
+
4
+ Copyright (c) 2013 Krooshal (http://krooshal.com/)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ =end
24
+
25
+ unless defined?(Motion::Project::Config)
26
+ raise "This file must be required within a RubyMotion project Rakefile."
27
+ end
28
+
29
+ Motion::Project::App.setup do |app|
30
+ Dir.glob(File.join(File.dirname(__FILE__), 'krooshal/*.rb')).each do |file|
31
+ app.files.unshift(file)
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-krooshal
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krooshal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: RubyMotion gem for Krooshal API
15
+ email: sdk@krooshal.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/motion-krooshal.rb
21
+ - lib/krooshal/installation.rb
22
+ homepage: http://rubygems.org/gems/motion-krooshal
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Krooshal keeps your app running like new
46
+ test_files: []