Class: Pantograph::Actions::DownloadAction

Inherits:
Pantograph::Action show all
Defined in:
pantograph/lib/pantograph/actions/download.rb

Constant Summary

Constants inherited from Pantograph::Action

Pantograph::Action::AVAILABLE_CATEGORIES, Pantograph::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Pantograph::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



68
69
70
# File 'pantograph/lib/pantograph/actions/download.rb', line 68

def self.authors
  ['KrauseFx']
end

.available_optionsObject



39
40
41
42
43
44
45
46
47
48
49
50
# 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

.categoryObject



64
65
66
# File 'pantograph/lib/pantograph/actions/download.rb', line 64

def self.category
  :misc
end

.descriptionObject



28
29
30
# File 'pantograph/lib/pantograph/actions/download.rb', line 28

def self.description
  'Download a file from a remote server (e.g. JSON file)'
end

.detailsObject



32
33
34
35
36
37
# 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_codeObject



58
59
60
61
62
# 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) ⇒ Boolean

Returns:



72
73
74
# File 'pantograph/lib/pantograph/actions/download.rb', line 72

def self.is_supported?(platform)
  true
end

.outputObject



52
53
54
55
56
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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