feralin 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.
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module Feralin
6
+ # `track_revenue` sözleşmesi (F5-E04-T01): olay adı `feralin_purchase`; tutar küçük birimde tam sayı (kuruş, cent), para birimi
7
+ # ISO 4217. İstemci beyanıdır: `verified` hiçbir zaman gönderilmez, doğrulama sunucu tarafı makbuz doğrulayıcısınındır. Kurallar
8
+ # Web, Node, Python, Go ve .NET SDK'larıyla birebir aynıdır.
9
+ module Revenue
10
+ EVENT_NAME = "feralin_purchase"
11
+ PURCHASE_TYPES = %w[purchase subscription_start subscription_renew subscription_cancel].freeze
12
+ RECEIPT_PROVIDERS = %w[apple google stripe].freeze
13
+ MAX_AMOUNT_MINOR = (2**53) - 1
14
+ MAX_QUANTITY = 100_000
15
+
16
+ # İstemci doğrulama beyan edemez: bu anahtarlar çağıranın özelliklerinden düşürülür, sunucu da düşürür.
17
+ RESERVED_KEYS = %w[verified verification verification_source receipt_provider receipt_digest receipt].freeze
18
+ CURRENCY_FORMAT = /\A[A-Z]{3}\z/.freeze
19
+
20
+ module_function
21
+
22
+ # Gelir olayının özelliklerini kurar; sözleşme dışı girdide InvalidEventError fırlatır. amount_minor satır toplamıdır
23
+ # (quantity ile çarpılmaz); abonelik iptalinde 0. receipt: { provider: "apple"|"google"|"stripe", token: "..." }.
24
+ def properties(transaction_id:, amount_minor:, currency:, product_id: nil, quantity: 1, purchase_type: "purchase",
25
+ receipt: nil, properties: nil)
26
+ unless transaction_id.is_a?(String) && transaction_id.length.between?(1, 128)
27
+ raise InvalidEventError, "transaction_id 1..128 karakterlik metin olmalı"
28
+ end
29
+ # Integer dışı (Float, BigDecimal, metin) kabul edilmez; kayan nokta yuvarlaması defteri bozardı.
30
+ unless amount_minor.is_a?(Integer) && amount_minor.between?(0, MAX_AMOUNT_MINOR)
31
+ raise InvalidEventError, "amount_minor küçük birimde (kuruş, cent) 0..2^53-1 tam sayı olmalı; ondalık kabul edilmez"
32
+ end
33
+ raise InvalidEventError, "currency ISO 4217 üç büyük harf olmalı" unless currency.is_a?(String) && CURRENCY_FORMAT.match?(currency)
34
+ if !product_id.nil? && !(product_id.is_a?(String) && product_id.length.between?(1, 128))
35
+ raise InvalidEventError, "product_id 1..128 karakterlik metin olmalı"
36
+ end
37
+ unless quantity.is_a?(Integer) && quantity.between?(0, MAX_QUANTITY)
38
+ raise InvalidEventError, "quantity 0..100000 tam sayı olmalı"
39
+ end
40
+
41
+ type = purchase_type.is_a?(Symbol) ? purchase_type.to_s : purchase_type
42
+ raise InvalidEventError, "purchase_type #{PURCHASE_TYPES.join('|')} değerlerinden biri olmalı" unless PURCHASE_TYPES.include?(type)
43
+ # Abonelik iptali para taşımaz; defter tutarlı iptali reddeder.
44
+ if type == "subscription_cancel" && amount_minor != 0
45
+ raise InvalidEventError, "subscription_cancel için amount_minor 0 olmalı"
46
+ end
47
+
48
+ receipt_value = normalize_receipt(receipt)
49
+ raise InvalidEventError, "properties bir Hash olmalı" unless properties.nil? || properties.is_a?(Hash)
50
+
51
+ # Sözleşme alanları önce yazılır: 100 özellik sınırını aşan girdi çağıranın ek alanlarıdır.
52
+ props = {
53
+ "transaction_id" => transaction_id,
54
+ "amount_minor" => amount_minor,
55
+ "currency" => currency,
56
+ "quantity" => quantity,
57
+ "purchase_type" => type
58
+ }
59
+ props["product_id"] = product_id unless product_id.nil?
60
+ props["receipt"] = receipt_value if receipt_value
61
+ (properties || {}).each do |key, value|
62
+ name = key.is_a?(Symbol) ? key.to_s : key
63
+ props[name] = value unless RESERVED_KEYS.include?(name) || props.key?(name)
64
+ end
65
+ props
66
+ end
67
+
68
+ def normalize_receipt(receipt)
69
+ return nil if receipt.nil?
70
+ raise InvalidEventError, "receipt bir Hash olmalı" unless receipt.is_a?(Hash)
71
+
72
+ provider = receipt[:provider] || receipt["provider"]
73
+ provider = provider.to_s if provider.is_a?(Symbol)
74
+ token = receipt[:token] || receipt["token"]
75
+ raise InvalidEventError, "receipt.provider apple|google|stripe olmalı" unless RECEIPT_PROVIDERS.include?(provider)
76
+ raise InvalidEventError, "receipt.token 1..8192 karakterlik metin olmalı" unless token.is_a?(String) && token.length.between?(1, 8192)
77
+
78
+ { "provider" => provider, "token" => token }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "uri"
6
+ require_relative "errors"
7
+
8
+ module Feralin
9
+ # HTTP taşıma katmanı: yalnız net/http. Testler ve özel ağ yığınları için
10
+ # `transport: ->(url, body, headers, timeout) { [status, body, response_headers] }` ile değiştirilebilir.
11
+ module Transport
12
+ MAX_RESPONSE_BODY = 64 * 1024
13
+
14
+ NETWORK_ERRORS = [
15
+ SocketError,
16
+ SystemCallError,
17
+ IOError,
18
+ Timeout::Error,
19
+ Net::ProtocolError,
20
+ Net::HTTPBadResponse,
21
+ Net::HTTPHeaderSyntaxError,
22
+ OpenSSL::SSL::SSLError
23
+ ].freeze
24
+
25
+ module_function
26
+
27
+ # POST isteği yapar. HTTP hata kodları normal yanıt olarak döner; ağ veya zaman aşımı TemporaryError fırlatır.
28
+ def net_http(url, body, headers, timeout)
29
+ uri = URI.parse(url)
30
+ http = Net::HTTP.new(uri.host, uri.port)
31
+ http.use_ssl = uri.scheme == "https"
32
+ http.open_timeout = timeout
33
+ http.read_timeout = timeout
34
+ http.write_timeout = timeout
35
+ # Yeniden deneme istemcinin işidir; net/http kendi başına ikinci istek göndermesin.
36
+ http.max_retries = 0
37
+
38
+ request = Net::HTTP::Post.new(uri.request_uri, { "Accept-Encoding" => "identity" }.merge(headers))
39
+ request.body = body
40
+
41
+ status = 0
42
+ payload = String.new(encoding: Encoding::BINARY)
43
+ response_headers = {}
44
+ http.start do
45
+ http.request(request) do |response|
46
+ status = response.code.to_i
47
+ response.each_header { |name, value| response_headers[name] = value }
48
+ response.read_body do |chunk|
49
+ remaining = MAX_RESPONSE_BODY - payload.bytesize
50
+ payload << chunk.byteslice(0, remaining) if remaining.positive?
51
+ end
52
+ end
53
+ end
54
+ [status, payload, response_headers]
55
+ rescue *NETWORK_ERRORS => e
56
+ raise TemporaryError.new("ağ hatası: #{e.class}: #{e.message}", status: 0)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Feralin
6
+ # Monotonik ULID üretimi (26 karakter Crockford base32). Web SDK ile aynı düzen:
7
+ # 10 karakter zaman (ms) + 16 adet 5 bitlik rastgele basamak (80 bit).
8
+ module Ulid
9
+ CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
10
+ FORMAT = /\A[0-7][0-9A-HJKMNP-TV-Z]{25}\z/.freeze
11
+
12
+ # 26 karakterlik Crockford base32 ULID biçim denetimi (ilk karakter en fazla '7').
13
+ def self.valid?(value)
14
+ value.is_a?(String) && FORMAT.match?(value)
15
+ end
16
+
17
+ # Aynı milisaniyede (veya saat geri gittiğinde) son değeri artırarak sıralamayı korur; iş parçacığı güvenlidir.
18
+ class Generator
19
+ def initialize(now_ms = nil)
20
+ @lock = Mutex.new
21
+ @now_ms = now_ms || -> { Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) }
22
+ @last_ms = -1
23
+ @last_rand = Array.new(16, 0)
24
+ end
25
+
26
+ def generate
27
+ @lock.synchronize do
28
+ ms = @now_ms.call
29
+ if ms > @last_ms
30
+ fill_random
31
+ else
32
+ ms = @last_ms
33
+ unless increment
34
+ ms += 1
35
+ fill_random
36
+ end
37
+ end
38
+ @last_ms = ms
39
+ encode(ms)
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def encode(milliseconds)
46
+ out = String.new(capacity: 26)
47
+ remaining = milliseconds
48
+ time_digits = Array.new(10) do
49
+ digit = CROCKFORD[remaining & 31]
50
+ remaining >>= 5
51
+ digit
52
+ end
53
+ time_digits.reverse_each { |digit| out << digit }
54
+ @last_rand.each { |value| out << CROCKFORD[value] }
55
+ out
56
+ end
57
+
58
+ def fill_random
59
+ @last_rand = SecureRandom.random_bytes(16).bytes.map { |byte| byte & 31 }
60
+ end
61
+
62
+ # Rastgele kısmı base32 düzeninde bir artırır; tüm basamaklar taşarsa false döner.
63
+ def increment
64
+ 15.downto(0) do |index|
65
+ if @last_rand[index] < 31
66
+ @last_rand[index] += 1
67
+ return true
68
+ end
69
+ @last_rand[index] = 0
70
+ end
71
+ false
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Feralin
4
+ VERSION = "0.1.0"
5
+ end
data/lib/feralin.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "feralin/version"
4
+ require_relative "feralin/errors"
5
+ require_relative "feralin/ulid"
6
+ require_relative "feralin/events"
7
+ require_relative "feralin/transport"
8
+ require_relative "feralin/client"
9
+ require_relative "feralin/rack"
10
+
11
+ # Feralin UTP v1 sunucu SDK'sı.
12
+ module Feralin
13
+ class << self
14
+ # Rack middleware'in ve Railtie'nin paylaştığı süreç geneli istemci.
15
+ attr_accessor :client
16
+ end
17
+ end
18
+
19
+ require_relative "feralin/railtie" if defined?(::Rails::Railtie)
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: feralin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Feralin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Feralin toplayıcısına izin öncelikli, bellek içi kuyruklu ve idempotent
14
+ olay gönderir. Yalnız standart kütüphane kullanır; çalışma zamanı bağımlılığı yoktur.
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - lib/feralin.rb
24
+ - lib/feralin/client.rb
25
+ - lib/feralin/error_event.rb
26
+ - lib/feralin/errors.rb
27
+ - lib/feralin/events.rb
28
+ - lib/feralin/profile.rb
29
+ - lib/feralin/rack.rb
30
+ - lib/feralin/railtie.rb
31
+ - lib/feralin/revenue.rb
32
+ - lib/feralin/transport.rb
33
+ - lib/feralin/ulid.rb
34
+ - lib/feralin/version.rb
35
+ homepage: https://feralin.com
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ rubygems_mfa_required: 'true'
40
+ homepage_uri: https://feralin.com
41
+ source_code_uri: https://github.com/feralincom/sdk
42
+ changelog_uri: https://github.com/feralincom/sdk/blob/main/sdks/ruby/CHANGELOG.md
43
+ documentation_uri: https://github.com/feralincom/sdk/blob/main/docs/sdk/ruby.md
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.5.22
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: 'Feralin UTP v1 sunucu SDK''sı: Ruby istemcisi, Rack middleware ve Rails
63
+ Railtie'
64
+ test_files: []