2016-09-23 67 views
0

我想在rails應用程序中顯示用戶的活動飼料。我正在使用feedjira。Github活動飼料網址不包括域名

2.2.4 :006 > xml_feed = Feedjira::Feed.fetch_raw "https://github.com/prasadsurase.atom" 
2.2.4 :006 > github_feed = Feedjira::Feed.parse xml_feed 
2.2.4 :006 > github_feed.entries.first.content 
=> "<!-- pull_request -->\n<svg aria-label=\"Pull request\" class=\"octicon octicon-git-pull-request dashboard-event-icon\" height=\"32\" role=\"img\" version=\"1.1\" viewBox=\"0 0 12 16\" width=\"24\"><path d=\"M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z\"></path></svg>\n\n<div class=\"time\">\n <relative-time datetime=\"2016-09-23T07:30:24Z\">Sep 23, 2016</relative-time>\n</div>\n\n<div class=\"title\">\n <a href=\"/prasadsurase\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:actor\">prasadsurase</a> opened pull request <a href=\"/joshsoftware/code-curiosity/pull/136\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:pull\">joshsoftware/code-curiosity#136</a>\n</div>\n\n<div class=\"details\">\n <a href=\"/prasadsurase\"><img alt=\"@prasadsurase\" class=\"gravatar\" height=\"30\" src=\"https://avatars3.githubusercontent.com/u/562052?v=3&amp;s=60\" width=\"30\" /></a>\n <div class=\"message\">\n <blockquote>Fixed controller method scopes. Removed unwanted routes.</blockquote>\n  <div class=\"pull-info\">\n  <svg aria-hidden=\"true\" class=\"octicon octicon-git-commit\" height=\"16\" version=\"1.1\" viewBox=\"0 0 14 16\" width=\"14\"><path d=\"M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z\"></path></svg>\n  <em>1</em> commit with\n  <em>34</em> additions and\n  <em>31</em> deletions\n  </div>\n </div>\n</div>\n" 

從HAML github_feed.entries原料渲染每進入內容時,該鏈接不包含域名https://github.com/,但只包含路徑。這會導致問題,UI上呈現的鏈接包含應用程序域而不是github。我們如何解決這個問題?

回答

2

由於github atom feeds爲內容提供相對url,所以您可能想用絕對引用替換它們。

這可能與

gsub(%r{<a href=\"/},'<a href="https://github.com/') 

並由此完成更換您:

2.2.4 :006 > github_feed.entries.first.content 

2.2.4 :006 > github_feed.entries.first.content.gsub(%r{<a href=\"/},'<a href="https://github.com/') 
=> "<!-- pull_request_review_comment -->\n<svg aria-label=\"Review pull request comment\" class=\"octicon octicon-comment-discussion dashboard-event-icon\" height=\"32\" role=\"img\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"32\"><path d=\"M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z\"></path></svg>\n\n<div class=\"time\">\n <relative-time datetime=\"2016-09-28T03:21:31Z\">Sep 28, 2016</relative-time>\n</div>\n\n<div class=\"title\">\n <a href=\"https://github.com/prasadsurase\" data-ga-click=\"News feed, event click, Event click type:PullRequestReviewCommentEvent target:actor\">prasadsurase</a> commented on pull request <a href=\"https://github.com/joshsoftware/code-curiosity/pull/137#discussion_r80837476\" data-ga-click=\"News feed, event click, Event click type:PullRequestReviewCommentEvent target:pull-comment\">joshsoftware/code-curiosity#137</a>\n</div>\n\n<div class=\"details\">\n <a href=\"https://github.com/prasadsurase\"><img alt=\"@prasadsurase\" class=\"gravatar\" height=\"30\" src=\"https://avatars3.githubusercontent.com/u/562052?v=3&amp;s=60\" width=\"30\" /></a>\n <div class=\"message markdown-body\">\n <blockquote>\n  <p><a href=\"https://github.com/BandanaPandey\" class=\"user-mention\">@BandanaPandey</a> Same here.</p>\n </blockquote>\n </div>\n</div>\n"