-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.rb
executable file
·157 lines (119 loc) · 4.62 KB
/
plugin.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
144
145
146
147
148
149
150
151
152
153
154
155
# name: discourse-googlebooks
# about: Googlebooks integration with discourse forum
# version: 1.0.0
# authors: Netdeamon
enabled_site_setting :google_books_enabled
register_asset "stylesheets/book-preview.scss"
register_asset "stylesheets/searchbook.scss"
after_initialize do
require_dependency 'topic_view_serializer'
require_dependency 'topic_list_item_serializer'
require_dependency 'guardian'
require_dependency 'search'
require_dependency 'topic';
Topic.register_custom_field_type('book', :json)
PostRevisor.track_topic_field(:bookid) do |tc,bookid|
topicbookid = "";
if(tc.topic.custom_fields && tc.topic.custom_fields["book"]!= nil) then
topicbookid = tc.topic.custom_fields['book']['id'];
end
if(topicbookid != bookid && bookid.length > 0) then
require 'net/http'
topic = tc.topic;
user = tc.user;
uri = URI.parse("https://www.googleapis.com/books/v1/volumes/"+ bookid +"?key="+SiteSetting.google_books_api_key)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri, {'Content-Type' => 'application/json','referer'=>SiteSetting.google_books_referer})
response = http.request(request)
response_body = JSON.parse(response.body);
topic.custom_fields['book'] = response_body;
topic.save
end
end
DiscourseEvent.on(:before_create_topic) do | topic, topic_creator |
user = topic_creator.user
params = topic_creator.opts
if params[:archetype] == "regular" then #check if enabled for this category
if params[:bookid] != nil && params[:bookid].length > 0 && topic.category.custom_fields['google_books_enabled'] === "true" then
require 'net/http'
bookid = params[:bookid].to_s
uri = URI.parse("https://www.googleapis.com/books/v1/volumes/"+ bookid +"?key="+SiteSetting.google_books_api_key)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri, {'Content-Type' => 'application/json','referer'=>SiteSetting.google_books_referer})
response = http.request(request)
response_body = JSON.parse(response.body);
topic.custom_fields['book'] = JSON.parse response.body;
# second inner
end
# first main if
end
end
add_to_serializer :topic_view_ , :book do
object.topic.custom_fields['book']
end
add_to_serializer :topic_list_item_ , :book do
object.custom_fields['book']
end
module ::GoogleBooks
class Engine < ::Rails::Engine
engine_name "googlebooks"
isolate_namespace GoogleBooks
end
end
class GoogleBooks::GooglebooksController < ::ApplicationController
@google_url = "https://www.googleapis.com/books/v1/volumes?fields=items(id,volumeInfo(title,authors,imageLinks))&q="
def test
render json: { name: "donut", description: "delicious!" }
end
def apikey
SiteSetting.google_books_api_key
end
def apiReferer
SiteSetting.google_books_referer
end
def gBaseUrl
"https://www.googleapis.com/books/v1/volumes"
end
def getBookById
require 'net/http'
bookid = params[:q]
uri = URI.parse(gBaseUrl+ bookid +"?key="+apikey)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri, {'Content-Type' => 'application/json','referer'=>apiReferer})
response = http.request(request)
render json: response.body
end
def search
require 'net/http'
bookname = params[:q]
uri = URI.parse(gBaseUrl+"?fields=items(id,volumeInfo(title,authors,imageLinks))&key="+apikey+"&q="+bookname)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri, {'Content-Type' => 'application/json','referer'=>apiReferer})
response = http.request(request)
render json: response.body
end
def getEnabledForCategory
category_id = params[:q]
categories ||= []
Category.find_each do |category|
temp = category.custom_fields;
temp["categoryId"] = category.id;
temp["categoryName"] = category.name;
categories.push(temp);
end
render json:categories;
end
end
GoogleBooks::Engine.routes.draw do
get '/sb' => 'googlebooks#search'
get '/gbid' => 'googlebooks#getBookById'
get '/ce' => 'googlebooks#getEnabledForCategory'
end
Discourse::Application.routes.append do
mount ::GoogleBooks::Engine, at: "/gb"
end
end