Class: Krooshal::Installation

Inherits:
Object
  • Object
show all
Defined in:
lib/krooshal/installation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_currentObject



171
172
173
# File 'lib/krooshal/installation.rb', line 171

def get_current
  @@current_installation
end

Instance Method Details

#alertView(alertView, didDismissWithButtonIndex: indexPath) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/krooshal/installation.rb', line 126

def alertView(alertView, didDismissWithButtonIndex: indexPath)
    data = @@update_running.clone
    @@update_running = false
    if data and data.length >= indexPath
        action = data[indexPath]
        type = action['type']
        url_open('actions', :POST, {action: type})
        target = action['target']
        return if is_nil_or_empty?(target)
        target_type = target['type']
        return if is_nil_or_empty?(target_type)
        if target_type != 'url'
            log_error "Unrecognized targetType: #{target_type}"
            return
        end
        target_url = target['data']
        return if is_nil_or_empty?(target_url)
        url = NSURL.URLWithString(target_url)
        unless UIApplication.sharedApplication.canOpenURL(url)
            log_error "Device cannot open #{target_url}"
            return
        end
        UIApplication.sharedApplication.openURL(url)
    end
end

#check_for_updateObject



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
# File 'lib/krooshal/installation.rb', line 101

def check_for_update
    return if @@update_running
    @@update_running = true
    url_open('status', :GET, nil, lambda{|status, body|
        @@update_running = false
        return unless body
        actions = body['actions']
        return unless actions and actions.length > 0
        @@update_running = true
        buttons = actions.collect do |action|
            action['label']
        end
        @@update_running = actions
        alert = UIAlertView.new
        alert.title = body['title']
        alert.message = body['message']
        buttons.each do |button|
            alert.addButtonWithTitle button
        end
        alert.delegate = self
        alert.show
    }, lambda{|status, body|
        @@update_running = false
    })
end

#get_installation_idObject



94
95
96
97
# File 'lib/krooshal/installation.rb', line 94

def get_installation_id
    value = get_property(:installation_id)
    return value
end

#install(api_key, on_install) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/krooshal/installation.rb', line 62

def install(api_key, on_install)
    if is_nil_or_empty?(api_key)
        log_error 'Empty api_key provided'
        return
    end
    device = UIDevice.currentDevice
    bundle = NSBundle.mainBundle

    environment = {
        osType: device.systemName,
        osVersion: device.systemVersion,
        deviceType: device.model,
        deviceMaker: :Apple,
        appVersion: bundle.infoDictionary.objectForKey('CFBundleShortVersionString'),
        appBundle: bundle.bundleIdentifier,
        timeZone: NSTimeZone.localTimeZone.abbreviation.lowercaseString,
        language: NSLocale.currentLocale.objectForKey(NSLocaleLanguageCode),
        stack: :rubymotion
    }
    @api_key = api_key
    url_open('', :POST, environment, lambda {|status, body|
        set_installation_id body['installationId'] if body and not is_nil_or_empty?(body['installationId'])
        on_install.call if on_install
    }, lambda {|status, body|
        if status == 404 and get_installation_id
            set_installation_id = nil
            install(api_key, on_install)
        else
            log_error("Check api_key")
        end
    })
end

#log_error(msg) ⇒ Object



30
31
32
# File 'lib/krooshal/installation.rb', line 30

def log_error(msg)
    NSLog("KrooshalSDK [ERROR] #{msg}")
end

#log_info(msg) ⇒ Object



27
28
29
# File 'lib/krooshal/installation.rb', line 27

def log_info(msg)
    NSLog("KrooshalSDK [INFO] #{msg}")
end

#tag(*tags) ⇒ Object



98
99
100
# File 'lib/krooshal/installation.rb', line 98

def tag(*tags)
    url_open('tags', :POST, {tags: tags})
end

#url_open(url_part, method, params, on_load = nil, on_error = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/krooshal/installation.rb', line 33

def url_open(url_part, method, params, on_load = nil, on_error = nil)
    url = 'https://api.krooshal.com/1/installations'
    installation_id = get_installation_id
    if installation_id
        log_info "Prior installation detected (#{installation_id})"
        url = "#{url}/#{installation_id}"
    else
        log_info 'No prior installation detected'
    end
    url = "#{url}/#{url_part}" unless is_nil_or_empty?(url_part)
    options = {headers: {'X-Krooshal-Api-Key' => @api_key, 'Content-Type' => 'application/json'}}            
    options[:payload] = BW::JSON.generate(params) if params
    options[:action] = lambda {|response, me|
        log_info "Got #{response.status_code}"
        body = nil
        begin
            body = BW::JSON.parse(response.body.to_str) if response.body and not is_nil_or_empty?(response.body.to_str)
            log_info "With body #{body}"
        rescue BW::JSON::ParserError
        end
        if (200..299).include?(response.status_code)
            on_load.call(response.status_code, body) if on_load
        else
            on_error.call(response.status_code, body) if on_error
        end
    }
    log_info "#{method}ing #{url} with #{params}"
    BW::HTTP::Query.new(url, method, options)
end