dimanche 29 mai 2016

Rails API Does not split Json

Weird problem. If the class at the bottom was a module, split the Json without problems, if it was only methods, also works, but the problem is.. when it is a class, it does nos split the Json anymore, returns an empty array.. however, if being a class I do a puts in the class to check the object, it actually puts the object.. Any thoughts about why? How can I fix it?

I have this controller:

 def index
   begin
     call_employee_work_locations_api
      rescue => ex
      render :json => {"service unavailable": "0001" }, :status => :service_unavailable
   end 
 end

I have this service:

def call_employee_work_locations_api
   auth = {:username=>ENV["USERNAME"], :password=>ENV["PASSWORD"]}
   employee_locations = HTTParty.get(employee_work_Location_url , :basic_auth => auth)
   #serialize_work_location(employee_locations) 
   serializer = EmployeeSerializer.new
   serializer.serialize_work_location(employee_locations)
end 

I have this builder:

json.array!(@top_locations) do |location|
   json.extract! location, :name, :description, :latitude, :longitude
end

I have this class:

class EmployeeSerializer

    def serialize_work_location(employee_locations)
        employee_locations= JSON.parse(employee_locations)
        locations=[]

        employee_locations["work_locations"].each do |attributes|
           location = Location.new(attributes["latitude"],attributes["longitude"],attributes["description"],attributes["name"])
          locations.push(location)
        end
        employee_locations_selector(locations)
    end 

    def top_office_location_selector(locations, city)
        top_locations=[]
        locations.each do |office|
            if office.name == city[0] then top_locations.push(office) end
            if office.name == city[1] then top_locations.push(office) end
        end
        @top_locations = top_locations
        p @top_locations <--- it prints the object perfectly, but does not pass to the view, I get an empty array instead.
     end

     def employee_locations_selector(locations)
        city = locations.each_with_object(Hash.new(0)) { |locations, counts| counts[locations.name] += 1 }.max_by{|k,v| v}
        top_office_location_selector(locations, city)
     end
end

Aucun commentaire:

Enregistrer un commentaire