Module: Capsolver::Utils

Defined in:
lib/capsolver/utils.rb

Class Method Summary collapse

Class Method Details

.camelize_key(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capsolver/utils.rb', line 21

def camelize_key(str)
  case str
  when "website_url" then "websiteURL"
  when "website_key" then "websiteKey"
  when "aws_key" then "awsKey"
  when "aws_challenge_js" then "awsChallengeJS"
  when "client_key" then "clientKey"
  when "task_id" then "taskId"
  when "callback_url" then "callbackUrl"
  when "app_id" then "appId"
  when "error_id" then "errorId"
  when "error_code" then "errorCode"
  when "error_description" then "errorDescription"
  when "captcha_url" then "captchaUrl"
  when "user_agent" then "userAgent"
  else
    # Default snake_case to camelCase (e.g., foo_bar_baz -> fooBarBaz)
    str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
  end
end

.camelize_keys(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/capsolver/utils.rb', line 7

def camelize_keys(value)
  case value
  when Hash
    value.each_with_object({}) do |(key, val), memo|
      new_key = camelize_key(key.to_s)
      memo[new_key] = camelize_keys(val)
    end
  when Array
    value.map { |item| camelize_keys(item) }
  else
    value
  end
end

.normalize_response(value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/capsolver/utils.rb', line 42

def normalize_response(value)
  case value
  when Hash
    value.each_with_object({}) do |(key, val), memo|
      norm_val = normalize_response(val)
      
      # Compute key variations
      snake = snakeize_key(key.to_s)
      camel = camelize_key(snake)

      memo[snake.to_sym] = norm_val
      memo[snake] = norm_val
      memo[camel.to_sym] = norm_val
      memo[camel] = norm_val
    end
  when Array
    value.map { |item| normalize_response(item) }
  else
    value
  end
end

.snakeize_key(str) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/capsolver/utils.rb', line 64

def snakeize_key(str)
  # Handlers for specific replacements
  str = str.gsub("websiteURL", "website_url")
  str = str.gsub("awsChallengeJS", "aws_challenge_js")
  
  # Convert standard camelCase to snake_case
  str.gsub(/([A-Z])/) { "_#{Regexp.last_match(1).downcase}" }.gsub(/^_/, "")
end