使用Metainspector在Rails中解析开放图数据

时间:2017-10-21 08:32:40

标签: ruby-on-rails metainspector

我正在开发一个应用程序,我需要获取并保存网站的开放图形数据。

到目前为止,我已经能够使用此代码

获取标题,描述,网址等属性
        /*
     * Maintains the value and index of the last value in the array, while rotating the remaining values
     */
    private static Team[] rotateTeams(Team[] teamsArray)
    {
        // capture last value in array 
        Team lastPosition = teamsArray[teamsArray.Length - 1];
        // create a smaller array to hold remaining values
        Team[] smallerTeamArray = new Team[teamsArray.Length - 1];
        for (int i = 0; i < smallerTeamArray.Length; i++)
        {
            smallerTeamArray[i] = teamsArray[i];
        }
        // create a temporary smaller array to rotate values
        Team[] tempTeamsArray = new Team[smallerTeamArray.Length];
        // choose how many positions to rotate
        int rotate = 1;
        // populate temp array with rotated values
        for (int i = 0; i < smallerTeamArray.Length; i++)
        {
            tempTeamsArray[i] = smallerTeamArray[(i + rotate) % smallerTeamArray.Length];
        }
        // repopulate larger array with values and positions from smaller temp array
        for (int i = 0; i < teamsArray.Length - 1; i++)
        {
            teamsArray[i] = tempTeamsArray[i];
        }
        // add captured last value from above to final position in array
        teamsArray[teamsArray.Length - 1] = lastPosition;
        return teamsArray;
    }

我正在使用metainspector gem并尝试获取诸如og:locale,og:type之类的值。我该如何获取这些值?

这是我用来交叉参考值的链接:doesn't include the ajax module

1 个答案:

答案 0 :(得分:0)

好的,所以我设法使用

来解决它
def check_link
        begin
            @page_link = MetaInspector.new(sanitized_url)
        rescue MetaInspector::RequestError => e
            errors.add(:link, "you provided is not being read by our system. Please check the link.")
        end
    end

在我的链接模型中

接着是

def get_meta_from_link
        page = @page_link
    paje = @page_link.meta_tags
        return unless page.to_hash.present?

    if page.title.present?
      self.btitle = page.title
        end
end