How to use initialize method of Fetchers Package

Best Inspec_ruby code snippet using Fetchers.initialize

fetcher.rb

Source:fetcher.rb Github

copy

Full Screen

...15 # This error is raised if the API returns a 413 (only printed in verbose)16 class FallbackError < HTTPError; end17 # This is the error raised if OpenSSL fails the cert verification18 class CertificateFailureError < HTTPError19 def initialize(remote_uri)20 remote_uri = filter_uri(remote_uri)21 super "Could not verify the SSL certificate for #{remote_uri}.\nThere" \22 " is a chance you are experiencing a man-in-the-middle attack, but" \23 " most likely your system doesn't have the CA certificates needed" \24 " for verification. For information about OpenSSL certificates, see" \25 " http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile" \26 " sources and change 'https' to 'http'."27 end28 end29 # This is the error raised when a source is HTTPS and OpenSSL didn't load30 class SSLError < HTTPError31 def initialize(msg = nil)32 super msg || "Could not load OpenSSL.\n" \33 "You must recompile Ruby with OpenSSL support or change the sources in your " \34 "Gemfile from 'https' to 'http'. Instructions for compiling with OpenSSL " \35 "using RVM are available at rvm.io/packages/openssl."36 end37 end38 # This error is raised if HTTP authentication is required, but not provided.39 class AuthenticationRequiredError < HTTPError40 def initialize(remote_uri)41 remote_uri = filter_uri(remote_uri)42 super "Authentication is required for #{remote_uri}.\n" \43 "Please supply credentials for this source. You can do this by running:\n" \44 " bundle config #{remote_uri} username:password"45 end46 end47 # This error is raised if HTTP authentication is provided, but incorrect.48 class BadAuthenticationError < HTTPError49 def initialize(remote_uri)50 remote_uri = filter_uri(remote_uri)51 super "Bad username or password for #{remote_uri}.\n" \52 "Please double-check your credentials and correct them."53 end54 end55 # Exceptions classes that should bypass retry attempts. If your password didn't work the56 # first time, it's not going to the third time.57 NET_ERRORS = [:HTTPBadGateway, :HTTPBadRequest, :HTTPFailedDependency,58 :HTTPForbidden, :HTTPInsufficientStorage, :HTTPMethodNotAllowed,59 :HTTPMovedPermanently, :HTTPNoContent, :HTTPNotFound,60 :HTTPNotImplemented, :HTTPPreconditionFailed, :HTTPRequestEntityTooLarge,61 :HTTPRequestURITooLong, :HTTPUnauthorized, :HTTPUnprocessableEntity,62 :HTTPUnsupportedMediaType, :HTTPVersionNotSupported].freeze63 FAIL_ERRORS = begin64 fail_errors = [AuthenticationRequiredError, BadAuthenticationError, FallbackError]65 fail_errors << Gem::Requirement::BadRequirementError if defined?(Gem::Requirement::BadRequirementError)66 fail_errors.concat(NET_ERRORS.map {|e| SharedHelpers.const_get_safely(e, Net) }.compact)67 end.freeze68 class << self69 attr_accessor :disable_endpoint, :api_timeout, :redirect_limit, :max_retries70 end71 self.redirect_limit = Bundler.settings[:redirect] # How many redirects to allow in one request72 self.api_timeout = Bundler.settings[:timeout] # How long to wait for each API call73 self.max_retries = Bundler.settings[:retry] # How many retries for the API call74 def initialize(remote)75 @remote = remote76 Socket.do_not_reverse_lookup = true77 connection # create persistent connection78 end79 def uri80 @remote.anonymized_uri81 end82 # fetch a gem specification83 def fetch_spec(spec)84 spec -= [nil, "ruby", ""]85 spec_file_name = "#{spec.join "-"}.gemspec"86 uri = URI.parse("#{remote_uri}#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}.rz")87 if uri.scheme == "file"88 Bundler.load_marshal Gem.inflate(Gem.read_binary(uri.path))...

Full Screen

Full Screen

launcher.rb

Source:launcher.rb Github

copy

Full Screen

