tinybird 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20843d4d73262c83e29f389e11e530c6c26128f93c1e41b12822147497714217
4
- data.tar.gz: 6e9fa9bec789401fde7704554ace18a3a69304118e315e563e2bc56a1e40cba7
3
+ metadata.gz: '08ce180c2ced2aeb456764d97c50976aa56d5e534e9499fd3aba3b06937373f2'
4
+ data.tar.gz: 72bd467c7d10f9f2bf6d6aeacf52e27603a9c9a4b465274e4e5b0d982df29668
5
5
  SHA512:
6
- metadata.gz: 77b30d000e0c7cba474b0e33e8f315eb7941749e6c23ed836ff8466a697db2b389e60e2213ce49232ea2d453f3df7a196ec6a203bd2bb052fed074d825364ef2
7
- data.tar.gz: 1366183deb36160c210af308570df410813b81b602aaa7dc92e7ac28a56c896fe645f4b7a096631854b27dc1aeb1d06f0d65fa6b27c22ea49531f7ffefd43b2e
6
+ metadata.gz: e82be52459f3d7ff31e368f5cff3a0cf311850428271b921bc57584e640208fccfc65728b578ee9fd3edc181fcab7a2e4c764f5e722906a407ef9b81bd7941f8
7
+ data.tar.gz: 3c9e5bfdb537b5588af1ac8c08e907fcfef941b9564f4a0b8ed522ea65fdd5faaf4c9939f3bc6c117ec30ed2a8aebed18ceba64efe4adf9c3c2a15a89ca266fd
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybird
4
+ module Callable
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def call(*args, **)
11
+ if args.empty?
12
+ new(**).call
13
+ else
14
+ new(*args, **).call
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # lib/tinybird/configuration.rb
2
+
3
+ module Tinybird
4
+ class Configuration
5
+ attr_accessor :auth_token, :base_url
6
+
7
+ def initialize
8
+ @auth_token = ENV.fetch("TINYBIRD_AUTH_TOKEN", nil)
9
+ @base_url = ENV.fetch("TINYBIRD_BASE_URL", "https://api.tinybird.co/v0")
10
+ end
11
+ end
12
+
13
+ class << self
14
+ attr_writer :configuration
15
+
16
+ def configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ alias_method :config, :configuration
21
+
22
+ def configure
23
+ yield configuration
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # lib/tinybird/event/create.rb
2
+
3
+ module Tinybird
4
+ module Event
5
+ class Create
6
+ include Tinybird::Callable
7
+ include Tinybird::Requestable
8
+
9
+ requestable method: :post
10
+
11
+ attr_reader :body, :event_name, :wait, :headers
12
+
13
+ def initialize(body, event_name:, wait: false, headers: {})
14
+ @body = body
15
+ @event_name = event_name
16
+ @wait = wait
17
+ @headers = headers
18
+ end
19
+
20
+ private
21
+
22
+ def path_segment
23
+ "v0/events?name=#{event_name}&wait=#{wait}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tinybird/event/create"
4
+
5
+ module Tinybird
6
+ module Event
7
+ class << self
8
+ def create(body, event_name:, wait: false, headers: {})
9
+ Create.call(body, event_name: event_name, wait: wait, headers: headers)
10
+ end
11
+ end
12
+
13
+ private_constant :Create
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybird
4
+ class HttpClient
5
+ attr_reader :uri
6
+
7
+ def initialize(uri)
8
+ @uri = uri
9
+ end
10
+
11
+ def get(headers = {})
12
+ request = Net::HTTP::Get.new(uri, headers)
13
+ make_request(request)
14
+ end
15
+
16
+ def post(body, headers = {})
17
+ request = Net::HTTP::Post.new(uri, headers)
18
+ request.body = body.to_json
19
+ make_request(request)
20
+ end
21
+
22
+ private
23
+
24
+ def make_request(request)
25
+ request["Authorization"] = "Bearer #{Tinybird.config.auth_token}"
26
+ request["User-Agent"] = "tinybird-ruby/#{Tinybird::VERSION}"
27
+
28
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
29
+ http.request(request).then do |response|
30
+ Response.new(response)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybird
4
+ module Query
5
+ class Get
6
+ include Tinybird::Callable
7
+ include Tinybird::Requestable
8
+
9
+ requestable method: :get
10
+
11
+ ALLOWED_FORMATS = %w[CSV CSVWithoutHeader CSVWithNames JSON TSV TSVWithoutHeader TSVWithNames PrettyCompact JSONEachRow Parquet].freeze
12
+
13
+ attr_reader :sql, :format, :headers
14
+
15
+ def initialize(sql: "", format: "JSON", headers: {})
16
+ @sql = sql
17
+ @format = format
18
+ @headers = headers
19
+ validate_format!
20
+ end
21
+
22
+ private
23
+
24
+ def path_segment
25
+ "v0/sql?q=#{formatted_sql}"
26
+ end
27
+
28
+ def body
29
+ nil
30
+ end
31
+
32
+ def formatted_sql
33
+ "#{sql} FORMAT #{format}"
34
+ end
35
+
36
+ def validate_format!
37
+ raise InvalidFormatError, "Invalid format: #{format}. Supported formats are: #{ALLOWED_FORMATS.join(", ")}" unless ALLOWED_FORMATS.include?(format)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tinybird/query/get"
4
+
5
+ module Tinybird
6
+ module Query
7
+ class << self
8
+ def get(sql: "", format: "JSON", headers: {})
9
+ Get.call(sql: sql, format: format, headers: headers)
10
+ end
11
+ end
12
+
13
+ private_constant :Get
14
+ end
15
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybird
4
+ module Requestable
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ DEFAULT_REQUEST_METHODS = %i[get post delete].freeze
11
+
12
+ private
13
+
14
+ def requestable(**options)
15
+ method = options.fetch(:method, DEFAULT_REQUEST_METHODS)
16
+ define_http_method(method)
17
+ end
18
+
19
+ def define_http_method(method)
20
+ define_method(:http_method) do
21
+ method
22
+ end
23
+ end
24
+ end
25
+
26
+ def call(**args)
27
+ uri = URI(endpoint)
28
+ client = Tinybird::HttpClient.new(uri)
29
+ if http_method == :get
30
+ client.send(http_method, headers)
31
+ else
32
+ client.send(http_method, body, headers)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def base_url
39
+ Tinybird.config.base_url.sub(/\/$/, "")
40
+ end
41
+
42
+ def endpoint
43
+ [
44
+ base_url,
45
+ path_segment
46
+ ].join("/")
47
+ end
48
+
49
+ def path_segment
50
+ raise NotImplementedError, "#{self.class} must implement #path_segment"
51
+ end
52
+
53
+ def body
54
+ raise NotImplementedError, "#{self.class} must implement #body"
55
+ end
56
+
57
+ def headers
58
+ raise NotImplementedError, "#{self.class} must implement #headers"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybird
4
+ class Response
5
+ attr_reader :status, :headers, :body, :message
6
+
7
+ def initialize(response)
8
+ @status = response.code.to_i
9
+ @headers = response.to_hash
10
+ @message = response.message
11
+ @body = begin
12
+ JSON.parse(response.body)
13
+ rescue JSON::ParserError
14
+ response.body
15
+ end
16
+ end
17
+
18
+ def inspect
19
+ "#<#{self.class}:0x#{object_id.to_s(16)} #{status} #{message} readbody=true>"
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tinybird
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/tinybird.rb CHANGED
@@ -1,7 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "tinybird/version"
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ require "tinybird/version"
8
+ require "tinybird/configuration"
9
+ require "tinybird/http_client"
10
+ require "tinybird/response"
11
+
12
+ require "tinybird/callable"
13
+ require "tinybird/requestable"
14
+
15
+ require "tinybird/query"
16
+ require "tinybird/event"
4
17
 
5
18
  module Tinybird
6
19
  class Error < StandardError; end
20
+
21
+ class InvalidFormatError < Error; end
7
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinybird
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drew Monroe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-03 00:00:00.000000000 Z
11
+ date: 2024-08-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby client for the Tinybird API
14
14
  email:
@@ -32,6 +32,15 @@ files:
32
32
  - bin/standardrb
33
33
  - docker-compose.yml
34
34
  - lib/tinybird.rb
35
+ - lib/tinybird/callable.rb
36
+ - lib/tinybird/configuration.rb
37
+ - lib/tinybird/event.rb
38
+ - lib/tinybird/event/create.rb
39
+ - lib/tinybird/http_client.rb
40
+ - lib/tinybird/query.rb
41
+ - lib/tinybird/query/get.rb
42
+ - lib/tinybird/requestable.rb
43
+ - lib/tinybird/response.rb
35
44
  - lib/tinybird/version.rb
36
45
  - tinybird.gemspec
37
46
  homepage: https://github.com/dvmonroe/tinybird-ruby