Created
May 14, 2017 09:25
-
-
Save mikejakobsen/9b50a0b5d6c107407cb104131c7cbbdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'net/http' | |
| require 'nokogiri' | |
| require 'json' | |
| class FeedParser | |
| attr_accessor :url | |
| def initialize url | |
| @url = url | |
| end | |
| def parse | |
| parser.xpath("//item").map do |item| | |
| title = "#{item.xpath('title').first.inner_text}" | |
| url = "#{item.xpath('enclosure').first.attribute('url')}" | |
| { title: title, url: url } | |
| end.to_json | |
| end | |
| def self.parse(url) | |
| new(url).parse | |
| end | |
| private | |
| def parser | |
| Nokogiri::XML.parse(get) | |
| end | |
| def get | |
| Net::HTTP.get_response(uri).body | |
| end | |
| def uri | |
| URI(url) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment