Class: Danger::DangerTheCodingLove

Inherits:
Plugin
  • Object
show all
Defined in:
lib/the_coding_love/plugin.rb

Overview

Prints markdown containing a random post from [thecodinglove.com](thecodinglove.com)

Examples:

Prints markdown containing a random post


the_coding_love.random

See Also:

  • valeriomazzeo/danger-the_coding_love

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instance_nameObject



56
57
58
# File 'lib/the_coding_love/plugin.rb', line 56

def self.instance_name
  to_s.gsub('Danger', '').danger_underscore.split('/').last
end

Instance Method Details

#at_url(love_page_url) ⇒ text, image_url

Returns text and url containing given post from thecodinglove.com url

Returns:

  • (text, image_url)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/the_coding_love/plugin.rb', line 38

def at_url(love_page_url)
  require 'open-uri'
  require 'nokogiri'

  raise 'Empty coding love page URL' if love_page_url.empty?

  @doc = Nokogiri::HTML(URI.parse(love_page_url).open)
  @article = @doc.at_xpath("//article[@class='blog-post content-single']")

  text = @article.at_xpath('//h1').text
  image_url = @article.at_xpath("//div[@class='blog-post-content']/p/img/@data-src").to_s
  video_gif_url = @article.at_xpath("//div[@class='blog-post-content']/p/video//object[@type='image/gif']/@data").to_s

  return [text, video_gif_url] if image_url.empty?

  [text, image_url]
end

#randomtext, image_url

Prints markdown containing a random post from thecodinglove.com

Returns:

  • (text, image_url)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/the_coding_love/plugin.rb', line 19

def random
  rnd_url = 'https://thecodinglove.com/random'
  text, image_url = at_url(rnd_url)

  markdown(
    "------\n"\
    "#{text}\n"\
    "--\n"\
    "![Funny image](#{image_url})\n"\
    "--\n"\
    "*Source: [The Coding Love](#{rnd_url})*"
  )
  [text, image_url]
end