class Medium

Retrieve user's content from Medium

Public Class Methods

new() click to toggle source
# File lib/service/medium.rb, line 4
def initialize
  @agent = Mechanize.new
end

Public Instance Methods

last_content(target) click to toggle source
# File lib/service/medium.rb, line 15
def last_content(target)
  stories(target).first
end
random_content(target) click to toggle source
# File lib/service/medium.rb, line 19
def random_content(target)
  stories(target).sample
end
stories(target) click to toggle source
# File lib/service/medium.rb, line 8
def stories(target)
  url = target['@'].nil? ? url_from_domain(target) : url_from_user(target)
  url = URI.join('http', url).to_s
  page = Mechanize.new.get url
  parse_stories page
end

Private Instance Methods

parse_entry(entry) click to toggle source
# File lib/service/medium.rb, line 41
def parse_entry(entry)
  {
    title: entry.search('.//title').text,
    url: entry.search('.//link').text
  }
end
parse_stories(page) click to toggle source
# File lib/service/medium.rb, line 34
def parse_stories(page)
  page.search('//item').map do |entry|
    next if entry.search('.//category').empty?
    parse_entry(entry)
  end.compact
end
url_from_domain(domain) click to toggle source
# File lib/service/medium.rb, line 29
def url_from_domain(domain)
  domain = "https://#{domain}" unless domain =~ /\A#{URI.regexp(%w[http https])}\z/
  "#{domain}/feed"
end
url_from_user(username) click to toggle source
# File lib/service/medium.rb, line 25
def url_from_user(username)
  "https://medium.com/feed/#{username}"
end