Class: PagecordCLI::PostFile
- Inherits:
-
Object
- Object
- PagecordCLI::PostFile
- Defined in:
- lib/pagecord_cli/post_file.rb
Constant Summary collapse
- MARKDOWN_EXTENSIONS =
[ ".md", ".markdown" ].freeze
- HTML_EXTENSIONS =
[ ".html", ".htm" ].freeze
- FRONT_MATTER =
/\A---[ \t]*\n(.*?)\n---[ \t]*\n?/m- HTML_METADATA =
/\A\s*<!--\s*pagecord:\s*\n(.*?)\n-->\s*/m
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #content_for(blog, client:) ⇒ Object
- #content_format ⇒ Object
- #default_title ⇒ Object
- #html? ⇒ Boolean
-
#initialize(path) ⇒ PostFile
constructor
A new instance of PostFile.
- #markdown? ⇒ Boolean
- #params ⇒ Object
- #supported? ⇒ Boolean
- #token_for(blog) ⇒ Object
- #write_token(blog, token, api_key: nil, status: nil) ⇒ Object
- #wrong_blog?(blog, api_key) ⇒ Boolean
Constructor Details
#initialize(path) ⇒ PostFile
Returns a new instance of PostFile.
16 17 18 19 20 |
# File 'lib/pagecord_cli/post_file.rb', line 16 def initialize(path) @path = path @content = File.read(path) @metadata, @body = end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
14 15 16 |
# File 'lib/pagecord_cli/post_file.rb', line 14 def body @body end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
14 15 16 |
# File 'lib/pagecord_cli/post_file.rb', line 14 def content @content end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
14 15 16 |
# File 'lib/pagecord_cli/post_file.rb', line 14 def @metadata end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/pagecord_cli/post_file.rb', line 14 def path @path end |
Class Method Details
.blog_fingerprint(api_key) ⇒ Object
91 92 93 |
# File 'lib/pagecord_cli/post_file.rb', line 91 def self.blog_fingerprint(api_key) Digest::SHA256.hexdigest(api_key)[0, 12] end |
Instance Method Details
#content_for(blog, client:) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pagecord_cli/post_file.rb', line 38 def content_for(blog, client:) return body unless markdown? ImageUploads.new( body, file_path: path, metadata: , blog: blog, client: client ).process end |
#content_format ⇒ Object
62 63 64 |
# File 'lib/pagecord_cli/post_file.rb', line 62 def content_format markdown? ? "markdown" : nil end |
#default_title ⇒ Object
95 96 97 98 |
# File 'lib/pagecord_cli/post_file.rb', line 95 def default_title title = File.basename(path, File.extname(path)).tr("_-", " ").squeeze(" ").strip title.empty? ? "" : title[0].upcase + title[1..].to_s end |
#html? ⇒ Boolean
26 27 28 |
# File 'lib/pagecord_cli/post_file.rb', line 26 def html? HTML_EXTENSIONS.include?(File.extname(path).downcase) end |
#markdown? ⇒ Boolean
22 23 24 |
# File 'lib/pagecord_cli/post_file.rb', line 22 def markdown? MARKDOWN_EXTENSIONS.include?(File.extname(path).downcase) end |
#params ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pagecord_cli/post_file.rb', line 66 def params publish_params = { title: } %w[slug published_at canonical_url locale].each do |key| value = frontmatter_string([key]) publish_params[key.to_sym] = value if value end = publish_params[:tags] = if hidden = frontmatter_boolean(["hidden"]) publish_params[:hidden] = hidden unless hidden.nil? publish_params end |
#supported? ⇒ Boolean
100 101 102 |
# File 'lib/pagecord_cli/post_file.rb', line 100 def supported? markdown? || html? end |
#token_for(blog) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/pagecord_cli/post_file.rb', line 30 def token_for(blog) if markdown? ["pagecord_token"] else .dig(blog, "token") || [blog] end end |
#write_token(blog, token, api_key: nil, status: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pagecord_cli/post_file.rb', line 50 def write_token(blog, token, api_key: nil, status: nil) if markdown? ["pagecord_token"] = token ["pagecord_blog_fingerprint"] = self.class.blog_fingerprint(api_key) if api_key ["status"] = status if status write_markdown else [blog] = token write_html end end |
#wrong_blog?(blog, api_key) ⇒ Boolean
83 84 85 86 87 88 89 |
# File 'lib/pagecord_cli/post_file.rb', line 83 def wrong_blog?(blog, api_key) return false unless markdown? return false unless ["pagecord_token"] return false unless ["pagecord_blog_fingerprint"] ["pagecord_blog_fingerprint"].to_s != self.class.blog_fingerprint(api_key) end |