serpcheap-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b835b2a8d37f97ccae48b83a9a749a0b3f1a02dabbe4884a2d3c039ae24682cf
4
+ data.tar.gz: 98a3fc890644c89df7d3a3ba3b2ace65911162504adb68a6d322ee8ecd29276f
5
+ SHA512:
6
+ metadata.gz: fafabef66a5cd3146bdcedced78199d19224be680b9912b667087c1ca71a7acf1f7d56a614b2619b71ed7d509095bfe4259e8559297b51bc1e6f1a9183e8130c
7
+ data.tar.gz: 4a7b84a516a5805486737c9fb9e98d3c2b29ed7be1fbe1065ec56b1b7d75cb82296c3ed50c6ed0c9204ec988f30876778dce113da58e133c262404d21e9635b3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) serp.cheap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # serpcheap-rails
2
+
3
+ Rails integration for the [serp.cheap](https://serp.cheap) **Google Search API** — real-time Google SERP data (organic results, ads, knowledge graph, page scraping, rank tracking) wrapped with a Railtie, `Rails.cache` result caching, credentials/ENV config, and an ActiveJob.
4
+
5
+ It's the **cheapest Google Search API** we know of — $0.0003 per cached search, $0.0006 fresh, no monthly minimum (~10× cheaper than SerpApi).
6
+
7
+ ## Install
8
+
9
+ ```ruby
10
+ # Gemfile
11
+ gem "serpcheap-rails"
12
+ ```
13
+
14
+ Set your API key (get one at [app.serp.cheap](https://app.serp.cheap)) via `SERPCHEAP_API_KEY`, Rails credentials, or an initializer:
15
+
16
+ ```ruby
17
+ # config/initializers/serpcheap.rb
18
+ SerpCheap::Rails.configure do |c|
19
+ c.api_key = Rails.application.credentials.serpcheap_api_key
20
+ # c.cache_ttl = 3600
21
+ # c.cache_enabled = true
22
+ end
23
+ ```
24
+
25
+ You can also configure it the Rails way in any environment file:
26
+
27
+ ```ruby
28
+ config.serpcheap.api_key = ENV["SERPCHEAP_API_KEY"]
29
+ config.serpcheap.cache_ttl = 1.hour
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```ruby
35
+ results = SerpCheap::Rails.search("best running shoes", gl: "us")
36
+ page = SerpCheap::Rails.scrape("https://example.com")
37
+ rank = SerpCheap::Rails.rank("example.com", "best running shoes")
38
+ ```
39
+
40
+ Results are cached in `Rails.cache` by default (keyed by query + options) so repeat calls don't spend credits. Reach the raw, uncached SDK client when you need it:
41
+
42
+ ```ruby
43
+ SerpCheap::Rails.raw.search_pages("best running shoes", from: 1, to: 3)
44
+ ```
45
+
46
+ ### Background cache warming
47
+
48
+ Pre-warm the cache off the request cycle with the bundled ActiveJob:
49
+
50
+ ```ruby
51
+ SerpCheap::Rails::SearchJob.perform_later("best running shoes", "gl" => "br")
52
+ ```
53
+
54
+ ## Configuration
55
+
56
+ | Setting | Default | Description |
57
+ | --- | --- | --- |
58
+ | `api_key` | `ENV["SERPCHEAP_API_KEY"]` | API key (required) |
59
+ | `base_url` | `https://api.serp.cheap` | API base URL |
60
+ | `timeout_ms` | `15000` | Per-request timeout |
61
+ | `max_retries` | `2` | Retry count on transient errors |
62
+ | `cache_enabled` | `true` | Toggle `Rails.cache` result caching |
63
+ | `cache_store` | `Rails.cache` | Any `ActiveSupport::Cache::Store` |
64
+ | `cache_ttl` | `3600` | Cache TTL in seconds |
65
+
66
+ ## License
67
+
68
+ MIT
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "json"
5
+
6
+ module SerpCheap
7
+ module Rails
8
+ class CachedClient
9
+ def initialize(sdk, configuration)
10
+ @sdk = sdk
11
+ @configuration = configuration
12
+ end
13
+
14
+ def raw
15
+ @sdk
16
+ end
17
+
18
+ def search(query, **opts)
19
+ fetch("search", [query, opts]) { @sdk.search(query, **opts) }
20
+ end
21
+
22
+ def scrape(url, **opts)
23
+ fetch("scrape", [url, opts]) { @sdk.scrape(url, **opts) }
24
+ end
25
+
26
+ def rank(url, query, **opts)
27
+ fetch("rank", [url, query, opts]) { @sdk.rank(url, query, **opts) }
28
+ end
29
+
30
+ private
31
+
32
+ def fetch(kind, key_parts)
33
+ store = @configuration.cache_store
34
+ return yield unless @configuration.cache_enabled && store
35
+
36
+ key = "serpcheap:#{kind}:#{Digest::MD5.hexdigest(JSON.generate(normalize(key_parts)))}"
37
+ store.fetch(key, expires_in: @configuration.cache_ttl) { yield }
38
+ end
39
+
40
+ def normalize(value)
41
+ case value
42
+ when Hash then value.sort_by { |k, _| k.to_s }.to_h.transform_values { |v| normalize(v) }
43
+ when Array then value.map { |v| normalize(v) }
44
+ else value
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SerpCheap
4
+ module Rails
5
+ class Configuration
6
+ attr_accessor :api_key, :base_url, :timeout_ms, :max_retries,
7
+ :cache_enabled, :cache_ttl, :cache_store
8
+
9
+ def initialize
10
+ @api_key = ENV.fetch("SERPCHEAP_API_KEY", nil)
11
+ @base_url = ENV.fetch("SERPCHEAP_BASE_URL", nil)
12
+ @timeout_ms = 15_000
13
+ @max_retries = 2
14
+ @cache_enabled = true
15
+ @cache_ttl = 3600
16
+ @cache_store = nil
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+ require "active_support/ordered_options"
5
+
6
+ module SerpCheap
7
+ module Rails
8
+ class Railtie < ::Rails::Railtie
9
+ config.serpcheap = ActiveSupport::OrderedOptions.new
10
+
11
+ initializer "serpcheap.configure" do |app|
12
+ SerpCheap::Rails.apply_options(app.config.serpcheap)
13
+ end
14
+
15
+ config.after_initialize do
16
+ SerpCheap::Rails.configuration.cache_store ||= ::Rails.cache
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_job"
4
+
5
+ module SerpCheap
6
+ module Rails
7
+ # Warms the result cache off the request cycle:
8
+ # SerpCheap::Rails::SearchJob.perform_later("best running shoes", "gl" => "br")
9
+ class SearchJob < ActiveJob::Base
10
+ def perform(query, opts = {})
11
+ SerpCheap::Rails.search(query, **symbolize(opts))
12
+ end
13
+
14
+ private
15
+
16
+ def symbolize(opts)
17
+ opts.each_with_object({}) { |(k, v), out| out[k.to_sym] = v }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SerpCheap
4
+ module Rails
5
+ VERSION = "0.1.0" # x-release-please-version
6
+ end
7
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "serpcheap"
4
+
5
+ require_relative "rails/version"
6
+ require_relative "rails/configuration"
7
+ require_relative "rails/cached_client"
8
+
9
+ module SerpCheap
10
+ # Rails integration: a Railtie wires config + Rails.cache; SerpCheap::Rails
11
+ # exposes a cached search/scrape/rank facade and a raw client escape hatch.
12
+ module Rails
13
+ class << self
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(configuration) if block_given?
20
+ reset_client!
21
+ configuration
22
+ end
23
+
24
+ def apply_options(options)
25
+ c = configuration
26
+ c.api_key = options[:api_key] unless options[:api_key].nil?
27
+ c.base_url = options[:base_url] unless options[:base_url].nil?
28
+ c.timeout_ms = options[:timeout_ms] unless options[:timeout_ms].nil?
29
+ c.max_retries = options[:max_retries] unless options[:max_retries].nil?
30
+ c.cache_store = options[:cache_store] unless options[:cache_store].nil?
31
+ c.cache_enabled = options[:cache_enabled] unless options[:cache_enabled].nil?
32
+ c.cache_ttl = options[:cache_ttl] unless options[:cache_ttl].nil?
33
+ reset_client!
34
+ c
35
+ end
36
+
37
+ def reset_client!
38
+ @client = nil
39
+ end
40
+
41
+ def client
42
+ @client ||= build_client
43
+ end
44
+
45
+ def raw
46
+ client.raw
47
+ end
48
+
49
+ def search(query, **opts)
50
+ client.search(query, **opts)
51
+ end
52
+
53
+ def scrape(url, **opts)
54
+ client.scrape(url, **opts)
55
+ end
56
+
57
+ def rank(url, query, **opts)
58
+ client.rank(url, query, **opts)
59
+ end
60
+
61
+ private
62
+
63
+ def build_client
64
+ c = configuration
65
+ if c.api_key.nil? || c.api_key.empty?
66
+ raise SerpCheap::Error.new(
67
+ "missing_api_key",
68
+ "Set SerpCheap::Rails api_key (or SERPCHEAP_API_KEY). Get a key at https://app.serp.cheap."
69
+ )
70
+ end
71
+ sdk = SerpCheap::Client.new(
72
+ c.api_key,
73
+ base_url: c.base_url,
74
+ timeout_ms: c.timeout_ms,
75
+ max_retries: c.max_retries
76
+ )
77
+ CachedClient.new(sdk, c)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ require_relative "rails/railtie"
84
+ require_relative "rails/search_job"
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serpcheap-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - serp.cheap
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: serpcheap
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.2'
69
+ description: Railtie, cached client (Rails.cache), credentials/ENV config, and an
70
+ ActiveJob for the serp.cheap SERP API.
71
+ email:
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - README.md
78
+ - lib/serpcheap/rails.rb
79
+ - lib/serpcheap/rails/cached_client.rb
80
+ - lib/serpcheap/rails/configuration.rb
81
+ - lib/serpcheap/rails/railtie.rb
82
+ - lib/serpcheap/rails/search_job.rb
83
+ - lib/serpcheap/rails/version.rb
84
+ homepage: https://serp.cheap
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ homepage_uri: https://serp.cheap
89
+ source_code_uri: https://github.com/SerpCheap/serpcheap-rails
90
+ documentation_uri: https://serp.cheap/docs
91
+ rubygems_mfa_required: 'true'
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.7.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.5.16
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Rails integration for the serp.cheap Google Search API.
111
+ test_files: []