Module: Mysigner::CLI::Concerns::ApiHelpers

Included in:
Mysigner::CLI
Defined in:
lib/mysigner/cli/concerns/api_helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_api_urlObject

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

Returns:

  • (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
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 64

def normalize_api_url(url)
  # Add http:// if no protocol specified
  url = "http://#{url}" unless url.match?(%r{^https?://})

  # Remove trailing slash
  url.chomp('/')
end

#prompt_api_urlObject

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_emailObject

Prompt for user email with validation



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 92

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

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 73

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?

  # Valid formats:
  # - http://localhost:3000
  # - https://api.example.com
  # - http://192.168.1.1:8080
  true
rescue URI::InvalidURIError
  false
end

#valid_email?(email) ⇒ Boolean

Basic email validation

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/mysigner/cli/concerns/api_helpers.rb', line 114

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