Created
May 9, 2017 09:31
-
-
Save mikejakobsen/6d495664f41edffce233201f3dc3a5d6 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 'json' | |
| class MercuryParser | |
| attr_reader :url | |
| def initialize(url) | |
| @url = url | |
| end | |
| def parse | |
| req = Net::HTTP::Get.new(uri) | |
| req['x-api-key'] = ENV.fetch('MERCURY_TOKEN') | |
| res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| | |
| http.request(req) | |
| end | |
| JSON.parse res.body | |
| end | |
| def self.parse(url) | |
| new(url).parse | |
| end | |
| private | |
| def uri | |
| URI(base + api_url(url)) | |
| end | |
| def base | |
| "https://mercury.postlight.com" | |
| end | |
| def api_url(url) | |
| "/parser?url=#{url}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment