Class: CSTM2::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: 30) ⇒ Client

Returns a new instance of Client.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cstm2.rb', line 34

def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: 30)
  raise Error.new('API key is required — generate one at careerstudiomax.com/developer', 'missing_api_key', 0) if api_key.nil? || api_key.empty?

  @api_key = api_key
  @base_url = base_url.chomp('/')
  @timeout = timeout

  @careerlm     = CareerLM.new(self)
  @careerembed  = CareerEmbed.new(self)
  @careerscore  = CareerScore.new(self)
  @careeragent  = CareerAgent.new(self)
  @careervoice  = CareerVoice.new(self)
  @careervision = CareerVision.new(self)
  @careervideo  = CareerVideo.new(self)
  @careerwebapi = CareerWebAPI.new(self)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



32
33
34
# File 'lib/cstm2.rb', line 32

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



32
33
34
# File 'lib/cstm2.rb', line 32

def base_url
  @base_url
end

#careeragentObject (readonly)

Returns the value of attribute careeragent.



51
52
53
# File 'lib/cstm2.rb', line 51

def careeragent
  @careeragent
end

#careerembedObject (readonly)

Returns the value of attribute careerembed.



51
52
53
# File 'lib/cstm2.rb', line 51

def careerembed
  @careerembed
end

#careerlmObject (readonly)

Returns the value of attribute careerlm.



51
52
53
# File 'lib/cstm2.rb', line 51

def careerlm
  @careerlm
end

#careerscoreObject (readonly)

Returns the value of attribute careerscore.



51
52
53
# File 'lib/cstm2.rb', line 51

def careerscore
  @careerscore
end

#careervideoObject (readonly)

Returns the value of attribute careervideo.



51
52
53
# File 'lib/cstm2.rb', line 51

def careervideo
  @careervideo
end

#careervisionObject (readonly)

Returns the value of attribute careervision.



51
52
53
# File 'lib/cstm2.rb', line 51

def careervision
  @careervision
end

#careervoiceObject (readonly)

Returns the value of attribute careervoice.



51
52
53
# File 'lib/cstm2.rb', line 51

def careervoice
  @careervoice
end

#careerwebapiObject (readonly)

Returns the value of attribute careerwebapi.



51
52
53
# File 'lib/cstm2.rb', line 51

def careerwebapi
  @careerwebapi
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



32
33
34
# File 'lib/cstm2.rb', line 32

def timeout
  @timeout
end

Instance Method Details

#get(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/cstm2.rb', line 55

def get(path)
  request(Net::HTTP::Get, path)
end

#post(path, body = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
# File 'lib/cstm2.rb', line 60

def post(path, body = {})
  request(Net::HTTP::Post, path, body)
end

#stream(path, body = {}, &on_event) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cstm2.rb', line 65

def stream(path, body = {}, &on_event)
  uri = URI("#{@base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.read_timeout = @timeout

  req = Net::HTTP::Post.new(uri)
  req['Authorization'] = "Bearer #{@api_key}"
  req['Content-Type'] = 'application/json'
  req['Accept'] = 'text/event-stream'
  req.body = body.compact.to_json

  buffer = +''
  http.request(req) do |response|
    if response.code.to_i >= 400
      raise build_error(response.body, response.code.to_i)
    end

    response.read_body do |chunk|
      buffer << chunk
      while (idx = buffer.index("\n\n"))
        event_block = buffer.slice!(0..idx + 1)
        event_block.each_line do |line|
          next unless line.start_with?('data:')

          payload = line.sub('data:', '').strip
          next if payload.empty?

          begin
            on_event.call(JSON.parse(payload))
          rescue JSON::ParserError
            next
          end
        end
      end
    end
  end
  nil
rescue Net::OpenTimeout, Net::ReadTimeout
  raise Error.new('Stream request timed out', 'timeout', 504)
rescue Errno::ECONNREFUSED, SocketError => e
  raise Error.new("Could not reach the CSTM-2 API: #{e.message}", 'connection_error', 0)
end