...8require File.dirname(__FILE__)+"/launchable_fetcher"9class Launcher10 attr_reader :configuration, :i18n_version, :logger, :env, :force_threaded_framework11 attr_accessor :import_queue, :fetching_completed, :threads12 def initialize(i18n_version = "us", force_threaded_framework = false)13 @i18n_version = i18n_version14 @fetcher_queue = Array.new15 @running_queue = Array.new16 Dir.mkdir File.join(File.dirname(__FILE__),"..", "log") unless File.exists? File.join(File.dirname(__FILE__),"..", "log")17 @logger = Logger.new(File.join(File.dirname(__FILE__),"..", "log", "launcher.log"))18 @logger.formatter = SeverityFormatter.new19 @threads = []20 @env = ENV['RAILS_ENV']21 @force_threaded_framework = force_threaded_framework22 @import_queue = Queue.new23 @fetching_completed = false24 load_configuration25 prepare_fetchers26 end27 def load_configuration28 config = load_configuration_from_file(File.join(File.dirname(__FILE__),"..", "config", "launcher.yml"))29 if env.nil?30 @configuration = config[:default] || {}31 else32 @configuration = (config[:default] || {}).merge(config[env.to_sym] || {})33 end34 @configuration.merge!(load_configuration_from_file(File.join(File.dirname(__FILE__),"..", "config", "launcher.#{self.i18n_version}.yml")))35 end36 def prepare_fetchers37 if @force_threaded_framework38 @configuration[:fetchers].each do |fetcher|39 fetcher[:threaded] = true40 end41 end42 end43 def load_configuration_from_file(file)44 YAML.load(ERB.new(IO.read(file)).result(binding))45 end46 def run47 self.initialize_fetcher_queue48 initialize_import_thread49 while self.fetcher_queue.any? || self.running_queue.any?50 launch_fetcher51 remove_fetchers :expired52 remove_fetchers :completed53 sleep 154 end55 ## All fetchers are pushed onto import queue by this time. Solves race condition.56 @fetching_completed = true57 58 self.logger.info "joining threads"59 @threads.each{|t| t.join}60 end61 def initialize_import_thread62 num_import_threads.times do |i|63 @threads << Thread.new do64 run_import_thread i65 end66 end67 end68 def remove_fetchers(expired_or_completed)69 raise ArgumentError unless [:expired, :completed].include?(expired_or_completed)70 of_interest = @running_queue.select{|x| x.send("#{expired_or_completed}?")}71 of_interest.each do |fetcher|72 @running_queue.delete(fetcher)73 fetcher.after_expired if :expired == expired_or_completed74 75 @import_queue << fetcher76 end77 end78 def pop_next_fetcher!79 next_fetcher_index = nil80 self.fetcher_queue.each_with_index do |fetcher,index|81 if fetcher && fetcher.runnable?82 next_fetcher_index = index83 break84 end85 end86 next_fetcher = next_fetcher_index ? self.fetcher_queue.delete_at(next_fetcher_index) : nil87 next_fetcher88 end89 def launch_fetcher90 if self.running_queue.length < self.concurrent_running_limit91 if fetcher = self.pop_next_fetcher!92 self.running_queue << fetcher93 @threads << Thread.new do94 fetcher.launch95 end96 end97 end98 end99 def initialize_fetcher_queue100 prioritized_fetchers = self.fetchers.select{|x| x.has_key?(:priority)}.sort{|x,y| x[:priority] <=> y[:priority]}.reverse101 non_prioritized_fetchers = self.fetchers.select{|x| !x.has_key?(:priority)}102 all_fetchers = prioritized_fetchers | non_prioritized_fetchers103 @fetcher_queue = all_fetchers.map{|x| LaunchableFetcher.new(x.merge({:launcher => self}))}104 end105 def run_import_thread(thread_number)106 self.logger.info "Starting import thread #{thread_number}"107 until fetching_completed? && @import_queue.empty?108 import_a_fetcher109 sleep 1110 end111 112 self.logger.info "Terminating import thread"113 end...

Full Screen

Full Screen

fetches.rb

Source:fetches.rb Github

copy

Full Screen

