Module: API

Included in:
Bitbucket, OctopusApi, Project, Repository, TeamcityApi
Defined in:
lib/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



2
3
4
# File 'lib/api.rb', line 2

def api
  @api
end

Instance Method Details

#create_api(config) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/api.rb', line 4

def create_api(config)
  @api = Faraday.new(url: config.url) do |connection|
    connection.ssl[:verify] = false
    connection.adapter :net_http
    if config.api_key
      connection.headers['X-Octopus-ApiKey'] = config.api_key
    else
      connection.basic_auth(config.user, config.pass)
    end
  end
end

#parsed_response(resp) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/api.rb', line 38

def parsed_response(resp)
  if resp.headers['content-type'] =~ /application\/json/
    JSON.parse(resp.body)
  else
    resp
  end
end

#request(method, url, query = nil, type = "json", parse = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/api.rb', line 16

def request(method, url, query=nil, type="json", parse=true)
  if query
    Log.warn "request url: #{method.upcase} #{api.url_prefix}#{url}"
    Log.info "request body: #{query}"
    response = api.send(method) do |request|
      request.url url
      request.body = query
      request.headers['Content-Type'] = "application/#{type}"
    end
  else
    Log.warn "request url: #{method.upcase} #{api.url_prefix}#{url}"
    response = api.send(method) do |request|
      request.url url
      request.headers['Content-Type'] = "application/#{type}"
    end
  end

  Log.warn "response code: #{response.status}"
  Log.info "response body: #{response.body}"
  parse ? parsed_response(response) : response
end