getway lib

This commit is contained in:
呱呱呱 2023-09-27 14:12:03 +08:00
parent b080b8fca5
commit 9d5c5acfbf
8 changed files with 169 additions and 14 deletions

20
app/libs/getway.rb Normal file
View File

@ -0,0 +1,20 @@
module Getway
class << self
def getway_config
getway_config = {}
begin
config = Rails.application.config_for(:configuration).symbolize_keys!
getway_config = config[:getway].symbolize_keys!
raise 'getway config missing' if getway_config.blank?
rescue => ex
raise ex if Rails.env.production?
puts %Q{\033[33m [warning] getway config or configuration.yml missing,
please add it or execute 'cp config/configuration.yml.example config/configuration.yml' \033[0m}
getway_config = {}
end
getway_config
end
end
end

View File

@ -41,6 +41,29 @@ class Topic < ApplicationRecord
end end
end end
def get_visitor_data
data = {
visits: 0,
created_time: format_time(Time.now)
}
if self.url.include?("gitlink.org.cn/forums/") || self.url.include?("trustie.net/forums/")
request_memo = Forum::Memos::GetService.call(self.uuid)
binding.pry
data[:visits] = request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"]
data[:created_time] = request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"]
end
if self.url.include?("gitlink.org.cn/zone/") || self.url.include?("trustie.net/zone/")
request_doc = Getway::Cms::GetService.call(self.uuid)
data[:visits] = request_doc.nil? ? 0 : request_doc["data"]["visits"]
data[:created_time] = request_doc.nil? ? format_time(Time.now) : request_doc["data"]["publishTime"]
end
data
end
private private
def image_url(type) def image_url(type)

View File

@ -0,0 +1,94 @@
class Getway::ClientService < ApplicationService
attr_reader :url, :params
PAGINATE_DEFAULT_PAGE = 1
PAGINATE_DEFAULT_LIMIT = 20
def initialize(options={})
@url = options[:url]
@params = options[:params]
end
def get(url, params={})
conn(params).get do |req|
req.url full_url(url, 'get')
params.except(:token).each_pair do |key, value|
req.params["#{key}"] = value
end
end
# response.headers.each do |k,v|
# puts "#{k}:#{v}"
# end #=> 响应头
end
private
def conn(auth={})
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.response :logger # 显示日志
req.adapter Faraday.default_adapter
end
end
@client
end
def base_url
Getway.getway_config[:base_url]
end
def domain
Getway.getway_config[:domain]
end
def api_url
[domain, base_url].join('')
end
def full_url(api_rest, action='post')
url = [api_url, api_rest].join('').freeze
url = action === 'get' ? url : URI.escape(url)
url = URI.escape(url) unless url.ascii_only?
puts "[getway] request url: #{url}"
return url
end
def render_response(response)
status = response.status
body = response&.body
# log_error(status, body)
body, message = get_body_by_status(status, body)
[status, message, body]
end
def get_body_by_status(status, body)
body, message =
case status
when 401 then [nil, "401"]
when 404 then [nil, "404"]
when 403 then [nil, "403"]
when 500 then [nil, "500"]
else
if body.present?
body = JSON.parse(body)
fix_body(body)
else
nil
end
end
[body, message]
end
def fix_body(body)
return [body, nil] if body.is_a?(Array) || body.is_a?(Hash)
body['message'].blank? ? [body, nil] : [nil, body['message']]
end
end

View File

@ -0,0 +1,21 @@
class Getway::Cms::GetService < Getway::ClientService
attr_reader :doc_id
def initialize(doc_id)
@doc_id = doc_id
end
def call
response = get(url)
code, message, body = render_response(response)
if code == 200 && body["code"] == 200
return body
else
return nil
end
end
def url
"/cms/doc/open/#{doc_id}".freeze
end
end

View File

@ -1,4 +1,4 @@
json.(activity_forum, :id, :title, :url) json.(activity_forum, :id, :title, :url)
request_memo = Forum::Memos::GetService.call(activity_forum&.uuid)
json.visits request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"] json.visits activity_forum.get_visitor_data[:visits]
json.created_time request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"] json.created_time activity_forum.get_visitor_data[:visits]

View File

@ -1,4 +1,3 @@
json.(experience_forum, :id, :title, :url) json.(experience_forum, :id, :title, :url)
request_memo = Forum::Memos::GetService.call(experience_forum&.uuid) json.visits experience_forum.get_visitor_data[:visits]
json.visits request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"] json.created_time experience_forum.get_visitor_data[:visits]
json.created_time request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"]

View File

@ -1,4 +1,3 @@
json.(glcc_news, :id, :title, :url, :uuid) json.(glcc_news, :id, :title, :url, :uuid)
request_memo = Forum::Memos::GetService.call(glcc_news&.uuid) json.visits glcc_news.get_visitor_data[:visits]
json.visits request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"] json.created_time glcc_news.get_visitor_data[:visits]
json.created_time request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"]

View File

@ -1,4 +1,3 @@
json.(pinned_forum, :id, :title, :url) json.(pinned_forum, :id, :title, :url)
request_memo = Forum::Memos::GetService.call(pinned_forum&.uuid) json.visits pinned_forum.get_visitor_data[:visits]
json.visits request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"] json.created_time pinned_forum.get_visitor_data[:visits]
json.created_time request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"]