Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Analyze the update ranking of "随時更新" items.
require 'minitest/autorun'
require 'json'
require 'date'
def execute
# How to create qiita.json
# 1. curl -o qiita.json https://qiita.com/api/v2/items?page=1&per_page=100&query=title%3A%E9%9A%8F%E6%99%82%E6%9B%B4%E6%96%B0
# 2. curl -o qiita.json https://qiita.com/api/v2/items?page=2&per_page=100&query=title%3A%E9%9A%8F%E6%99%82%E6%9B%B4%E6%96%B0
# 3. Merge them manually
path = '/Users/jit/Desktop/qiita.json'
str = File.read(path)
JSON.parse(str)
end
class Item
attr_reader :title, :created_at, :updated_at, :url, :tags, :user
def initialize(hash)
@title = hash['title']
@created_at = Date.parse hash['created_at']
@updated_at = Date.parse hash['updated_at']
@url = hash['url']
@tags = hash['tags'].map{|h| h['name']}
@user = hash['user']['id']
end
def to_s
"#{title}\t#{tags.join(', ')}\t#{user}\t#{created_at}\t#{updated_at}\t#{update_span}\t#{url}"
end
def inspect
to_s
end
def update_span
(updated_at - created_at).to_i
end
def <=>(other)
ret = update_span <=> other.update_span
ret == 0 ? created_at <=> other.created_at : ret
end
end
class QiitaTest < MiniTest::Test
def test_qiita
array = execute
items = array.map{|hash| Item.new(hash)}
puts items.sort.reverse
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment