Sha256: 3ee3bdcf62e1ba26aa733d61f775c1bffcb672a054946c95a7662216621bde0f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'net/http'
require 'json'

module Paraxial
  module FreeTier
    class << self
      attr_reader :is_free_tier

      def initialize
        @is_free_tier = false
        check_free_tier_status
      end

      private

      # Kicks off an async HTTP request
      def check_free_tier_status
        Thread.new do
          uri = URI.parse(Paraxial::Helpers.get_free_tier_url())
          headers = { 'Content-Type': 'application/json' }
          body = { api_key: Paraxial::Helpers.get_api_key }
          r = Net::HTTP.post(uri, body.to_json, headers)
          result = JSON.parse(r.body)
          # Assume the API response contains a field `free_tier` (true/false)
          # Response Body: {"error":"api_key not valid"}
          if result['free_tier'] == true
            puts "[Paraxial] Free tier site, some functionality is disabled"
            @is_free_tier = true
          elsif result['free_tier'] == false
            puts "[Paraxial] Account active, all features available"
            @is_free_tier = false
          else
            puts "[Paraxial] Account check failed, some functionality is disabled"
            @is_free_tier = true
          end
        rescue StandardError => e
          # Handle any errors (network issues, parsing errors, etc.)
          puts "Error fetching free tier status: #{e.message}"
          @is_free_tier = true
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paraxial-1.4.5 lib/paraxial/free_tier.rb