class Pantograph::Actions::DownloadAction

Public Class Methods

authors() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 68
def self.authors
  ['KrauseFx']
end
available_options() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 39
def self.available_options
  [
    PantographCore::ConfigItem.new(
      key: :url,
      env_name: 'DOWNLOAD_URL',
      description: 'The URL that should be downloaded',
      verify_block: proc do |value|
        UI.important('The URL does not start with http or https') unless value.start_with?('http')
      end
    )
  ]
end
category() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 64
def self.category
  :misc
end
description() click to toggle source

@!group Documentation

# File pantograph/lib/pantograph/actions/download.rb, line 28
def self.description
  'Download a file from a remote server (e.g. JSON file)'
end
details() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 32
def self.details
  [
    'Specify the URL to download and get the content as a return value.',
    'Automatically parses JSON into a Ruby data structure.'
  ].join("\n")
end
example_code() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 58
def self.example_code
  [
    'data = download(url: "https://host.com/api.json")'
  ]
end
is_supported?(platform) click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 72
def self.is_supported?(platform)
  true
end
output() click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 52
def self.output
  [
    ['DOWNLOAD_CONTENT', 'The content of the file we just downloaded']
  ]
end
run(params) click to toggle source
# File pantograph/lib/pantograph/actions/download.rb, line 8
def self.run(params)
  require 'net/http'

  begin
    result = Net::HTTP.get(URI(params[:url]))
    begin
      result = JSON.parse(result) # try to parse and see if it's valid JSON data
    rescue
      # never mind, using standard text data instead
    end
    Actions.lane_context[SharedValues::DOWNLOAD_CONTENT] = result
  rescue => ex
    UI.user_error!("Error fetching remote file: #{ex}")
  end
end