...3 def self.included(base) # :nodoc:4 base.class_eval do5 @@default_fetcher_options = { :using => "find",6 :from => :id,7 :initialize => false }8 cattr_accessor :default_fetcher_options9 10 def self.fetchers # :nodoc:11 read_inheritable_attribute(:fetchers) || write_inheritable_attribute(:fetchers, {})12 end13 14 def self.initializing_fetchers # :nodoc:15 read_inheritable_attribute(:initializing_fetchers) || write_inheritable_attribute(:initializing_fetchers, {})16 end17 # Automatically creates a helper method to fetch a memoized record based on18 # a passed-in parameter.19 #20 # Usage:21 #22 # class UsersController < ApplicationController23 # fetches :user24 # end25 #26 # # Advanced example with nested route such as /users/:user_id/articles27 # class ArticlesController < ApplicationController28 # fetches :user, :as => :author, :from => :user_id, :using => :find_by_login29 # fetches :article, :initialize => true30 # end31 #32 # # Example with Proc-based 'from'33 # class UsersController < ApplicationController34 # fetches :user, :from => Proc.new{ |c| c.params[:user_id] || c.params[:id] }35 # end36 #37 # Options:38 #39 # - +as+: the name of the helper method to generate (default is the model name)40 # - +from+: the parameter passed into the finder method (default is +:id+). May also be passed as a Proc that evaluates against a controller argument.41 # - +using+: the class method name to use as a finder (default is +"find"+)42 # - +initialize+: optionally initialize a new record using parameters if one isn't found. If set to true, initializes from +params[:model_name]+, if false, won't initialize. May also be set to a parameter key or Proc. Default is +false+.43 #44 # Default options may be specified by setting them in an initializer. Example:45 #46 # ActionController::Base.default_fetcher_options[:using] = "find_by_id"47 def self.fetches(model_name, options = {})48 method_name = options.delete(:as) || default_fetcher_options[:as] || model_name.to_s49 finder = options.delete(:using) || default_fetcher_options[:using]50 from = options.delete(:from) || default_fetcher_options[:from]51 initializing = options.delete(:initialize) || default_fetcher_options[:initialize]52 initializing = model_name if initializing == true53 klass = self.respond_to?(:class_eval) ? self : self.metaclass54 fetchers[method_name.to_sym] = from55 initializing_fetchers[method_name.to_sym] = initializing56 57 klass.class_eval <<-EOS, __FILE__, __LINE__58 def #{method_name}59 if defined?(@#{method_name})60 @#{method_name}61 else62 fetcher = self.class.fetchers[:#{method_name}]63 from = fetcher.is_a?(Proc) ? fetcher.call(self) : params[fetcher]64 initializer = self.class.initializing_fetchers[:#{method_name}]65 @#{method_name} = #{model_name.to_s.classify}.#{finder.to_s}(from) if from66 @#{method_name} ||= #{model_name.to_s.classify}.new(initializer.is_a?(Proc) ? initializer.call(self) : params[initializer]) 67 end68 end69 EOS70 helper_method method_name71 end72 end73 end74 end75end...

Full Screen

Full Screen

initialize

Using AI Code Generation

copy

Full Screen

1fetcher.fetch("http://www.google.com")2fetcher.fetch("http://www.google.com")3fetcher.fetch("http://www.google.com")4fetcher.fetch("http://www.google.com")5fetcher.fetch("http://www.google.com")6fetcher.fetch("http://www.google.com")7fetcher.fetch("http://www.google.com")8fetcher.fetch("http://www.google.com")9fetcher.fetch("http://www.google.com")10fetcher.fetch("http://www.google.com")11fetcher.fetch("http://www.google.com")12fetcher.fetch("http://www.google.com")13fetcher.fetch("http://www.google.com")14fetcher.fetch("http://www.google.com")

Full Screen

Full Screen

initialize

Using AI Code Generation

copy

Full Screen

1fetchers.fetch("http://www.google.com")2fetchers.fetch("http://www.yahoo.com")3fetchers.fetch("http://www.abcnews.com")4fetchers.fetch("http://www.google.com")5fetchers.fetch("http://www.yahoo.com")6fetchers.fetch("http://www.abcnews.com")7fetchers.fetch("http://www.google.com")8fetchers.fetch("http://www.yahoo.com")9fetchers.fetch("http://www.abcnews.com")10fetchers.fetch("http://www.google.com")11fetchers.fetch("http://www.yahoo.com")12fetchers.fetch("http://www.abcnews.com")13fetchers.fetch("http://www.google.com")14fetchers.fetch("http://www.yahoo.com")15fetchers.fetch("http://www.abcnews.com")16fetchers.fetch("http://www.google.com")17fetchers.fetch("http://www.yahoo.com")18fetchers.fetch("http://www.abcnews.com")19fetchers.fetch("http://www.google.com")20fetchers.fetch("http://www.yahoo.com")21fetchers.fetch("http://www.abcnews.com")22fetchers.fetch("http://www.google.com")23fetchers.fetch("http://www.yahoo.com")24fetchers.fetch("http://www.abcnews.com")25fetchers.fetch("http://www.google.com")26fetchers.fetch("http://www.yahoo.com")27fetchers.fetch("http://www.abcnews.com")

Full Screen

Full Screen

initialize

Using AI Code Generation

copy

Full Screen

1fetcher.get("http://www.google.com", "google.html")2fetcher.get("http://www.yahoo.com", "yahoo.html")3fetcher = Fetchers.new("http://www.google.com", "google.html")4fetcher = Fetchers.new("http://www.google.com", "google.html")5fetcher.get("http://www.yahoo.com", "yahoo.html")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful