Module: ThreeScaleToolbox::ResourceReader
- Included in:
- Commands::ActiveDocsCommand::Apply::ApplySubcommand, Commands::ActiveDocsCommand::Create::CreateSubcommand, Commands::ImportCommand::OpenAPI::OpenAPISubcommand, Commands::PlansCommand::Import::ImportSubcommand, Commands::PoliciesCommand::ImportSubcommand, Commands::ProductCommand::ImportSubcommand
- Defined in:
- lib/3scale_toolbox/resource_reader.rb
Instance Method Summary collapse
-
#load_resource(resource, verify_ssl) ⇒ Object
Load resource from different types of sources.
-
#read_content(resource, verify_ssl) ⇒ Object
Reads resources from different types of sources.
-
#read_file(filename) ⇒ Object
Detect format from file extension.
- #read_stdin(_resource) ⇒ Object
- #read_stringio(resource) ⇒ Object
- #read_url(resource, verify_ssl) ⇒ Object
Instance Method Details
#load_resource(resource, verify_ssl) ⇒ Object
Load resource from different types of sources. Supported types are: file, URL, stdin Loaded content is returned
7 8 9 10 11 12 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 7 def load_resource(resource, verify_ssl) # Json format is parsed as well YAML.safe_load(read_content(resource, verify_ssl)) rescue Psych::SyntaxError => e raise ThreeScaleToolbox::Error, "JSON/YAML validation failed: #{e.message}" end |
#read_content(resource, verify_ssl) ⇒ Object
Reads resources from different types of sources. Supported types are: file, URL, stdin Resource raw content is returned
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 18 def read_content(resource, verify_ssl) case resource when '-' read_stdin(resource) when /\A#{URI::DEFAULT_PARSER.make_regexp}\z/ read_url(resource, verify_ssl) when StringIO read_stringio(resource) else read_file(resource) end end |
#read_file(filename) ⇒ Object
Detect format from file extension
32 33 34 35 36 37 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 32 def read_file(filename) raise ThreeScaleToolbox::Error, "File not found: #{filename}" unless File.file?(filename) raise ThreeScaleToolbox::Error, "File not readable: #{filename}" unless File.readable?(filename) File.read(filename) end |
#read_stdin(_resource) ⇒ Object
39 40 41 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 39 def read_stdin(_resource) STDIN.read end |
#read_stringio(resource) ⇒ Object
56 57 58 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 56 def read_stringio(resource) resource.string end |
#read_url(resource, verify_ssl) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/3scale_toolbox/resource_reader.rb', line 43 def read_url(resource, verify_ssl) endpoint = URI.parse(resource) http_client = Net::HTTP.new(endpoint.host, endpoint.port) http_client.use_ssl = endpoint.is_a?(URI::HTTPS) http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE unless verify_ssl response = http_client.get(endpoint) case response when Net::HTTPSuccess then response.body else raise ThreeScaleToolbox::Error, "could not download resource: #{response.body}" end end |