Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rb configs #1657

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions scrapers/rb_common/configs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
12 changes: 1 addition & 11 deletions scrapers/rb_common/configs/config_base.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions scrapers/rb_common/configs/stash_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions scrapers/rb_common/configs/stashdb_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions scrapers/rb_common/graphql/stash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading