Last active
August 29, 2015 14:06
-
-
Save leftis/0dfaf8dc836743bf259e to your computer and use it in GitHub Desktop.
This file contains 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
# require 'net/ftp' | |
require 'nokogiri' | |
module Orbit | |
mattr_accessor :folder, :file | |
DEFAULT_OPTIONS = { | |
:folder => Pathname.new( Rails.public_path + '/orbit'), | |
:file => 'orbit.xml' | |
} | |
def Orbit.file_path_for(file) | |
begin | |
@@folder + file | |
rescue Exception => e | |
puts e | |
end | |
end | |
class Properties | |
def initialize(xpath, properties = [] ) | |
@xpath ||= xpath | |
@orbit_ids ||= @xpath.xpath(".//PID").map(&:text) | |
@properties = properties.blank? ? Villa.all : properties | |
synch | |
end | |
def synch | |
delete_properties_that_was_deleted_from_orbit | |
create_or_update orbit_properties | |
end | |
def create_or_update properties | |
properties.each do |property| | |
p = Villa.find_or_initialize_by_orbit_id property[:orbit_id] | |
if p.new_record? | |
update_property_images(p, property) | |
update_property_attributes(p, property) | |
else | |
update_property_attributes(p, property) if needs_update? p, property | |
update_property_images(p, property) if images_needs_update? p, property | |
end | |
end | |
end | |
def update_property_attributes(property, attributes) | |
property.attributes = attributes | |
property.save! | |
end | |
def update_property_images(property, attributes) | |
current_property_images_path = Orbit.file_path_for(attributes[:orbit_id]) | |
media = [] | |
Dir["#{current_property_images_path}/*"].each do |image| | |
medium = Medium.find_or_initialize_by_data_file_name File.basename(image) | |
medium.data = File.new(image, 'r') | |
medium.save! | |
media.push medium.id | |
end | |
property.medium_ids = media | |
property.save! | |
end | |
def images_needs_update?(active_record_property, property_from_hash) | |
current_property_images_path = Orbit.file_path_for(property_from_hash[:orbit_id]) | |
xml_date, property_date = parse_dates_from_hash_and_property(property_from_hash[:last_photo_update], active_record_property.updated_at) | |
return true if xml_date.present? && property_date.present? && ( (Dir["#{current_property_images_path}/*"].count != active_record_property.media.count) || (xml_date > property_date) ) | |
false | |
end | |
def needs_update?(active_record_property, property_from_hash) | |
xml_date, property_date = parse_dates_from_hash_and_property(property_from_hash[:last_update_date], active_record_property.updated_at) | |
return true if property_date.blank? | |
return true if xml_date.present? && property_date.present? && xml_date > property_date | |
false | |
end | |
def parse_dates_from_hash_and_property(xml_date, property_date) | |
[DateTime.parse(xml_date), property_date] if xml_date.present? && property_date.present? | |
end | |
def orbit_properties | |
properties = [] | |
@xpath.xpath('.//Property').each do |node| | |
property = { | |
:orbit_id => 'PID', | |
:available => 'PDIATH', | |
:buy => 'PAGOROPOL', | |
:rent => 'PENOIKIASI', | |
:granting => 'PANTIPAR', | |
:area_id => 'PPERIOXI', | |
:land_size => 'PTOTEPIOIK', | |
:level => 'POROFOS', | |
:bedrooms => 'PBEDROOM', | |
:fireplace => 'PTZAKI', | |
:warehouse => 'PAPOTHIKI', | |
:garage => 'PGARAGE', | |
:parking => 'PPARKING', | |
:floors => 'PFLOORS', | |
:basement => 'PBASEMENTS', | |
:price => 'PASKPRICE', | |
:comments => 'PMEMO', | |
:pool => 'PPOOL', | |
:body => 'PAGGELIA', | |
:video_url => 'PVIDEOURL', | |
:last_update_date => 'LASTUPDATE', | |
:last_photo_update => 'LASTPHUPDT', | |
:build_size => 'PTOTEPIKAT', | |
:surface_comments => 'PEPIFMEMO', | |
:bathrooms => 'PBATBHROOM', | |
:lat => 'PLATITUDE', | |
:lng => 'PLONGTITUDE' | |
} | |
# Use the get_xpathed method to replace values with actual values from xpath | |
properties.push property.inject({}) { |h, (k, v)| h[k] = get_xpathed(node, v); h } | |
end | |
properties | |
end | |
def get_xpathed(node, attribute) | |
node.xpath(".//#{attribute}").text | |
end | |
def delete_properties_that_was_deleted_from_orbit | |
properties_to_destroy = @properties.reject{ |property| @orbit_ids.include? property.orbit_id.to_s } | |
properties_to_destroy.each(&:destroy) if properties_to_destroy.present? | |
end | |
end | |
class XML | |
def initialize(options = {}) | |
options = Orbit::DEFAULT_OPTIONS if options.blank? | |
process(options) | |
Orbit::Properties.new(open_xml) | |
end | |
def open_xml | |
begin | |
file = File.open(file_path) | |
@xml = Nokogiri::XML(file) | |
rescue Exception => e | |
puts e | |
ensure | |
file.close | |
end | |
end | |
def file_path | |
Orbit::file_path_for(Orbit.send('file')) | |
end | |
def process(options) | |
options.each_pair do |key, value| | |
Orbit.send("#{key}=", value) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment