forked from stringer-rss/stringer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fever_api.rb
144 lines (117 loc) · 3.2 KB
/
fever_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require "sinatra/base"
require "sinatra/activerecord"
require "digest/md5"
require_relative "app/repositories/story_repository"
require_relative "app/repositories/feed_repository"
require_relative "app/commands/stories/mark_as_read"
require_relative "app/commands/stories/mark_as_unread"
require_relative "app/commands/stories/mark_as_starred"
require_relative "app/commands/stories/mark_as_unstarred"
class FeverAPI < Sinatra::Base
configure do
set :database_file, "config/database.yml"
register Sinatra::ActiveRecordExtension
ActiveRecord::Base.include_root_in_json = false
end
before do
halt 403 unless authenticated?(params[:api_key])
end
def authenticated?(api_key)
user = User.first
user.api_key && api_key == user.api_key
end
get "/" do
content_type :json
get_response(params)
end
post "/" do
content_type :json
get_response(params)
end
def get_response(params, is_json = true)
response = {}
response[:api_version] = 3
response[:auth] = 1
response[:last_refreshed_on_time] = Time.now.to_i
keys = params.keys.map{|k| k.to_sym}
if keys.include?(:groups)
response[:groups] = groups
response[:feeds_groups] = feeds_groups
end
if keys.include?(:feeds)
response[:feeds] = feeds.map{|f| f.as_fever_json}
response[:feeds_groups] = feeds_groups
end
if keys.include?(:favicons)
response[:favicons] = [
{
id: 0,
data: "image/gif;base64,R0lGODlhAQABAIAAAObm5gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
}
]
end
if keys.include?(:items)
if keys.include?(:with_ids)
response[:items] = stories_by_ids(params[:with_ids].split(",")).map{|s| s.as_fever_json}
response[:total_items] = stories_by_ids(params[:with_ids].split(",")).count
else
response[:items] = unread_stories(params[:since_id]).map{|s| s.as_fever_json}
response[:total_items] = unread_stories.count
end
end
if keys.include?(:links)
response[:links] = []
end
if keys.include?(:unread_item_ids)
response[:unread_item_ids] = unread_stories.map{|s| s.id}.join(",")
end
if keys.include?(:saved_item_ids)
response[:saved_item_ids] = all_starred_stories.map{|s| s.id}.join(",")
end
if params[:mark] == "item"
case params[:as]
when "read"
MarkAsRead.new(params[:id]).mark_as_read
when "unread"
MarkAsUnread.new(params[:id]).mark_as_unread
when "saved"
MarkAsStarred.new(params[:id]).mark_as_starred
when "unsaved"
MarkAsUnstarred.new(params[:id]).mark_as_unstarred
end
end
response.to_json
end
def unread_stories(since_id = nil)
if since_id
StoryRepository.unread_since_id(since_id)
else
StoryRepository.unread
end
end
def starred_stories
Story.where(is_starred: true)
end
def stories_by_ids(ids)
StoryRepository.fetch_by_ids(ids)
end
def feeds
FeedRepository.list
end
def groups
[
{
id: 1,
title: "All items"
}
]
end
def feeds_groups
[
{
group_id: 1,
feed_ids: Feed.all.map{|f| f.id}.join(",")
}
]
end
end