class Telegraph::Parser::Fetcher

Constants

HOST

Public Instance Methods

fetch(article_id) click to toggle source
# File lib/telegraph/parser/fetcher.rb, line 8
def fetch(article_id)
  response = Net::HTTP.get_response(article_uri(article_id))
  handle_response(response)
rescue EOFError
  raise Telegraph::Parser::ArticleNotFound
end
fetch_image(src, prefix) click to toggle source
# File lib/telegraph/parser/fetcher.rb, line 15
def fetch_image(src, prefix)
  src = src.start_with?('http') ? src : "https://#{HOST}#{src}"
  { image_id(src, prefix) => open(src).read }
end
image_id(src, prefix) click to toggle source
# File lib/telegraph/parser/fetcher.rb, line 20
def image_id(src, prefix)
  "#{prefix}/#{File.basename(URI.parse(src).path)}"
end

Private Instance Methods

article_uri(article_id) click to toggle source
# File lib/telegraph/parser/fetcher.rb, line 26
def article_uri(article_id)
  URI("https://#{HOST}/#{article_id}")
end
handle_response(response) click to toggle source
# File lib/telegraph/parser/fetcher.rb, line 30
def handle_response(response)
  case response.code
  when '404'
    raise Telegraph::Parser::ArticleNotFound
  when '200'
    response.body
  end
end