Class: FrenchPress::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/frenchpress/post.rb,
lib/frenchpress/post/code.rb,
lib/frenchpress/post/link.rb,
lib/frenchpress/post/image.rb,
lib/frenchpress/post/generic.rb,
lib/frenchpress/post/spotify.rb,
lib/frenchpress/post/youtube.rb,
lib/frenchpress/post/markdown.rb

Overview

This represents a post in a FrenchPress blog

Defined Under Namespace

Classes: Code, Generic, Image, Link, Markdown, Spotify, Youtube

Class Method Summary collapse

Class Method Details

.generate_reply(original_url, *args) ⇒ Object



49
50
51
52
53
# File 'lib/frenchpress/post.rb', line 49

def self.generate_reply(original_url, *args)
  original = get_post_from_url original_url
  reply = new_post_from_multiple(*args) # Form a reply from all other args
  new_post_from_multiple original.render_as_quote, reply.render
end

.get_post_from_url(url_string) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/frenchpress/post.rb', line 83

def self.get_post_from_url(url_string)
  # Ensure we have http:// prefix
  url_string = url_string.gsub('http://', '').gsub('https://', '')
  url_string = url_string.prepend('https://') # i'm an optimist
  post_uri = URI.parse(url_string)
  # Update path to point to raw file
  post_uri.path = post_uri.path.split('/').pop.prepend('/raw/')
  dl = nil
  begin
    dl = post_uri.open.read.dup # Dup to remove frozen state
  rescue
    begin
      url_string = url_string.gsub 'https://', 'http://'
      post_uri = URI.parse url_string
      post_uri.path = post_uri.path.split('/').pop.prepend('/raw/')
      dl = post_uri.open.read.dup
    rescue StandardError => e
      puts "Woah! I can't grab the original post. Check your internet."
      puts e
      exit 1
    end
  end
  post = FrenchPress::Post.new_post dl
  post.parent_blog = {
    url: url_string,
    host: post_uri.host
  }
  post
end

.new_post(item) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/frenchpress/post.rb', line 13

def self.new_post(item)
  type = parse_type item
  file = nil
  if type =~ /^file-[^-]+$/
    item, file = parse_file item
    type = type.gsub 'file-', ''
  end
  case type
  when 'text'
    FrenchPress::Post::Generic.new(item)
  when 'code'
    FrenchPress::Post::Code.new(item, file)
  when 'markdown'
    FrenchPress::Post::Markdown.new(item)
  when 'image'
    FrenchPress::Post::Image.new(item, type.split('-')[1])
  when 'link'
    FrenchPress::Post::Link.new(item)
  when 'spotify'
    FrenchPress::Post::Spotify.new(item)
  when 'youtube'
    FrenchPress::Post::Youtube.new(item)
  end
end

.new_post_from_multiple(*args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/frenchpress/post.rb', line 38

def self.new_post_from_multiple(*args)
  # Check we really have multiple arguments
  return new_post(args[0]) if args.length == 1

  args.map!(&method(:new_post)) # Run new_post on each item in args
  args.map!(&:render) # Render each item in args (which are now posts)

  # Then join them with newlines, and pass them to a new, combined post
  FrenchPress::Post::Generic.new args.join("\n\n")
end

.parse_file(f) ⇒ Object



113
114
115
116
117
118
# File 'lib/frenchpress/post.rb', line 113

def self.parse_file(f)
  file = File.open f
  r = file.read
  file.close
  [r, f.split('.').pop]
end

.parse_type(item) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/frenchpress/post.rb', line 55

def self.parse_type(item)
  if item =~ /\A#{URI.regexp(%w(http https))}\z/
    if item.include?('open.spotify.com')
      return 'spotify'
    elsif item.include?('youtube.com')
      return 'youtube'
    else
      return 'link'
    end
  elsif File.exist?(item)
    filetype = item.split('.').pop
    # it's a file that's being posted
    # is it a text or markdown file?
    # or possibly a code snippet
    if %w(md markdown).include?(filetype)
      return 'file-markdown'
    elsif %w(rb py c go cpp rs swift js yml).include?(filetype)
      return 'file-code'
    elsif %w(png jpg).include?(filetype)
      return "file-image-#{filetype}"
    else
      return 'file-text'
    end
  else
    return 'text'
  end
end