# frozen_string_literal: true require_relative './base' module GetYourRep # Parses rep information from Google response. class GoogleRep < Base # An array of phone numbers. Default = [] attr_accessor :phones # An array of email addresses. Default = [] attr_accessor :emails # An array of websites. Default = [] attr_accessor :urls # An array of office addresses to be passes to a GoogleOffice instance for parsing. Default = [] attr_accessor :address # An array of social media handles. Default = [] attr_accessor :channels # Passes the GetYourRep::Representative constructor hash to different methods for assembly. attr_accessor :hash # The office name must be parsed from an array passed to the build_hash method. # the office_array is an array of hashes and is a sibling to the officials array from which # the rep info is parsed. attr_accessor :office_name # Define the :photoUrl in case it's not passed in the options. attr_accessor :photoUrl # Sets array attributes to empty arrays and and builds other attributes from options hash. def initialize(options = {}) self.phones = [] self.emails = [] self.urls = [] self.address = [] self.channels = [] super end # Build a hash from attributes to be passed to # the GetYourRep::Representative constructor method. # def build_hash(offices_array, index) find_office_name(offices_array, index) self.hash = { name: name, office: office_name, party: party, phones: phones, office_locations: office_locations, email: emails, url: url, photo: photoUrl } parse_channels # returns the hash object end private def find_office_name(offices_array, index) office_data_hash = offices_array.detect { |office| office['officialIndices'].include?(index) } self.office_name = office_data_hash['name'] end def office_locations address.map do |office| location = GoogleOffice.new(office) office_hash = location.build_hash OfficeLocation.new(office_hash) end end def url urls.first end # Parses social media handles and adds them to the Delegation object. def parse_channels channels.each do |channel| hash[channel['type'].downcase.to_sym] = channel['id'] end hash end end end