From d0e08250724daa5038ea1bf14f93c1456baeac92 Mon Sep 17 00:00:00 2001 From: mmenanno Date: Mon, 19 Feb 2024 00:52:02 -0500 Subject: [PATCH] update rb configs --- scrapers/rb_common/configs/README.md | 30 ++++++++++++-------- scrapers/rb_common/configs/config_base.rb | 12 +------- scrapers/rb_common/configs/stash_config.rb | 12 ++++---- scrapers/rb_common/configs/stashdb_config.rb | 10 +++---- scrapers/rb_common/graphql/stash.rb | 5 ++-- 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/scrapers/rb_common/configs/README.md b/scrapers/rb_common/configs/README.md index 2bdca990b..11cf852fe 100644 --- a/scrapers/rb_common/configs/README.md +++ b/scrapers/rb_common/configs/README.md @@ -2,29 +2,35 @@ ## Adding your settings to an existing config -Various ruby scrapers will use these configs so that each one doesn't separately need to ask for your api keys and endpoints. For example if you want to configure your stash instance you can open the `stash_config.rb` with any text editor and in the `USER_CONFIG`, add your details. For example you might change: +Various ruby scrapers will use these configs so that each one doesn't separately need to ask for your api keys and endpoints. For example if you want to configure your stash instance you can open the `stash_config.rb` with any text editor and in the relevant attributes in the `initialize` method to add your details. For example you might change: ```Ruby -USER_CONFIG = { - endpoint: "http://localhost:9999", - api_key: "" -} +def initialize + @endpoint = "http://localhost:9999" + @api_key = "" +end ``` to (made up endpoint and key for example): ```Ruby -USER_CONFIG = { - endpoint: "http://192.168.0.99:6969", - api_key: "thisIsAFakeAPIKeyTheRealOneIsMuchLonger" -} +def initialize + @endpoint = "http://192.168.0.99:6969" + @api_key = "thisIsAFakeAPIKeyTheRealOneIsMuchLonger" +end ``` ## Calling these configs in your scraper -The config values have been defined as class methods, so calling them in your scraper is as simple as requiring the config file either directly with something like `require_relative "rb_common/configs/stash_config"` or generally by requiring all the common interfaces with something like `require_relative "rb_common/rb_common"`. +Calling them in your scraper is as simple as requiring the config file either directly with something like `require_relative "rb_common/configs/stash_config"` or generally by requiring all the common interfaces with something like `require_relative "rb_common/rb_common"`. -Once required your scrupt can access them via calls like `Config::Stash.api_key` and `Config::Stash.endpoint`. +Once required your scrupt can access them via calls by initializing an instance of the config and calling for the attribute like: + +```Ruby +stash_config = Config::Stash.new +stash_config.api_key +stash_config.endpoint +``` ## Creating a new config @@ -39,4 +45,4 @@ module Config class Stash < ConfigBase ``` -From there the base class expects you to define a `USER_CONFIG` hash. The base class contains `endpoint` and `api_key` methods that look for keys of the same name in the `USER_CONFIG`. If the keys are not present in the `USER_CONFIG`, the methods will just return nil. You are free to ad more than the default two keys to your `USER_CONFIG`, but you will need to define their accessor methods on your child class. +Inheriting from `ConfigBase` will add reader and writer methods for `endpoint` and `api_key` to an instance of your class. It is suggested that you definte an initialize method that sets the defaults for these attributes. If no defaults are defined in your class they will return nil when called. You are free to ad more than the default two attributes to your class, but you will need to define their accessor methods on your child class. diff --git a/scrapers/rb_common/configs/config_base.rb b/scrapers/rb_common/configs/config_base.rb index 22dd21a92..641193c8c 100644 --- a/scrapers/rb_common/configs/config_base.rb +++ b/scrapers/rb_common/configs/config_base.rb @@ -1,15 +1,5 @@ # frozen_string_literal: true class ConfigBase - class << self - def endpoint - return nil unless self::USER_CONFIG[:endpoint] - self::USER_CONFIG[:endpoint] - end - - def api_key - return nil unless self::USER_CONFIG[:api_key] - self::USER_CONFIG[:api_key] - end - end + attr_accessor :endpoint, :api_key end diff --git a/scrapers/rb_common/configs/stash_config.rb b/scrapers/rb_common/configs/stash_config.rb index 3b5a33967..84853a531 100644 --- a/scrapers/rb_common/configs/stash_config.rb +++ b/scrapers/rb_common/configs/stash_config.rb @@ -4,11 +4,11 @@ module Config class Stash < ConfigBase - # Tweak user settings below. An API Key can be generated in Stash's setting page - # ( Settings > Security > Authentication ) - USER_CONFIG = { - endpoint: "http://localhost:9999", - api_key: "" - } + def initialize + # Tweak user settings below. An API Key can be generated in Stash's setting page + # ( Settings > Security > Authentication ) + @endpoint = "http://localhost:9999" + @api_key = "" + end end end diff --git a/scrapers/rb_common/configs/stashdb_config.rb b/scrapers/rb_common/configs/stashdb_config.rb index b07642159..ee251c03d 100644 --- a/scrapers/rb_common/configs/stashdb_config.rb +++ b/scrapers/rb_common/configs/stashdb_config.rb @@ -4,10 +4,10 @@ module Config class StashDB < ConfigBase - # Tweak user settings below. An API Key can be generated in StashDB's user page - USER_CONFIG = { - endpoint: "https://stashdb.org/graphql", - api_key: "" - } + def initialize + # Tweak user settings below. An API Key can be generated in StashDB's user page + @endpoint = "https://stashdb.org/graphql" + @api_key = "" + end end end diff --git a/scrapers/rb_common/graphql/stash.rb b/scrapers/rb_common/graphql/stash.rb index e29120456..3fbe453cc 100644 --- a/scrapers/rb_common/graphql/stash.rb +++ b/scrapers/rb_common/graphql/stash.rb @@ -19,8 +19,9 @@ module GraphQL class Stash < GraphQLBase def initialize(referer: nil) - @api_key = Config::Stash.api_key - @url = Config::Stash.endpoint + "/graphql" + @config = Stash::Config.new + @api_key = @config.api_key + @url = @config.endpoint + "/graphql" @extra_headers = { "ApiKey": @api_key } @extra_headers["Referer"] = referer if referer end