Module: Mysigner::CLI::Concerns::ApiHelpers
- Included in:
- Mysigner::CLI
- Defined in:
- lib/mysigner/cli/concerns/api_helpers.rb
Instance Method Summary collapse
-
#default_api_url ⇒ Object
Smart API URL detection.
- #localhost_accessible?(url) ⇒ Boolean
-
#normalize_api_url(url) ⇒ Object
Normalize API URL (add protocol, remove trailing slash).
-
#prompt_api_url ⇒ Object
Prompt for API URL with smart default and validation.
-
#prompt_for_email ⇒ Object
Prompt for user email with validation.
-
#valid_api_url?(url) ⇒ Boolean
Validate API URL format.
-
#valid_email?(email) ⇒ Boolean
Basic email validation.
Instance Method Details
#default_api_url ⇒ Object
Smart API URL detection
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 8 def default_api_url # Priority: # 1. Environment variable # 2. Check if localhost is running # 3. Production default return ENV['MYSIGNER_API_URL'] if ENV['MYSIGNER_API_URL'] # Check if localhost:3000 is accessible localhost_url = 'http://localhost:3000' return localhost_url if localhost_accessible?(localhost_url) # Default to production 'https://mysigner.dev' end |
#localhost_accessible?(url) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 24 def localhost_accessible?(url) require 'net/http' require 'uri' begin uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = 1 http.read_timeout = 1 request = Net::HTTP::Get.new('/up') response = http.request(request) response.code == '200' rescue StandardError false end end |
#normalize_api_url(url) ⇒ Object
Normalize API URL (add protocol, remove trailing slash)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 64 def normalize_api_url(url) url = url.to_s.strip # Add a scheme if none was given. Default to https; fall back to # http ONLY for an obvious loopback host, so a scheme-less remote # host never silently downgrades the API token to cleartext. unless url.match?(%r{^https?://}) = url[%r{\A[^/:]+}].to_s.downcase scheme = Mysigner::Client::LOOPBACK_HOSTS.include?() ? 'http' : 'https' url = "#{scheme}://#{url}" end # Remove trailing slash url.chomp('/') end |
#prompt_api_url ⇒ Object
Prompt for API URL with smart default and validation
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 42 def prompt_api_url default = default_api_url say "API URL: #{default}", :cyan loop do custom = ask('Press Enter to use default, or type custom URL:', default: '') # Use default if empty or nil return default if custom.nil? || custom.empty? # Validate and normalize the custom URL normalized_url = normalize_api_url(custom.strip) return normalized_url if valid_api_url?(normalized_url) error 'Invalid URL format' say 'Please enter a valid URL (e.g., http://localhost:3000 or https://api.example.com)', :yellow say '' end end |
#prompt_for_email ⇒ Object
Prompt for user email with validation
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 105 def prompt_for_email loop do email = ask('Your Email:').to_s.strip if email.empty? error 'Email cannot be empty' say '' next end unless valid_email?(email) error 'Invalid email format' say 'Please enter a valid email address (e.g., user@example.com)', :yellow say '' next end return email end end |
#valid_api_url?(url) ⇒ Boolean
Validate API URL format
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 81 def valid_api_url?(url) uri = URI.parse(url) # Must have http or https scheme return false unless uri.scheme.to_s.match?(/^https?$/) # Must have a host return false if uri.host.nil? || uri.host.empty? # Plain http may only target a loopback host (local dev). The API # token is sent as a Bearer header, so http to a remote host would # leak it in cleartext. uri.hostname strips IPv6 brackets ([::1]). return false if uri.scheme == 'http' && !Mysigner::Client::LOOPBACK_HOSTS.include?(uri.hostname.downcase) # Valid formats: # - http://localhost:3000 # - https://api.example.com true rescue URI::InvalidURIError false end |
#valid_email?(email) ⇒ Boolean
Basic email validation
127 128 129 130 131 132 |
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 127 def valid_email?(email) # Simple regex for basic email validation # Format: local@domain.tld email_regex = /\A[^@\s]+@[^@\s]+\.[^@\s]+\z/ email.match?(email_regex) end |