Syndicate code posts to Github Gists
This commit is contained in:
31
lib/adamantium/client/gist.rb
Normal file
31
lib/adamantium/client/gist.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require "httparty"
|
||||
|
||||
module Adamantium
|
||||
module Client
|
||||
class Gist
|
||||
include Dry::Monads[:result]
|
||||
|
||||
def initialize(token:)
|
||||
@token = token
|
||||
end
|
||||
|
||||
def create_gist(description:, public:, files: {})
|
||||
request_body = {
|
||||
description: description,
|
||||
public: public,
|
||||
files: files
|
||||
}
|
||||
|
||||
res = HTTParty.post("https://api.github.com/gists",
|
||||
body: JSON.generate(request_body),
|
||||
headers: {Authorization: "Bearer #{@token}"})
|
||||
|
||||
if res.code >= 200 && res.code < 400
|
||||
Success(res["html_url"])
|
||||
else
|
||||
Failure(:could_not_save_gist)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
23
lib/adamantium/syndication/gist.rb
Normal file
23
lib/adamantium/syndication/gist.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require "dry/monads"
|
||||
require "sanitize"
|
||||
|
||||
module Adamantium
|
||||
module Syndication
|
||||
class Gist
|
||||
include Dry::Monads[:result]
|
||||
include Deps[gist_client: "clients.gist"]
|
||||
|
||||
def call(post:)
|
||||
description = ""
|
||||
public = true
|
||||
files = {
|
||||
post[:name] => {content: Sanitize.fragment(post[:content])}
|
||||
}
|
||||
|
||||
gist_client.create_gist(description: description,
|
||||
public: public,
|
||||
files: files)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user