Skip to content

Instantly share code, notes, and snippets.

@ryancurtin
Forked from ornerymoose/controller.rb
Last active May 23, 2017 22:29
Show Gist options
  • Save ryancurtin/adef1885c882fa50abb202acefc5cb93 to your computer and use it in GitHub Desktop.
Save ryancurtin/adef1885c882fa50abb202acefc5cb93 to your computer and use it in GitHub Desktop.
class CustomersController < ApplicationController
include CustomersHelper
def lookup_phone
customer_data = Customer.customer_data_by_type('phone', params[:PhoneNumber])
reset_device(customer_data)
Customer.customer_data_to_json(customer_data)
end
def lookup_account_id
customer_data = Customer.customer_data_by_type('account_id', params[:AccountID])
reset_device(customer_data)
Customer.customer_data_to_json(customer_data)
end
end
class Customer < ActiveRecord::Base
def self.customer_data_by_type(type, param)
Customer.send("lookup_#{type}", param)
end
def self.customer_data_to_json(objects)
if objects.blank?
objects += @match_not_found
else
objects = objects.map{|c| [c.match_found = true, c.transfer_flag = false, c.reset_url = Customer.obtain_url(c)]}
end
objects.to_json(:methods => [:transfer_flag, :match_found, :reset_url])
end
end
module CustomersHelper
def reset_device(customer_data)
url_list = []
customer_data.each_with_index do |e, index|
url_list << e.reset_url
end
responses = []
url_list.each do |url|
responses << RestClient::Request.execute(method: :get, url: url)
end
@success = []
@failing = []
responses.zip(customer_data).each do |res, customer|
data = Hash.from_xml(res)
cust_string = "Account Code: #{customer['AccountCode']}, Serial Number: #{customer['sSerialNumber']}"
if (data['string']['xmlns'] == "http://localhost/CHRPPVWS/")
@success << cust_string
else
@failing << cust_string
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment