Class: CustomerDevice

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/customer_device.rb

Instance Method Summary collapse

Instance Method Details

#describe_idObject



8
9
10
# File 'app/models/customer_device.rb', line 8

def describe_id
"CustomerDevice ##{id}"
end

#disable_endpoint_arn(sns) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/models/customer_device.rb', line 32

def disable_endpoint_arn(sns)
	begin
		sns.set_endpoint_attributes(endpoint_arn: self.endpoint_arn,attributes: {Enabled: "false"})
		return true
	rescue Exception => e
		return false
	end
end

#endable_endpoint_arn(sns) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/models/customer_device.rb', line 23

def endable_endpoint_arn(sns)
	begin
		sns.set_endpoint_attributes(endpoint_arn: self.endpoint_arn,attributes: {Enabled: "true"})
		return true
	rescue Exception => e
		return false
	end
end

#enpoint_arn_enable?(sns) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
# File 'app/models/customer_device.rb', line 12

def enpoint_arn_enable?(sns)
	begin
		info = sns.get_endpoint_attributes(endpoint_arn: self.endpoint_arn)
		return info.attributes["Enabled"] == "true" ? true : false
	rescue Exception => e
		p "==========="
		p e
		return false
	end
end

#push_notification(message_text) ⇒ Object



54
55
56
57
58
59
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
86
87
88
89
90
91
92
93
# File 'app/models/customer_device.rb', line 54

def push_notification(message_text)
  sns = Aws::SNS::Client.new
  
  enable = self.enpoint_arn_enable?(sns)
  if enable
    if self.device_type == "Android"
      begin
      	p "================publish android"
      	gcm_message = {data: {message: message_text}}
 			message = {"GCM"=> gcm_message.to_json}.to_json
	resp = sns.publish({
	target_arn: self.endpoint_arn,
	message_structure: "json",
	message: message,})
      rescue Exception => e
        p e
      end
    elsif self.device_type == "iOS"
      iphone_notification = {aps: {alert: message_text, sound: "default", badge: 1, extra:  {a: 1, b: 2}}}
      sns_message = {
        default: "Hi there",
        APNS_SANDBOX: iphone_notification.to_json,
        APNS: iphone_notification.to_json
      }
      begin
        resp = sns.publish({
        target_arn: self.endpoint_arn,
        message: sns_message.to_json,
        message_structure:"json"
        })
        rescue Exception => e
          p e
      end
    else
      p "=====Unknown device========"
    end
  else
    self.remove_endpoint_arn(sns)
  end
end

#push_notification_agent_online(agent) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/customer_device.rb', line 163

def push_notification_agent_online(agent)
  iphone_notification = {}
		iphone_notification[:aps] = {:alert=>{:title =>"Agent is online",:body => "Join to get new agents"}}
	iphone_notification[:type] = "location_agent_online_alert"
	iphone_notification[:information] ={
		latitude: agent.latitude,
		longitude: agent.longitude
	}

	push_notification_all(iphone_notification)
end

#push_notification_all(message_text) ⇒ Object



98
99
100
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
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/customer_device.rb', line 98

def push_notification_all(message_text)
  sns = Aws::SNS::Client.new
  
  enable = self.enpoint_arn_enable?(sns)
  if enable
    if self.device_type == "Android"
      begin
      	p "================publish android"
      	gcm_message = {data: message_text}
 			message = {"GCM"=> gcm_message.to_json}.to_json
	resp = sns.publish({
	target_arn: self.endpoint_arn,
	message_structure: "json",
	message: message,})
      rescue Exception => e
        p e
      end
    elsif self.device_type == "iOS"
      iphone_notification = message_text
      sns_message = {
        default: "Hi there",
        APNS_SANDBOX: iphone_notification.to_json,
        APNS: iphone_notification.to_json
      }
      begin
        resp = sns.publish({
        target_arn: self.endpoint_arn,
        message: sns_message.to_json,
        message_structure:"json"
        })
        rescue Exception => e
          p e
      end
    else
      p "=====Unknown device========"
    end
  else
    self.remove_endpoint_arn(sns)
  end
end

#push_notification_background(type, agent, customer, sharing) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/customer_device.rb', line 224

def push_notification_background(type,agent,customer,sharing)
  iphone_notification = {}
  if type == "share_wishlist"
    iphone_notification[:aps] = {:alert=>{:title =>"Received a Share Wishlist ",:body => "From Customer: #{customer.name}"}}
    iphone_notification[:type] = "share_wishlist"
    
  elsif type == "showing_request"
    iphone_notification[:aps] = {:alert=>{:title =>"Received a Showing Request ",:body => "From Customer: #{customer.name}"}}
    iphone_notification[:type] = "showing_request"
  elsif type == "showing_request_plus"
    iphone_notification[:aps] = {:alert=>{:title =>"Received a Showing Request Plus ",:body => "From Customer: #{customer.name}"}}
    iphone_notification[:type] = "showing_request_plus"
  elsif type == "customer_picked_you"
    iphone_notification[:aps] = {:alert=>{:title =>"Customer has picked you ",:body => "From Customer: #{customer.name}"}}
    iphone_notification[:type] = "customer_picked_you"

  end
  iphone_notification[:agent_id] = agent.id
  iphone_notification[:sharing_id] = sharing.id
  push_notification_all(iphone_notification)

end

#push_notification_change_company_name(status, agent_changeinfo) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/customer_device.rb', line 176

def push_notification_change_company_name(status,agent_changeinfo)
  iphone_notification = {}
	if status == "approved"
		iphone_notification[:aps] = {:alert=>{:title =>"Update company name",:body => "Update company name was approved"}}
		iphone_notification[:type] = "change_company_name"
		iphone_notification[:information] ={
			result: "APPROVED",
			old_company_name: agent_changeinfo.company_name_old,
			last_company_name: agent_changeinfo.company_name_new
		}
	else
		iphone_notification[:aps] = {:alert=>{:title =>"Update company name",:body => "Update company name was rejected"}}
		iphone_notification[:type] = "change_company_name"
		iphone_notification[:information] ={
			result: "REJECT",
			old_company_name: agent_changeinfo.company_name_old,
			last_company_name: agent_changeinfo.company_name_new
		}

	end
	push_notification_all(iphone_notification)
end

#push_notification_change_first_last_name(status, agent_changeinfo) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/customer_device.rb', line 140

def push_notification_change_first_last_name(status,agent_changeinfo)
  iphone_notification = {}
	if status == "approved"
		iphone_notification[:aps] = {:alert=>{:title =>"Update basic profile",:body => "Profile was approved"}}
		iphone_notification[:type] = "change_basic_info"
		iphone_notification[:information] ={
			result: "APPROVED",
			first_name: agent_changeinfo.first_name_new,
			last_name: agent_changeinfo.last_name_new
		}
	else
		iphone_notification[:aps] = {:alert=>{:title =>"Update basic profile",:body => "Profile was rejected"}}
		iphone_notification[:type] = "change_basic_info"
		iphone_notification[:information] ={
			result: "REJECT",
			first_name: agent_changeinfo.first_name_old,
			last_name: agent_changeinfo.last_name_old
		}

	end
	push_notification_all(iphone_notification)
end

#push_notification_change_license(status, agent) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/customer_device.rb', line 199

def push_notification_change_license(status,agent)
  iphone_notification = {}
  if status == "approved"
    iphone_notification[:aps] = {:alert=>{:title =>"License Info",:body => "License Info was approved"}}
    iphone_notification[:type] = "change_license_info"
    iphone_notification[:information] ={
      result: "APPROVED",
      license_number: agent.license_number,
      state_license_issue: agent.license_state_issued,
      date_license_issue: agent.license_issued_date
    }
  else
    iphone_notification[:aps] = {:alert=>{:title =>"License Info",:body => "License Info was rejected"}}
    iphone_notification[:type] = "change_license_info"
    iphone_notification[:information] ={
      result: "REJECT",
      license_number: agent.license_number,
      state_license_issue: agent.license_state_issued,
      date_license_issue: agent.license_issued_date
    }

  end
  push_notification_all(iphone_notification)
end

#remove_endpoint_arn(sns) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/customer_device.rb', line 41

def remove_endpoint_arn(sns)
	begin
		endpoint_arn = self.endpoint_arn
		self.destroy
		sns.delete_endpoint(endpoint_arn: endpoint_arn)
		return true
	rescue Exception => e
		p "======="
		p e
		return false
	end
end