Skip to content

Commit

Permalink
Use RSS feed to fetch latest Medium posts instead
Browse files Browse the repository at this point in the history
  • Loading branch information
julianguyen committed Dec 26, 2023
1 parent 8f3f506 commit 12cb579
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ def set_blurbs
end

def parse_author(post)
post[1]['content']['subtitle'] || ''
post['author']
end

def fetch_posts
medium = Medium.new
medium_posts = Medium.new.posts
posts = []
medium.posts.each do |post|
medium_posts.each do |post|
posts.push(
link_name: post[1]['title'],
link: "https://medium.com/ifme/#{post[1]['uniqueSlug']}",
link_name: post['title'],
link: post['link'],
author: parse_author(post)
)
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/medium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

class Medium
def posts
content_hash['payload']['references']['Post']
content_hash['items']
end

private

def content_hash
JSON.parse(content[16..])
JSON.parse(content)
end

def content
content = ''
URI.open('https://medium.com/ifme?format=json', 'User-Agent' => 'if-me.org') do |file|
URI.open('https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/ifme') do |file|
file.each_line { |line| content += line }
end
content
Expand Down
5 changes: 2 additions & 3 deletions spec/services/medium_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
subject { Medium.new.posts }

it 'retrieve Medium posts from API' do
expect(subject.first[1]['previewContent']['bodyModel']['paragraphs'][1]['text']).to be_truthy
expect(subject.first[1]['title']).to be_truthy
expect(subject.first[1]['uniqueSlug']).to be_truthy
expect(subject.first['title']).to be_truthy
expect(subject.first['link']).to be_truthy
end
end

0 comments on commit 12cb579

Please sign in to comment.