Guest User

Untitled

a guest
May 22nd, 2018
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. class Vcard < ActiveRecord::Base
  2.  
  3. def import
  4. @card = Vpim::Vcard.decode(self.data).first
  5.  
  6. get_data
  7.  
  8. find_contact
  9.  
  10. if @contact
  11. update_contact
  12. else
  13. create_contact
  14. end
  15.  
  16. process_associations
  17. end
  18.  
  19. private
  20.  
  21. def update_contact
  22. @contact.update_attributes(contact_attributes)
  23. end
  24.  
  25. def contact_attributes
  26. {:url => @url,
  27. :notes => @notes,
  28. :birthday => @birthday}
  29. end
  30.  
  31. def find_contact
  32. @card = Vpim::Vcard.decode(self.data).first
  33. [find_by_emails, find_by_phones].each do |method|
  34. contact_found = method
  35. return if contact_found
  36. end
  37. end
  38.  
  39. def find_by_emails
  40. emails = @card.emails.map {|e| remove_newlines(e.to_s.downcase)}
  41. email = Email.find_by_address(emails)
  42.  
  43. @contact = email.contact if email
  44. end
  45.  
  46. def find_by_phones
  47. phones = @card.telephones.map {|p| remove_newlines(p)}
  48. phone = Phone.find_by_number(phones)
  49.  
  50. @contact = phone.contact if phone
  51. end
  52.  
  53. def create_contact
  54. @contact = Contact.create(contact_attributes.merge(:first_name => @first_name,
  55. :last_name => @last_name))
  56. end
  57.  
  58. def process_associations
  59. card_emails
  60. card_phones
  61. card_addresses
  62. card_photo
  63. end
  64.  
  65. def get_data
  66. @first_name = @card.name.given.blank? ? nil : remove_newlines(@card.name.given.capitalize)
  67. @last_name = @card.name.family.blank? ? nil : remove_newlines(@card.name.family.capitalize)
  68. @first_name = @card.name.fullname if (@first_name.nil? && @last_name.nil? && !@card.name.fullname.blank?)
  69. @birthday = @card.birthday.blank? ? nil : @card.birthday
  70. @url = @card.url.blank? ? nil : remove_newlines(@card.url.uri.gsub("\\",""))
  71. @notes = @card.note.blank? ? nil : remove_newlines(@card.note)
  72. end
  73.  
  74. def card_addresses
  75. street_numbers = @contact.addresses.map(&:street_1).map(&:numbers_only)
  76.  
  77. @card.addresses.each do |@address|
  78. # don't bother saving the address if there is no region (state)
  79. # assume that if the contact currently has an address with the same numbers that this address is already stored
  80.  
  81. unless @address.region.blank? || street_numbers.include?(@address.street.numbers_only)
  82.  
  83. @country = get_country || Country.find_by_name("United States")
  84.  
  85. region = get_region
  86. next if region.nil?
  87.  
  88. # labels retrieved from the vcard are only home or work.. custom does not work.
  89. @contact.addresses << Address.create(:country_id => @country.id,
  90. :region_id => region.id,
  91. :street_1 => remove_newlines(@address.street),
  92. :street_2 => remove_newlines(@address.pobox),
  93. :city => remove_newlines(@address.locality),
  94. :postal_code => remove_newlines(@address.postalcode),
  95. :label => get_label(@address))
  96.  
  97. end
  98. end
  99. end
  100.  
  101. def get_country
  102. if !@address.country.blank?
  103. card_country = @address.country.gsub(".","")
  104. countries = Country.all
  105. country_attributes = [:name, :official_name, :alpha_2_code, :alpha_3_code]
  106. find_in_list(countries, country_attributes, card_country)
  107. end
  108. end
  109.  
  110. def get_region
  111. # best way I could deal with nev. => nev
  112. card_region = (@address.region.last == "." ? @address.region.chop : @address.region)
  113.  
  114. regions_for_country = Region.find_all_by_country_id(@country.id)
  115. region_attributes = [:name, :abbreviation, :abbrev2, :abbrev3]
  116. find_in_list(regions_for_country, region_attributes, card_region)
  117. end
  118.  
  119. def find_in_list(records, attributes, to_find)
  120. # a better way of doing Country.all.find {|c| [c.name, c.official_name].compact.map(&:downcase).include?(to_find.downcase) }
  121. records.find {|r| attributes.map{|a| r.send(a)}.compact.map(&:downcase).include?(to_find.downcase) }
  122. end
  123.  
  124. def card_phones
  125. @card.telephones.each do |phone|
  126. # phone is something like: #<Vpim::Vcard::Telephone: "123-456-7890", pref, work
  127. # doing phone.to_s will extract just the number
  128.  
  129. unless @contact.phones.map(&:number).map(&:numbers_only).include?(phone.to_s.numbers_only)
  130. # labels retrieved from the vcard are only home or work.. custom does not work.
  131.  
  132. @contact.phones << Phone.create(:label => get_label(phone), :number => remove_newlines(phone.to_s))
  133. end
  134. end
  135. end
  136.  
  137. def card_emails
  138. @card.emails.each do |email|
  139. # email is something like: #<Vpim::Vcard::Email: "test@yahoo.com", pref, home
  140. # doing email.to_s will extract just the email address
  141.  
  142. unless @contact.emails.map(&:address).map(&:downcase).include?(email.to_s.downcase)
  143. # labels retrieved from the vcard are only home or work.. custom does not work.
  144.  
  145. @contact.emails << Email.create(:label => get_label(email), :address => remove_newlines(email.to_s.downcase))
  146. end
  147. end
  148. end
  149.  
  150. def card_photo
  151. unless @card.photos.size.zero?
  152. if @contact.photo.nil?
  153. photo_album = PhotoAlbum.find_or_create_by_name_with_access_level('users', 'admin')
  154. access = AccessLevel.find_by_name('public')
  155. @contact.photo = Photo.create(:image_file_string => @card.photos.first,
  156. :contact_id => @contact.id,
  157. :name => "#{@contact.first_name} #{@contact.last_name}".strip,
  158. :description => "User photo",
  159. :access_level_id => access.id,
  160. :photo_album_id => photo_album.id,
  161. :server_format => "JPEG")
  162. end
  163. end
  164. end
  165.  
  166. def get_label(obj)
  167. obj.location.blank? ? nil : remove_newlines(obj.location).gsub("-","").strip
  168. end
  169.  
  170. def remove_newlines(obj)
  171. obj.to_s.gsub("\n", " ")
  172. end
Add Comment
Please, Sign In to add comment