docs: Add Ruby SDK README with comprehensive API documentation and implement modular architecture with HTTParty integration

- Add README.md with installation instructions, architecture overview, and API examples for all SDK modules
- Refactor TelecomSDK::Client to use modular API clients (SubscriberAPI, UsageAPI, PaymentAPI, RatingPlanAPI, SystemAPI, GraphQLAPI)
- Add AuthProvider class with API key and JWT token support (generate_jwt_token, validate_jwt_token methods)
- Add HTTPClient base class with retry
This commit is contained in:
nutcas3
2026-04-25 04:57:00 +03:00
parent d846c304a4
commit ea20dfb302
10 changed files with 637 additions and 194 deletions
+50 -194
View File
@@ -3,6 +3,15 @@ require "json"
require "websocket-client-simple"
require "uri"
require_relative "telecom_sdk/auth"
require_relative "telecom_sdk/http_client"
require_relative "telecom_sdk/subscribers_api"
require_relative "telecom_sdk/usage_api"
require_relative "telecom_sdk/payments_api"
require_relative "telecom_sdk/rating_plans_api"
require_relative "telecom_sdk/system_api"
require_relative "telecom_sdk/graphql_api"
module TelecomSDK
class Error < StandardError; end
class AuthenticationError < Error; end
@@ -13,210 +22,57 @@ module TelecomSDK
class ServerError < Error; end
class Client
include HTTParty
attr_reader :config
attr_reader :config, :subscribers, :usage, :payments, :rating_plans, :system, :graphql
def initialize(config = {})
@config = Config.new(config)
self.class.base_uri @config.api_url
self.class.default_options.update(
@auth_provider = AuthProvider.new(api_key: @config.api_key, jwt_secret: @config.jwt_secret)
@http_client = HTTPClient.new(
@config.api_url,
@auth_provider,
timeout: @config.timeout,
headers: {
"User-Agent" => "Telecom-Ruby-SDK/1.0.0",
"Content-Type" => "application/json"
}
max_retries: @config.max_retries,
retry_delay: @config.retry_delay,
enable_logging: @config.enable_logging
)
if @config.api_key
self.class.default_options[:headers]["Authorization"] = "Bearer #{@config.api_key}"
end
# Initialize API modules
@subscribers = SubscriberAPI.new(@http_client)
@usage = UsageAPI.new(@http_client)
@payments = PaymentAPI.new(@http_client)
@rating_plans = RatingPlanAPI.new(@http_client)
@system = SystemAPI.new(@http_client)
@graphql = GraphQLAPI.new(@http_client)
end
# Subscriber Management
def get_subscriber(id)
response = self.class.get("/v1/subscribers/#{id}")
handle_response(response)
Subscriber.new(response.parsed_response)
# Authentication methods
def generate_jwt_token(user_id, expiry_hours = 24, additional_claims = {})
@auth_provider.generate_jwt_token(user_id, expiry_hours, additional_claims)
end
def list_subscribers(page: 1, page_size: 50, status: nil)
params = { page: page, page_size: page_size }
params[:status] = status if status
response = self.class.get("/v1/subscribers", query: params)
handle_response(response)
SubscriberList.new(response.parsed_response)
def validate_jwt_token(token)
@auth_provider.validate_jwt_token(token)
end
def create_subscriber(request)
response = self.class.post("/v1/subscribers", body: request.to_json)
handle_response(response)
Subscriber.new(response.parsed_response)
end
def update_subscriber(id, request)
response = self.class.put("/v1/subscribers/#{id}", body: request.to_json)
handle_response(response)
Subscriber.new(response.parsed_response)
end
def suspend_subscriber(id)
response = self.class.post("/v1/subscribers/#{id}/suspend")
handle_response(response)
Subscriber.new(response.parsed_response)
end
def activate_subscriber(id)
response = self.class.post("/v1/subscribers/#{id}/activate")
handle_response(response)
Subscriber.new(response.parsed_response)
end
def terminate_subscriber(id)
response = self.class.delete("/v1/subscribers/#{id}")
handle_response(response)
response.parsed_response["success"]
end
# Usage Management
def get_usage_stats(subscriber_id, start_date, end_date)
params = {
start_date: start_date.iso8601,
end_date: end_date.iso8601
}
response = self.class.get("/v1/subscribers/#{subscriber_id}/usage", query: params)
handle_response(response)
UsageStats.new(response.parsed_response)
end
def list_usage_events(subscriber_id: nil, usage_type: nil, start_date: nil, end_date: nil, page: 1, page_size: 50)
params = { page: page, page_size: page_size }
params[:subscriber_id] = subscriber_id if subscriber_id
params[:usage_type] = usage_type if usage_type
params[:start_date] = start_date.iso8601 if start_date
params[:end_date] = end_date.iso8601 if end_date
response = self.class.get("/v1/usage/events", query: params)
handle_response(response)
UsageEventList.new(response.parsed_response)
end
def get_real_time_usage(subscriber_id)
response = self.class.get("/v1/subscribers/#{subscriber_id}/realtime")
handle_response(response)
RealTimeUsage.new(response.parsed_response)
end
# Payment Management
def create_payment_transaction(request)
response = self.class.post("/v1/payments/transactions", body: request.to_json)
handle_response(response)
PaymentTransaction.new(response.parsed_response)
end
def get_payment_transaction(transaction_id)
response = self.class.get("/v1/payments/transactions/#{transaction_id}")
handle_response(response)
PaymentTransaction.new(response.parsed_response)
end
def list_payment_transactions(subscriber_id: nil, status: nil, page: 1, page_size: 50)
params = { page: page, page_size: page_size }
params[:subscriber_id] = subscriber_id if subscriber_id
params[:status] = status if status
response = self.class.get("/v1/payments/transactions", query: params)
handle_response(response)
PaymentTransactionList.new(response.parsed_response)
end
# Rating Plans
def list_rating_plans
response = self.class.get("/v1/rating-plans")
handle_response(response)
response.parsed_response.map { |plan| RatingPlan.new(plan) }
end
def get_rating_plan(plan_id)
response = self.class.get("/v1/rating-plans/#{plan_id}")
handle_response(response)
RatingPlan.new(response.parsed_response)
end
# System Management
def get_system_stats
response = self.class.get("/v1/system/stats")
handle_response(response)
SystemStats.new(response.parsed_response)
end
def get_health_status
response = self.class.get("/v1/health")
handle_response(response)
HealthStatus.new(response.parsed_response)
end
# WebSocket Support
def connect_websocket(message_handler: nil)
ws_url = @config.api_url.gsub("http://", "ws://") + "/ws"
WebSocket::Client::Simple.connect(ws_url) do |ws|
ws.on :message do |msg|
begin
data = JSON.parse(msg.data)
ws_message = WebSocketMessage.new(data)
message_handler.call(ws_message) if message_handler
rescue JSON::ParserError => e
raise APIError, "Failed to parse WebSocket message: #{e.message}"
end
end
end
end
# GraphQL Support
def execute_graphql_query(query, variables = {})
request = { query: query, variables: variables }
response = self.class.post("/graphql", body: request.to_json)
handle_response(response)
GraphQLResponse.new(response.parsed_response)
end
private
def handle_response(response)
case response.code
when 401
raise AuthenticationError, "Authentication failed"
when 429
raise RateLimitError, "Rate limit exceeded"
when 400..499
error_data = response.parsed_response["error"] || "Bad request"
raise APIError, "API error: #{error_data}"
when 500..599
raise ServerError, "Server error: #{response.code}"
end
response
# Cleanup
def close
@http_client.close
end
end
class Config
attr_accessor :api_url, :api_key, :timeout, :max_retries
def initialize(options = {})
@api_url = options[:api_url] || "http://localhost:8000"
@api_key = options[:api_key]
@timeout = options[:timeout] || 30
@max_retries = options[:max_retries] || 3
attr_accessor :api_url, :api_key, :jwt_secret, :timeout, :max_retries, :retry_delay, :enable_logging
def initialize(config = {})
@api_url = config[:api_url] || "http://localhost:8000"
@api_key = config[:api_key]
@jwt_secret = config[:jwt_secret]
@timeout = config[:timeout] || 30
@max_retries = config[:max_retries] || 3
@retry_delay = config[:retry_delay] || 1
@enable_logging = config[:enable_logging] || false
end
end
end