Class: Build::Api

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

Constant Summary collapse

API_PATH =
"https://api.readme.build/v1/"
HTTP_OPEN_TIMEOUT =
5

Instance Method Summary collapse

Instance Method Details

#config(api_key, password = '') ⇒ Object



11
12
13
14
# File 'lib/api.rb', line 11

def config(api_key, password='')
  @api_key = api_key
  @password = ''
end

#run(service, method, data = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api.rb', line 16

def run(service, method, data={})
  begin
    organization, service, version_override = generate_api_url_parts(service)

    if organization.nil?
      service_full = "/#{service}"
    else
      service_full = "/@#{organization}/#{service}"
    end

    path = "#{API_PATH}run#{service_full}/#{method}"
    url = URI(path)
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.open_timeout = HTTP_OPEN_TIMEOUT

    request = Net::HTTP::Post.new(url)
    request = prepare_headers(request, version_override)
    request.basic_auth @api_key, @password
    request.body = data.to_json
    response = http.request(request)

    case response.code
      when '200'
        return response.body
      when '404', '400'
        return JSON.parse(response.body)
      else
        return response.body
    end
  rescue Exception => e
    return e.message
  end
end