Class: HumanTone::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/humantone/client.rb

Constant Summary collapse

API_KEY_REGEX =
/\Aht_[0-9a-f]{64}\z/
DEFAULT_BASE_URL =
'https://api.humantone.io'
DEFAULT_TIMEOUT_SECONDS =
120
DEFAULT_MAX_RETRIES =
2
HUMANIZE_OUTPUT_FORMATS =
%i[text html markdown].freeze
HUMANIZE_DECODER =
lambda do |body, request_id|
  Coercion.optional_string(body[:request_id], 'request_id')
  HumanTone::HumanizeResult.new(
    text: Coercion.string!(body[:content], 'content'),
    output_format: Coercion.enum_symbol!(body[:output_format], 'output_format', HUMANIZE_OUTPUT_FORMATS),
    credits_used: Coercion.integer!(body[:credits_used], 'credits_used'),
    request_id: request_id
  )
end
DETECT_DECODER =
lambda do |body, request_id|
  Coercion.optional_string(body[:request_id], 'request_id')
  HumanTone::DetectResult.new(
    ai_score: Coercion.integer_in_range!(body[:ai_score], 'ai_score', 0..100),
    request_id: request_id
  )
end

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT_SECONDS, max_retries: DEFAULT_MAX_RETRIES, retry_on_post: false, user_agent: nil, logger: nil) ⇒ Client

Returns a new instance of Client.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/humantone/client.rb', line 41

def initialize(api_key: nil,
               base_url: nil,
               timeout: DEFAULT_TIMEOUT_SECONDS,
               max_retries: DEFAULT_MAX_RETRIES,
               retry_on_post: false,
               user_agent: nil,
               logger: nil)
  @api_key = resolve_api_key(api_key)
  validate_api_key!(@api_key)

  @base_url = resolve_base_url(base_url)
  @logger = logger || Logger.new(IO::NULL)

  @http = Http.new(
    base_url: @base_url,
    api_key: @api_key,
    timeout: timeout,
    user_agent_suffix: user_agent,
    logger: @logger
  )
  @retry_layer = Retry.new(
    max_retries: max_retries,
    retry_on_post: retry_on_post,
    logger: @logger
  )
end

Instance Method Details

#accountObject



99
100
101
# File 'lib/humantone/client.rb', line 99

def 
  @account ||= Account.new(http: @http, retry_layer: @retry_layer)
end

#detect(text:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/humantone/client.rb', line 86

def detect(text:)
  payload = { content: text }

  @retry_layer.call(method: :post, endpoint: :detect) do
    raw = @http.request(method: :post, path: '/v1/detect', body: JSON.generate(payload))
    ErrorParser.parse(
      endpoint: :detect,
      status: raw[:status], headers: raw[:headers], body_string: raw[:body_string],
      decoder: DETECT_DECODER
    )
  end
end

#humanize(text:, level: :standard, output_format: :text, custom_instructions: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/humantone/client.rb', line 68

def humanize(text:, level: :standard, output_format: :text, custom_instructions: nil)
  payload = {
    content: text,
    humanization_level: enum_to_string(level),
    output_format: enum_to_string(output_format)
  }
  payload[:custom_instructions] = custom_instructions unless custom_instructions.nil?

  @retry_layer.call(method: :post, endpoint: :humanize) do
    raw = @http.request(method: :post, path: '/v1/humanize', body: JSON.generate(payload))
    ErrorParser.parse(
      endpoint: :humanize,
      status: raw[:status], headers: raw[:headers], body_string: raw[:body_string],
      decoder: HUMANIZE_DECODER
    )
  end
end