0

So I am trying to grab all high schools and graduate schools from this array of hashes:

"education": [
    {
      "school": {
        "id": "110703012290674", 
        "name": "Kunskapsgymnasiet Malmö"
      }, 
      "year": {
        "id": "136328419721520", 
        "name": "2009"
      }, 
      "type": "High School"
    }, 
    {
      "school": {
        "id": "112812485399398", 
        "name": "Malmö University"
      }, 
      "year": {
        "id": "118118634930920", 
        "name": "2012"
      }, 
      "concentration": [
        {
          "id": "104076956295773", 
          "name": "Computer Science"
        }
      ], 
      "type": "Graduate School", 
      "classes": [
        {
          "id": "165093923542525", 
          "name": "Programmering", 
          "description": "Kursen fokuserar på metoder och tekniker vid utveckling av webbapplikationer med hjälp av HTML5."
        }
      ]
    }
  ],

Like this:

if auth["extra"]["raw_info"]["education"]  

        # hash/array element 0
        if auth["extra"]["raw_info"]["education"][0]["type"] == "High School" 
          user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?)
          user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?)

        elsif auth["extra"]["raw_info"]["education"][0]["type"] == "Graduate School"
          user.graduateschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?)
          user.graduateschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?)
        end

        # hash/array element 1 
        if auth["extra"]["raw_info"]["education"][1]["type"] == "High School"
          user.highschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?)
          user.highschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?)

        elsif auth["extra"]["raw_info"]["education"][1]["type"] == "Graduate School"        
          user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?)
          user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?)
        end
      end

Is there a more efficient way to do it, like a loop of somekind? Any great ideas?

1 Answer 1

2

Yes, exactly, you should use a loop. Seems that you want something like this:

if edu = auth["extra"]["raw_info"]["education"]  
  edu.each do |e|
    if e["type"] == "High School" 
      user.highschool_name = e["school"]["name"] if (!auth["extra"].blank? && !e["school"].blank?)
      user.highschool_year = e["year"]["name"] if (!auth["extra"].blank? && !e["year"].blank?)
    elsif e["type"] == "Graduate School"
      user.graduateschool_name = e["school"]["name"] if (!auth["extra"].blank? && !e["school"].blank?)
      user.graduateschool_year = e["year"]["name"] if (!auth["extra"].blank? && !e["year"].blank?)
    end
  end
end

Update

Okay, here's even simpler version (based on tokland's suggestions).

if (auth["extra"]["raw_info"]["education"] || []).each do |e|

  nameprop, yearprop = if e["type"] == "High School"
    [:highschool_name, :highschool_year]
  elsif e["type"] == "Graduate School"
    [:graduateschool_name, :graduateschool_year]
  end

  user.send("#{nameprop}=", e["school"]["name"]) if !e["school"].blank?
  user.send("#{yearprop}=", e["year"]["name"]) if !e["year"].blank?
end
Sign up to request clarification or add additional context in comments.

3 Comments

Sergio, this can be simplified further, auth["extra"].blank? is everywhere. Also, this saves two lines a one indentation level: (something || []).each { ... }. Also, isn't the logic the same except for the receiver object? this simplified it even more.
Since !extra.blank? is everywhere it can be moved up, and I am happy with Sergio's solution :-) @SHUMAcupcake: another question would be: that kind of code tend to show problems in the data model.
@tokland: seems that we can delete !extra.blank? calls altogether. If we entered the loop, it's obviously not blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.