Class: Skiptorium::Blogger::Destination
- Inherits:
-
Skiptorium::BaseDestination
- Object
- Skiptorium::BaseDestination
- Skiptorium::Blogger::Destination
- Defined in:
- lib/skiptorium/blogger/destination.rb
Constant Summary collapse
- API_BASE =
'https://www.googleapis.com/blogger/v3'- TOKEN_URL =
'https://oauth2.googleapis.com/token'
Instance Attribute Summary
Attributes inherited from Skiptorium::BaseDestination
Class Method Summary collapse
Instance Method Summary collapse
- #exists?(article, force: false) ⇒ Boolean
-
#initialize(config) ⇒ Destination
constructor
A new instance of Destination.
- #publish(article) ⇒ Object
Methods inherited from Skiptorium::BaseDestination
Constructor Details
#initialize(config) ⇒ Destination
Returns a new instance of Destination.
9 10 11 12 13 14 15 16 |
# File 'lib/skiptorium/blogger/destination.rb', line 9 def initialize(config) super @blog_id = config['blog_id'] @api_key = config['api_key'] @client_id = config['client_id'] @client_secret = config['client_secret'] @refresh_token = config['refresh_token'] end |
Class Method Details
.to_article(item) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/skiptorium/blogger/destination.rb', line 88 def self.to_article(item) Article.new( id: item['id'].to_s, title: item['title'], published_at: Article.parse_date(item['published']), markdown: false ) end |
Instance Method Details
#exists?(article, force: false) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/skiptorium/blogger/destination.rb', line 18 def exists?(article, force: false) response = with_cache(:articles, force: force) do with_client(url: API_BASE) do |client| client.get("blogs/#{@blog_id}/posts", { 'key' => @api_key, 'fetchBodies' => 'false', 'maxResults' => 50 }) end end return false unless response.success? items = JSON.parse(response.body)['items'] || [] articles = items.map(&:with_indifferent_access).map(&self.class.method(:to_article)) articles.any? do |a| article.same?(a) end end |
#publish(article) ⇒ Object
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 |
# File 'lib/skiptorium/blogger/destination.rb', line 40 def publish(article) access_token = refresh_access_token html_content = article.to_html client = Faraday.new(url: API_BASE) do |builder| builder.headers['Authorization'] = "Bearer #{access_token}" builder.headers['Content-Type'] = 'application/json' builder.adapter Faraday.default_adapter end payload = { 'kind' => 'blogger#post', 'blog' => { 'id' => @blog_id }, 'title' => article.title, 'content' => html_content } payload['labels'] = article. if article.&.any? response = client.post("blogs/#{@blog_id}/posts", JSON.generate(payload)) raise "Blogger publish error: #{response.status} - #{response.body}" unless response.success? post = JSON.parse(response.body) puts " Published to Blogger: #{post['url']}" end |