Created
February 3, 2012 21:33
-
-
Save azuby/1732808 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: house | |
# | |
# id :integer not null, primary key | |
# number :string(255) | |
# subscribed :boolean default(FALSE) | |
# last_location_id :integer | |
# last_location_time :datetime | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class House < ActiveRecord::Base | |
# Associations | |
belongs_to :last_location, :class_name => "Location" | |
# Callbacks | |
before_validation { number.gsub!(/[\D]/, "") if number } # sanitize_number | |
# Validations | |
attr_accessible :number, :subscribed | |
validates :number, :presence => true | |
# Methods | |
def location | |
return nil if !subscribed? | |
if last_location_time && (Time.now - last_location_time) < 5.minutes | |
puts last_location_time, last_location | |
return last_location | |
end | |
client = Savon::Client.new(CONFIG['locator_wsdl']) | |
response = client.request(:web, :get_location) do | |
soap.body = { | |
login: CONFIG['login'], | |
password: CONFIG['password'], | |
house_num: number | |
} | |
end | |
if response.success? | |
if response.to_array(:get_location_response, :return, :msisdn_error).first.nil? | |
if status = response.to_array(:get_location_response, :return, :location_response, :status).first == "FOUND" | |
lat = response.to_array(:get_location_response, :return, :location_response, :coordinate, :y).first | |
long = response.to_array(:get_location_response, :return, :location_response, :coordinate, :x).first | |
self.last_location_time = Time.now | |
self.save! | |
return self.create_last_location( latitude: lat, longitude: long ) | |
elsif status == "NOT_FOUND" | |
return { error: { message: "House not found" } } | |
end | |
else | |
# contains :error_code amd :error_message | |
return nil | |
end | |
else | |
return nil | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: locations | |
# | |
# id :integer not null, primary key | |
# latitude :float | |
# longitude :float | |
# ip :string(255) | |
# street :string(255) | |
# city :string(255) | |
# state :string(255) | |
# zip :string(255) | |
# country :string(255) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Location < ActiveRecord::Base | |
# Validations | |
attr_accessible :latitude, :longitude, :ip, :street, :city, :state, :zip, :country | |
geocoded_by :address | |
reverse_geocoded_by :latitude, :longitude do |obj,results| | |
if geo = results.first | |
obj.street = geo.address.split(',').first | |
obj.city = geo.city | |
obj.state = geo.state | |
obj.zip = geo.postal_code | |
obj.country = geo.country | |
end | |
end | |
after_validation :geocode#, :reverse_geocode | |
# Methods | |
def address | |
[street, city, state, zip, country].reject{|l| l.blank?}.join(', ') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment