-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- first touch - parse order_id - parse order documents - store topic id with order id - create post for existed topic
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,34 @@ | |
|
||
module Uvobot | ||
class DiscourseClient | ||
def initialize(host, api_key, api_username) | ||
def initialize(host: nil, api_key: nil, api_username: nil, local_store: nil) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
quatermain
Author
Owner
|
||
@client = DiscourseApi::Client.new(host, api_key, api_username) | ||
@local_store = local_store | ||
end | ||
|
||
def store_topic(order_id: nil, topic: nil, category: nil) | ||
if @local_store.check_topic?(order_id) | ||
This comment has been minimized.
Sorry, something went wrong.
jsuchal
|
||
create_post(@local_store.get_topic_id(order_id), topic[:body]) | ||
else | ||
response = create_topic( | ||
title: topic[:title], | ||
raw: topic[:body], | ||
category: category | ||
) | ||
# TODO: get topic_id and store it | ||
end | ||
end | ||
|
||
def create_topic(args = {}) | ||
@client.create_topic(args) | ||
rescue DiscourseApi::Error | ||
return nil | ||
end | ||
|
||
def create_post(topic_id, content) | ||
@client.create_post(topic_id: topic_id, raw: content) | ||
rescue DiscourseApi::Error | ||
return nil | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,33 +20,42 @@ def new_issue_not_published | |
def matching_announcements_found(_page_info, announcements) | ||
announcements.each do |a| | ||
topic = announcement_to_topic(a) | ||
@client.create_topic( | ||
title: topic[:title], | ||
raw: topic[:body], | ||
category: @category | ||
) | ||
@client.store_topic(order_id: a[:order][:id], topic: topic, category: @category) | ||
end | ||
end | ||
|
||
private | ||
|
||
def announcement_to_topic(announcement) | ||
detail = @scraper.get_announcement_detail(announcement[:link][:href]) | ||
|
||
{ | ||
title: announcement[:procurement_subject].to_s, | ||
body: ["**Obstarávateľ:** #{announcement[:procurer]}", | ||
"**Predmet obstarávania:** #{announcement[:procurement_subject]}", | ||
detail_message(detail), | ||
"**Zdroj:** [#{announcement[:link][:text]}](#{announcement[:link][:href]})"].join(" \n") | ||
} | ||
details = @scraper.get_announcement_detail(announcement[:link][:href], announcement[:release_date]) | ||
This comment has been minimized.
Sorry, something went wrong.
jsuchal
|
||
response = {} | ||
response[:title] = announcement[:procurement_subject].to_s | ||
response[:body] = [ | ||
"**Obstarávateľ:** #{announcement[:procurer]}", | ||
"**Predmet obstarávania:** #{announcement[:procurement_subject]}", | ||
price_details(details), | ||
order_documents(details), | ||
"**Zdroj:** [#{announcement[:link][:text]}](#{announcement[:link][:href]})" | ||
].join(" \n") | ||
end | ||
|
||
def detail_message(detail) | ||
if detail | ||
"**Cena:** #{detail[:amount]}" | ||
def price_details(details) | ||
if details && details[:amount] | ||
"**Cena:** #{details[:amount]}" | ||
else | ||
'**Detaily sa nepodarilo extrahovať.**' | ||
'**Cena:** nepodarilo sa extrahovať' | ||
end | ||
end | ||
|
||
def order_documents(details) | ||
if details && details[:order] && details[:order][:documents] | ||
response = [ "** Dokumenty zákazky:**" ] | ||
details[:order][:documents].each do |document| | ||
response << "#{document[:name]} [#{document[:href]}]" | ||
end | ||
response.join(" \n") | ||
else | ||
'** Dokumenty zákazky:** nepodarilo sa extrahovať.**' | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require_relative 'store/manager' | ||
|
||
module Uvobot | ||
module Store | ||
end | ||
end | ||
This comment has been minimized.
Sorry, something went wrong. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Uvobot::Store | ||
class CreateTopicsTable < ::ActiveRecord::Migration | ||
|
||
def up | ||
create_table :topics do |t| | ||
t.string :order_id | ||
This comment has been minimized.
Sorry, something went wrong. |
||
t.string :topic_id | ||
end | ||
add_index :topics, :order_id | ||
This comment has been minimized.
Sorry, something went wrong. |
||
end | ||
|
||
def down | ||
drop_table :topics | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'active_record' | ||
require_relative 'create_topics_table' | ||
require_relative 'topic' | ||
|
||
module Uvobot | ||
module Store | ||
class Manager | ||
This comment has been minimized.
Sorry, something went wrong.
jsuchal
|
||
def initialize(database_url) | ||
ActiveRecord::Base.establish_connection(database_url) | ||
check_migration | ||
end | ||
|
||
def check_topic?(order_id) | ||
Topic.where(order_id: order_id).count > 0 | ||
end | ||
|
||
def get_topic_id(order_id) | ||
Topic.where(order_id: order_id).first.topic_id | ||
end | ||
|
||
def check_migration | ||
unless ActiveRecord::Base.connection.table_exists? 'topics' | ||
run_migration | ||
end | ||
end | ||
|
||
private | ||
|
||
def run_migration | ||
CreateTopicsTable.migrate(:up) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Uvobot::Store | ||
class Topic < ::ActiveRecord::Base | ||
|
||
end | ||
end |
preco default nil? Vie to fungovat bez toho?