Module: AIA::Directives::WebAndFile

Defined in:
lib/aia/directives/web_and_file.rb

Constant Summary collapse

PUREMD_API_KEY =
ENV.fetch('PUREMD_API_KEY', nil)

Class Method Summary collapse

Class Method Details

.include(args, context_manager = nil) ⇒ Object Also known as: import



31
32
33
34
35
36
37
38
39
40
# File 'lib/aia/directives/web_and_file.rb', line 31

def self.include(args, context_manager = nil)
  # echo takes care of envars and tilde expansion
  file_path = `echo #{args.shift}`.strip

  if file_path.start_with?(/http?:\/\//)
    webpage(args)
  else
    include_file(file_path)
  end
end

.include_file(file_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aia/directives/web_and_file.rb', line 42

def self.include_file(file_path)
  @included_files ||= []
  if @included_files.include?(file_path)
    ""
  else
    if File.exist?(file_path) && File.readable?(file_path)
      @included_files << file_path
      File.read(file_path)
    else
      "Error: File '#{file_path}' is not accessible"
    end
  end
end

.included_filesObject



56
57
58
# File 'lib/aia/directives/web_and_file.rb', line 56

def self.included_files
  @included_files ||= []
end

.included_files=(files) ⇒ Object



60
61
62
# File 'lib/aia/directives/web_and_file.rb', line 60

def self.included_files=(files)
  @included_files = files
end

.paste(args = [], context_manager = nil) ⇒ Object Also known as: clipboard



64
65
66
67
68
69
70
71
# File 'lib/aia/directives/web_and_file.rb', line 64

def self.paste(args = [], context_manager = nil)
  begin
    content = Clipboard.paste
    content.to_s
  rescue => e
    "Error: Unable to paste from clipboard - #{e.message}"
  end
end

.webpage(args, context_manager = nil) ⇒ Object Also known as: website, web



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/aia/directives/web_and_file.rb', line 12

def self.webpage(args, context_manager = nil)
  if PUREMD_API_KEY.nil?
    "ERROR: PUREMD_API_KEY is required in order to include a webpage"
  else
    url = `echo #{args.shift}`.strip
    puremd_url = "https://pure.md/#{url}"

    response = Faraday.get(puremd_url) do |req|
      req.headers['x-puremd-api-token'] = PUREMD_API_KEY
    end

    if 200 == response.status
      response.body
    else
      "Error: Status was #{response.status}\n#{ap response}"
    end
  end
end