datacircle-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 +7 -0
- data/LICENSE +21 -0
- data/README.md +83 -0
- data/lib/datacircle/client.rb +143 -0
- data/lib/datacircle/engine.rb +22 -0
- data/lib/datacircle/helper.rb +66 -0
- data/lib/datacircle/rails.rb +18 -0
- data/lib/datacircle/tasks.rb +105 -0
- data/lib/datacircle/version.rb +5 -0
- data/lib/datacircle-rails.rb +3 -0
- data/lib/generators/datacircle/install/install_generator.rb +17 -0
- data/lib/generators/datacircle/install/templates/datacircle.rb +16 -0
- data/lib/tasks/datacircle.rake +26 -0
- metadata +82 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d88284ec90e0040844938b34c869039ab668b949f9a368ab8c327764244c99ad
|
|
4
|
+
data.tar.gz: 4a2ccfe9323c60255c30efb2aafb3a6603f35abd4474bc4f322dbbb63b6d6607
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1ce34f197b2617a1ab812b75abaf1c1cd802f8d7ee845804152147c74b99ab38c92596cbf31190ac772b49987844a09cdf6ad3f275491663eecab97f8bc24304
|
|
7
|
+
data.tar.gz: b5dc5a98736e754e31e8bc406897244a9c15e1272f4624a098de414e6b815e138395faa1cb96cfb95b3b7a60bae6316107244144e206f9d7b5e636ee352ea9d7
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Datacircle
|
|
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,83 @@
|
|
|
1
|
+
# Datacircle for Rails
|
|
2
|
+
|
|
3
|
+
Find the best GTM APIs. We benchmark all the GTM APIs and we put the results on our website: [datacircle.dev](https://datacircle.dev).
|
|
4
|
+
|
|
5
|
+
This Rails engine puts our results in your Rails app. The data comes from our open API at https://api.datacircle.dev/v1. You need no key and no account.
|
|
6
|
+
|
|
7
|
+
| Your app gets | Call |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| `Datacircle.client.apis` and `bin/rails datacircle:apis` | `GET /apis` |
|
|
10
|
+
| `Datacircle.client.best`, `bin/rails datacircle:best` and `datacircle_best_table` | `GET /apis/{slug}/best` |
|
|
11
|
+
| `Datacircle.client.plans` and `bin/rails datacircle:plans` | `GET /plans` |
|
|
12
|
+
| `Datacircle.client.provider` and `bin/rails datacircle:provider` | `GET /providers/{slug}` |
|
|
13
|
+
|
|
14
|
+
The engine works with Rails 7.2 and 8.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
bundle add datacircle-rails
|
|
20
|
+
bin/rails generate datacircle:install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The generator adds `config/initializers/datacircle.rb` with the default settings. You can delete `config/initializers/datacircle.rb`. The engine still runs.
|
|
24
|
+
|
|
25
|
+
## Rake tasks
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
bin/rails datacircle:apis
|
|
29
|
+
bin/rails datacircle:best API=email-finder LIMIT=5
|
|
30
|
+
bin/rails datacircle:plans QUERY=hunter API=email-finder SORT=price LIMIT=5 OFFSET=0
|
|
31
|
+
bin/rails datacircle:provider PROVIDER=bright-data BEST=1
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Each task prints a table. Add `FORMAT=json` to print JSON instead of a table. You can also pass the values in Rake's brackets: `bin/rails "datacircle:best[email-finder,5]"`.
|
|
35
|
+
|
|
36
|
+
`datacircle:apis` shows the slug of each API. `SORT` takes `score`, `provider` or `price`. `BEST=1` adds the provider's best plan on each API.
|
|
37
|
+
|
|
38
|
+
## View helpers
|
|
39
|
+
|
|
40
|
+
```erb
|
|
41
|
+
<%= datacircle_best_table "email-finder", limit: 5, class: "table" %>
|
|
42
|
+
<%= datacircle_credit %>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`datacircle_best_table` shows the best plans for one API: rank, provider, plan, score and price per 1,000. Each provider links to the plan's page on datacircle.dev. The caption links to the page of best plans for the API. A wrong slug raises `Datacircle::ApiError`. With the Datacircle API down, the page shows no table. The log gives the reason.
|
|
46
|
+
|
|
47
|
+
`datacircle_credit` adds a line that links to datacircle.dev. Our data is under CC BY 4.0. Keep a link to datacircle.dev near our data on your page.
|
|
48
|
+
|
|
49
|
+
`datacircle_money(7.98)` gives `"$7.98"`. The tables show prices in this format.
|
|
50
|
+
|
|
51
|
+
## The client
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
datacircle = Datacircle.client
|
|
55
|
+
datacircle.apis # GET /apis
|
|
56
|
+
datacircle.best("email-finder", limit: 5) # GET /apis/email-finder/best?limit=5
|
|
57
|
+
datacircle.plans(api: "email-finder", query: "hunter", sort: "price", limit: 5, offset: 0)
|
|
58
|
+
datacircle.provider("Bright Data", plans: "best") # GET /providers/bright-data?plans=best
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Each method returns the API's JSON as a Hash or an Array. In a view, `datacircle` gives the same client. Outside Rails, use `Datacircle::Client.new`.
|
|
62
|
+
|
|
63
|
+
A 4xx raises `Datacircle::ApiError` with the API's own words. `error.status` is the HTTP status. `error.body` is the whole answer. The answer to a wrong slug lists every slug. Any other failure raises `Datacircle::Error`. After a 429, the client waits for the time in `Retry-After`, up to one minute. Then the client calls once more. The API takes 60 calls a minute from one address.
|
|
64
|
+
|
|
65
|
+
The client asks for gzip, as some answers pass 1 MB.
|
|
66
|
+
|
|
67
|
+
## Settings
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
# config/initializers/datacircle.rb
|
|
71
|
+
Rails.application.configure do
|
|
72
|
+
config.datacircle.base_url = "https://api.datacircle.dev/v1"
|
|
73
|
+
config.datacircle.timeout = 30 # seconds a call may take
|
|
74
|
+
config.datacircle.expires_in = 1.hour # how long the cache keeps an answer
|
|
75
|
+
config.datacircle.cache_store = nil # Rails.cache when nil, no cache when false
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Answers stay in `Rails.cache` for an hour. In development, Rails turns the cache off until you run `bin/rails dev:cache`.
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
The code is under the MIT license. The data from the API is under CC BY 4.0. Find the best GTM APIs at [datacircle.dev](https://datacircle.dev).
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Datacircle
|
|
8
|
+
# A call that failed: the API could not be reached, or it answered something other than 200 or a 4xx.
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
|
|
11
|
+
# The API answered a 4xx: its own words are the message, its whole answer is body (a wrong slug's body lists every slug).
|
|
12
|
+
class ApiError < Error
|
|
13
|
+
attr_reader :status, :body
|
|
14
|
+
|
|
15
|
+
def initialize(status, message, body = {})
|
|
16
|
+
@status = status
|
|
17
|
+
@body = body
|
|
18
|
+
super(message)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Calls Datacircle's open API, https://api.datacircle.dev/v1 (its spec: https://api.datacircle.dev/v1/openapi.json).
|
|
23
|
+
# It needs no key and no account. Each answer is kept in cache (a Rails cache store) for expires_in seconds.
|
|
24
|
+
# More at https://datacircle.dev
|
|
25
|
+
class Client
|
|
26
|
+
BASE_URL = "https://api.datacircle.dev/v1"
|
|
27
|
+
|
|
28
|
+
attr_reader :base_url, :timeout, :cache, :expires_in
|
|
29
|
+
|
|
30
|
+
# base_url: where the client calls, BASE_URL when left out. timeout: seconds a call may take.
|
|
31
|
+
# cache: an ActiveSupport::Cache::Store, or nil to call the API each time. expires_in: seconds an answer is kept.
|
|
32
|
+
def initialize(base_url: nil, timeout: 30, cache: nil, expires_in: 3600, logger: nil)
|
|
33
|
+
@base_url = (base_url.nil? || base_url.to_s.strip.empty? ? BASE_URL : base_url.to_s.strip).chomp("/")
|
|
34
|
+
@timeout = timeout
|
|
35
|
+
@cache = cache
|
|
36
|
+
@expires_in = expires_in
|
|
37
|
+
@logger = logger
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# GET /apis: every API on the sheet, in its order, with its slug, how many plans and providers it has,
|
|
41
|
+
# its best plan and its pages.
|
|
42
|
+
def apis
|
|
43
|
+
get("/apis")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# GET /apis/{slug}/best: one API's plans, best first by score, each with its rank.
|
|
47
|
+
# api is its slug as apis gives it (email-finder) or its name. limit: 1 to 50, 10 when left out.
|
|
48
|
+
def best(api, limit: nil)
|
|
49
|
+
get("/apis/#{segment(need(api, "an API's slug"))}/best", "limit" => limit)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# GET /plans: every plan, or an API's (its name or slug), or those whose provider or plan name holds every word
|
|
53
|
+
# of query. sort: score (the default), provider or price. limit: 1 to 50, 10 when left out. offset: how many to skip.
|
|
54
|
+
def plans(api: nil, query: nil, sort: nil, limit: nil, offset: nil)
|
|
55
|
+
get("/plans", "api" => api, "query" => query, "sort" => sort, "limit" => limit, "offset" => offset)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# GET /providers/{slug}: the APIs one provider sells and its rank on each. provider is its slug (bright-data) or
|
|
59
|
+
# its name (Bright Data). plans: all (the default), best (its best plan on each API) or none.
|
|
60
|
+
def provider(provider, plans: nil)
|
|
61
|
+
get("/providers/#{segment(self.class.slugify(need(provider, "a provider's slug or name")))}", "plans" => plans)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Calls one path and gives its JSON. Parameters left nil or empty are not sent. A 429 is waited for once,
|
|
65
|
+
# as long as its Retry-After says (a minute at most).
|
|
66
|
+
def get(path, params = {})
|
|
67
|
+
query = params.reject { |_, v| v.nil? || v.to_s.strip.empty? }.map { |k, v| [k.to_s, v.to_s.strip] }
|
|
68
|
+
url = base_url + path + (query.empty? ? "" : "?#{URI.encode_www_form(query)}")
|
|
69
|
+
return fetch(url) if cache.nil? || !expires_in
|
|
70
|
+
|
|
71
|
+
cache.fetch("datacircle/#{url}", expires_in: expires_in) { fetch(url) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The API's rule for a provider's slug: its name in lowercase letters and digits, the rest a hyphen.
|
|
75
|
+
def self.slugify(name)
|
|
76
|
+
ascii = name.to_s.strip.unicode_normalize(:nfkd).encode("ASCII", invalid: :replace, undef: :replace, replace: "")
|
|
77
|
+
ascii.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def need(value, what)
|
|
83
|
+
raise ArgumentError, "Datacircle: give #{what}" if value.nil? || value.to_s.strip.empty?
|
|
84
|
+
|
|
85
|
+
value.to_s.strip
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def segment(value)
|
|
89
|
+
URI.encode_www_form_component(value).gsub("+", "%20")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def fetch(url)
|
|
93
|
+
2.times do |try|
|
|
94
|
+
response = call(url)
|
|
95
|
+
code = response.code.to_i
|
|
96
|
+
if code == 429 && try.zero?
|
|
97
|
+
after = response["Retry-After"].to_s
|
|
98
|
+
wait = after.match?(/\A\d+\z/) && after.to_i.between?(1, 60) ? after.to_i : 60
|
|
99
|
+
@logger&.warn("Datacircle: #{url} answered 429, calling again in #{wait} s")
|
|
100
|
+
pause(wait)
|
|
101
|
+
next
|
|
102
|
+
end
|
|
103
|
+
return parse(url, response) if code == 200
|
|
104
|
+
raise ApiError.new(code, *words(response)) if code.between?(400, 499)
|
|
105
|
+
|
|
106
|
+
raise Error, "Datacircle: #{url} answered #{code}"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Net::HTTP asks for gzip by itself (Accept-Encoding) and unpacks the answer: a provider's whole answer passes 1 MB.
|
|
111
|
+
def call(url)
|
|
112
|
+
uri = URI(url)
|
|
113
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: timeout, read_timeout: timeout) do |http|
|
|
114
|
+
request = Net::HTTP::Get.new(uri)
|
|
115
|
+
request["Accept"] = "application/json"
|
|
116
|
+
request["User-Agent"] = "datacircle-rails/#{VERSION} (+https://datacircle.dev)"
|
|
117
|
+
http.request(request)
|
|
118
|
+
end
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
raise Error, "Datacircle: #{url} failed: #{e.class}: #{e.message}"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def pause(seconds)
|
|
124
|
+
sleep seconds
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def parse(url, response)
|
|
128
|
+
JSON.parse(response.body.to_s)
|
|
129
|
+
rescue JSON::ParserError => e
|
|
130
|
+
raise Error, "Datacircle: #{url} answered something other than JSON: #{e.message[0, 200]}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def words(response)
|
|
134
|
+
body = JSON.parse(response.body.to_s)
|
|
135
|
+
error = body["error"] if body.is_a?(Hash)
|
|
136
|
+
return [error.to_s, body] if error.is_a?(String) && !error.empty?
|
|
137
|
+
|
|
138
|
+
["#{response.code} #{response.message}", body.is_a?(Hash) ? body : {}]
|
|
139
|
+
rescue JSON::ParserError
|
|
140
|
+
["#{response.code} #{response.message}", {}]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
|
|
5
|
+
module Datacircle
|
|
6
|
+
# Adds config.datacircle, the view helpers (Datacircle::Helper) and the datacircle:* rake tasks to the app.
|
|
7
|
+
class Engine < ::Rails::Engine
|
|
8
|
+
config.datacircle = ActiveSupport::OrderedOptions.new
|
|
9
|
+
# Where the client calls.
|
|
10
|
+
config.datacircle.base_url = Client::BASE_URL
|
|
11
|
+
# Seconds a call may take.
|
|
12
|
+
config.datacircle.timeout = 30
|
|
13
|
+
# The cache store answers are kept in: Rails.cache when nil, none when false.
|
|
14
|
+
config.datacircle.cache_store = nil
|
|
15
|
+
# How long an answer is kept.
|
|
16
|
+
config.datacircle.expires_in = 3600
|
|
17
|
+
|
|
18
|
+
initializer "datacircle.helper" do
|
|
19
|
+
ActiveSupport.on_load(:action_view) { include Datacircle::Helper }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Datacircle
|
|
4
|
+
HOME = "https://datacircle.dev"
|
|
5
|
+
|
|
6
|
+
# $ per 1,000 results: "$7.98", "$0.0166", "$0", or "–" when unknown.
|
|
7
|
+
def self.money(value)
|
|
8
|
+
return "–" if value.nil? || value.to_s.strip.empty?
|
|
9
|
+
|
|
10
|
+
n = Float(value)
|
|
11
|
+
return "$0" if n.zero?
|
|
12
|
+
return format("$%.4f", n) if n < 0.1
|
|
13
|
+
|
|
14
|
+
whole, part = format("%.2f", n).split(".")
|
|
15
|
+
"$#{whole.reverse.scan(/\d{1,3}/).join(",").reverse}.#{part}"
|
|
16
|
+
rescue ArgumentError, TypeError
|
|
17
|
+
"–"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The view helpers, in every view of the app. The data is under CC BY 4.0: each table links its page on datacircle.dev.
|
|
21
|
+
module Helper
|
|
22
|
+
# The client, set up by config.datacircle: <% datacircle.plans(api: "email-finder", sort: "price")["plans"] %>
|
|
23
|
+
def datacircle
|
|
24
|
+
Datacircle.client
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A table of one API's best plans, best first: rank, provider (linked to the plan's page), plan, score, $ per 1,000.
|
|
28
|
+
# api is its slug (email-finder) or its name; limit is 1 to 50. Options go on the table (class:, id:, data:).
|
|
29
|
+
# A wrong slug raises Datacircle::ApiError; when the API can't be reached the table is left out and the log says why.
|
|
30
|
+
def datacircle_best_table(api, limit: 10, **options)
|
|
31
|
+
answer = datacircle.best(api, limit: limit)
|
|
32
|
+
link = answer["bestPage"] || answer["sheetPage"] || HOME
|
|
33
|
+
options[:class] = ["datacircle", "datacircle-best", *options[:class]].join(" ")
|
|
34
|
+
head = ["Rank", "Provider", "Plan", "Score", "$ per 1,000"]
|
|
35
|
+
rows = Array(answer["plans"]).map do |plan|
|
|
36
|
+
provider = plan["page"].to_s.empty? ? plan["provider"].to_s : link_to(plan["provider"].to_s, plan["page"])
|
|
37
|
+
content_tag(:tr, safe_join([plan["rank"], provider, plan["plan"], plan["score"] || "–",
|
|
38
|
+
Datacircle.money(plan["perThousand"]),].map { |cell| content_tag(:td, cell) }))
|
|
39
|
+
end
|
|
40
|
+
content_tag(:table, options) do
|
|
41
|
+
safe_join([
|
|
42
|
+
content_tag(:caption, safe_join(["The best #{answer["api"]} APIs. Scores and prices from ",
|
|
43
|
+
link_to("Datacircle", link), ".",])),
|
|
44
|
+
content_tag(:thead, content_tag(:tr, safe_join(head.map { |name| content_tag(:th, name) }))),
|
|
45
|
+
content_tag(:tbody, safe_join(rows)),
|
|
46
|
+
])
|
|
47
|
+
end
|
|
48
|
+
rescue Datacircle::Error => e
|
|
49
|
+
raise if e.is_a?(Datacircle::ApiError) && e.status != 429
|
|
50
|
+
|
|
51
|
+
::Rails.logger&.warn("Datacircle: the best #{api} table is left out: #{e.message}")
|
|
52
|
+
"".html_safe
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# A line crediting Datacircle, linking datacircle.dev (or url, as a best page of datacircle.dev).
|
|
56
|
+
def datacircle_credit(url = HOME, **options)
|
|
57
|
+
options[:class] = ["datacircle-credit", *options[:class]].join(" ")
|
|
58
|
+
content_tag(:p, safe_join(["Scores and prices from ", link_to("Datacircle", url), "."]), options)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# $ per 1,000 results as the tables show it: "$7.98", "$0.0166", "–" when unknown.
|
|
62
|
+
def datacircle_money(value)
|
|
63
|
+
Datacircle.money(value)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "datacircle/version"
|
|
4
|
+
require "datacircle/client"
|
|
5
|
+
require "datacircle/helper"
|
|
6
|
+
require "datacircle/tasks"
|
|
7
|
+
require "datacircle/engine"
|
|
8
|
+
|
|
9
|
+
# Datacircle's open API in a Rails app: no key, no account. More at https://datacircle.dev
|
|
10
|
+
module Datacircle
|
|
11
|
+
# A client set up by config.datacircle: answers kept in Rails.cache unless config.datacircle.cache_store says otherwise.
|
|
12
|
+
def self.client
|
|
13
|
+
config = ::Rails.application.config.datacircle
|
|
14
|
+
store = config.cache_store.nil? ? ::Rails.cache : config.cache_store
|
|
15
|
+
Client.new(base_url: config.base_url, timeout: config.timeout, cache: store || nil,
|
|
16
|
+
expires_in: config.expires_in, logger: ::Rails.logger)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Datacircle
|
|
6
|
+
# What the datacircle:* rake tasks print: a table of the API's answer, or its JSON when json is true.
|
|
7
|
+
class Tasks
|
|
8
|
+
# Runs one task on Datacircle.client: an error is printed in the API's words and the task exits 1.
|
|
9
|
+
def self.run(out: $stdout, json: ENV["FORMAT"].to_s.casecmp?("json"))
|
|
10
|
+
yield new(Datacircle.client, out: out, json: json)
|
|
11
|
+
rescue Datacircle::Error, ArgumentError => e
|
|
12
|
+
message = e.message.delete_prefix("Datacircle: ")
|
|
13
|
+
message += "\nbin/rails datacircle:apis lists every slug." if e.is_a?(ApiError) && e.body.is_a?(Hash) && e.body["slugs"]
|
|
14
|
+
abort "Datacircle: #{message}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(client, out: $stdout, json: false)
|
|
18
|
+
@client = client
|
|
19
|
+
@out = out
|
|
20
|
+
@json = json
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def apis
|
|
24
|
+
apis = @client.apis
|
|
25
|
+
return print_json(apis) if @json
|
|
26
|
+
|
|
27
|
+
rows = Array(apis).map do |api|
|
|
28
|
+
best = api["best"] || {}
|
|
29
|
+
[api["api"], api["slug"], api["plans"], api["providers"], [best["provider"], best["plan"]].compact.join(" "),
|
|
30
|
+
best["score"], Datacircle.money(best["perThousand"]),]
|
|
31
|
+
end
|
|
32
|
+
table(["API", "Slug", "Plans", "Providers", "Best plan", "Score", "$ per 1,000"], rows)
|
|
33
|
+
line "#{rows.size} APIs. bin/rails datacircle:best API=<slug> ranks one API's plans. More at #{HOME}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def best(api, limit: nil)
|
|
37
|
+
answer = @client.best(api, limit: whole("LIMIT", limit))
|
|
38
|
+
return print_json(answer) if @json
|
|
39
|
+
|
|
40
|
+
plans = Array(answer["plans"])
|
|
41
|
+
line "#{answer["api"]}: #{plans.size} of #{answer["total"]} plans, best first"
|
|
42
|
+
table(["Rank", "Provider", "Plan", "Score", "$ per 1,000", "Page"],
|
|
43
|
+
plans.map { |p| [p["rank"], p["provider"], p["plan"], p["score"], Datacircle.money(p["perThousand"]), p["page"]] })
|
|
44
|
+
line "More at #{answer["bestPage"] || answer["sheetPage"] || HOME}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def plans(query: nil, api: nil, sort: nil, limit: nil, offset: nil)
|
|
48
|
+
offset = whole("OFFSET", offset)
|
|
49
|
+
answer = @client.plans(api: api, query: query, sort: sort, limit: whole("LIMIT", limit), offset: offset)
|
|
50
|
+
return print_json(answer) if @json
|
|
51
|
+
|
|
52
|
+
plans = Array(answer["plans"])
|
|
53
|
+
table(["API", "Provider", "Plan", "Score", "$ per 1,000", "Page"],
|
|
54
|
+
plans.map { |p| [p["api"], p["provider"], p["plan"], p["score"], Datacircle.money(p["perThousand"]), p["page"]] })
|
|
55
|
+
total = answer["total"].to_i
|
|
56
|
+
from = (answer["offset"] || offset || 0).to_i
|
|
57
|
+
return line("#{total} plans found.") if plans.empty?
|
|
58
|
+
|
|
59
|
+
to = from + plans.size
|
|
60
|
+
line "Plans #{from + 1} to #{to} of #{total}.#{" OFFSET=#{to} shows the next." if to < total}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def provider(provider, best: false)
|
|
64
|
+
answer = @client.provider(provider, plans: best ? "best" : "none")
|
|
65
|
+
return print_json(answer) if @json
|
|
66
|
+
|
|
67
|
+
apis = Array(answer["apis"])
|
|
68
|
+
line "#{answer["provider"]} (#{answer["website"]}): #{apis.size} APIs"
|
|
69
|
+
head = ["API", "Slug", "Rank", "Plans", "Page"]
|
|
70
|
+
head += ["Best plan", "Score", "$ per 1,000"] if best
|
|
71
|
+
table(head, apis.map do |api|
|
|
72
|
+
row = [api["api"], api["slug"], api["rank"], api["planCount"], api["bestPage"] || api["sheetPage"]]
|
|
73
|
+
top = Array(api["plans"]).first || {}
|
|
74
|
+
best ? row + [top["plan"], top["score"], Datacircle.money(top["perThousand"])] : row
|
|
75
|
+
end)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# A whole number given as LIMIT= or OFFSET=, nil when left out. The API checks its range.
|
|
81
|
+
def whole(name, value)
|
|
82
|
+
return nil if value.nil? || value.to_s.strip.empty?
|
|
83
|
+
raise ArgumentError, "#{name} must be a whole number" unless value.to_s.strip.match?(/\A\d+\z/)
|
|
84
|
+
|
|
85
|
+
value.to_s.strip.to_i
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def print_json(answer)
|
|
89
|
+
line JSON.pretty_generate(answer)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def line(text)
|
|
93
|
+
@out.puts text
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def table(head, rows)
|
|
97
|
+
rows = rows.map { |row| row.map { |cell| cell.nil? || cell.to_s.empty? ? "-" : cell.to_s } }
|
|
98
|
+
widths = head.each_index.map { |i| ([head[i]] + rows.map { |row| row[i] }).map(&:length).max }
|
|
99
|
+
format_row = ->(row) { row.each_with_index.map { |cell, i| cell.ljust(widths[i]) }.join(" ").rstrip }
|
|
100
|
+
line format_row.call(head)
|
|
101
|
+
line widths.map { |w| "-" * w }.join(" ")
|
|
102
|
+
rows.each { |row| line format_row.call(row) }
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module Datacircle
|
|
6
|
+
module Generators
|
|
7
|
+
# bin/rails generate datacircle:install
|
|
8
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
9
|
+
source_root File.expand_path("templates", __dir__)
|
|
10
|
+
desc "Adds config/initializers/datacircle.rb, where Datacircle's client is set up."
|
|
11
|
+
|
|
12
|
+
def copy_initializer
|
|
13
|
+
template "datacircle.rb", "config/initializers/datacircle.rb"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Datacircle's open API, https://api.datacircle.dev/v1: no key, no account. More at https://datacircle.dev
|
|
4
|
+
Rails.application.configure do
|
|
5
|
+
# Where the client calls.
|
|
6
|
+
config.datacircle.base_url = "https://api.datacircle.dev/v1"
|
|
7
|
+
|
|
8
|
+
# Seconds a call may take.
|
|
9
|
+
config.datacircle.timeout = 30
|
|
10
|
+
|
|
11
|
+
# How long an answer is kept in the cache. Its data changes when a benchmark runs again.
|
|
12
|
+
config.datacircle.expires_in = 1.hour
|
|
13
|
+
|
|
14
|
+
# The cache store answers are kept in: Rails.cache when nil, none when false.
|
|
15
|
+
config.datacircle.cache_store = nil
|
|
16
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Datacircle's open API from the command line: bin/rails datacircle:apis, and FORMAT=json on each for its JSON.
|
|
4
|
+
namespace :datacircle do
|
|
5
|
+
desc "List every GTM API on Datacircle with its slug and best plan"
|
|
6
|
+
task apis: :environment do
|
|
7
|
+
Datacircle::Tasks.run(&:apis)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
desc "Show one GTM API's best plans: API=email-finder [LIMIT=10]"
|
|
11
|
+
task :best, [:api, :limit] => :environment do |_, args|
|
|
12
|
+
Datacircle::Tasks.run { |t| t.best(args[:api] || ENV["API"], limit: args[:limit] || ENV["LIMIT"]) }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
desc "Search the plans of every GTM API: [QUERY=hunter] [API=email-finder] [SORT=score|provider|price] [LIMIT=10] [OFFSET=0]"
|
|
16
|
+
task :plans, [:query] => :environment do |_, args|
|
|
17
|
+
Datacircle::Tasks.run do |t|
|
|
18
|
+
t.plans(query: args[:query] || ENV["QUERY"], api: ENV["API"], sort: ENV["SORT"], limit: ENV["LIMIT"], offset: ENV["OFFSET"])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
desc "Show the GTM APIs one provider sells and its rank on each: PROVIDER=bright-data [BEST=1]"
|
|
23
|
+
task :provider, [:provider] => :environment do |_, args|
|
|
24
|
+
Datacircle::Tasks.run { |t| t.provider(args[:provider] || ENV["PROVIDER"], best: %w[1 true yes].include?(ENV["BEST"].to_s.downcase)) }
|
|
25
|
+
end
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: datacircle-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wayne Hamadi
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: railties
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '7.2'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '9'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '7.2'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '9'
|
|
32
|
+
description: 'This Rails engine reads Datacircle''s open API. You need no API key.
|
|
33
|
+
The engine adds a client, rake tasks and view helpers to your app. Your app can
|
|
34
|
+
list every GTM API. Your app can search every plan. Your app can rank the best plans
|
|
35
|
+
for one API. Your app can show the ranks of one provider. The engine keeps answers
|
|
36
|
+
in Rails.cache. We benchmark all the GTM APIs and we put the results on our website:
|
|
37
|
+
https://datacircle.dev'
|
|
38
|
+
email:
|
|
39
|
+
- wayne@datacircle.dev
|
|
40
|
+
executables: []
|
|
41
|
+
extensions: []
|
|
42
|
+
extra_rdoc_files: []
|
|
43
|
+
files:
|
|
44
|
+
- LICENSE
|
|
45
|
+
- README.md
|
|
46
|
+
- lib/datacircle-rails.rb
|
|
47
|
+
- lib/datacircle/client.rb
|
|
48
|
+
- lib/datacircle/engine.rb
|
|
49
|
+
- lib/datacircle/helper.rb
|
|
50
|
+
- lib/datacircle/rails.rb
|
|
51
|
+
- lib/datacircle/tasks.rb
|
|
52
|
+
- lib/datacircle/version.rb
|
|
53
|
+
- lib/generators/datacircle/install/install_generator.rb
|
|
54
|
+
- lib/generators/datacircle/install/templates/datacircle.rb
|
|
55
|
+
- lib/tasks/datacircle.rake
|
|
56
|
+
homepage: https://datacircle.dev
|
|
57
|
+
licenses:
|
|
58
|
+
- MIT
|
|
59
|
+
metadata:
|
|
60
|
+
homepage_uri: https://datacircle.dev
|
|
61
|
+
source_code_uri: https://github.com/waynehamadi/datacircle-rails
|
|
62
|
+
documentation_uri: https://github.com/waynehamadi/datacircle-rails#readme
|
|
63
|
+
bug_tracker_uri: https://github.com/waynehamadi/datacircle-rails/issues
|
|
64
|
+
rdoc_options: []
|
|
65
|
+
require_paths:
|
|
66
|
+
- lib
|
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '3.1'
|
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
requirements: []
|
|
78
|
+
rubygems_version: 3.6.9
|
|
79
|
+
specification_version: 4
|
|
80
|
+
summary: Find the best GTM APIs. Datacircle's API scores and prices in your Rails
|
|
81
|
+
app.
|
|
82
|
+
test_files: []
|