Class: Gemnasium::Connection

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

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



5
6
7
8
# File 'lib/gemnasium/connection.rb', line 5

def initialize
  @connection = Net::HTTP.new(Gemnasium.config.site, Gemnasium.config.use_ssl ? 443 : 80)
  @connection.use_ssl = Gemnasium.config.use_ssl
end

Instance Method Details

#api_path_for(item) ⇒ String

Set the API path for a specific item

Parameters:

  • item (String)

    item the route should point to

Returns:

  • (String)

    API path



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gemnasium/connection.rb', line 27

def api_path_for item
  base = "/api/#{Gemnasium.config.api_version}"

  case item
  when 'base'
    base
  when 'projects'
    "#{base}/projects"
  when 'dependency_files'
    "#{base}/projects/#{Gemnasium.config.project_slug}/dependency_files"
  else
    raise "No API path found for #{item}"
  end
end

#get(path, headers = {}) ⇒ Object



17
18
19
20
21
# File 'lib/gemnasium/connection.rb', line 17

def get(path, headers = {})
  request = Net::HTTP::Get.new(path, headers.merge('Accept' => 'application/json', 'Content-Type' => 'application/json'))
  request.basic_auth('X', Gemnasium.config.api_key)
  @connection.request(request)
end

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



10
11
12
13
14
15
# File 'lib/gemnasium/connection.rb', line 10

def post(path, body, headers = {})
  request = Net::HTTP::Post.new(path, headers.merge('Accept' => 'application/json', 'Content-Type' => 'application/json'))
  request.basic_auth('X', Gemnasium.config.api_key)
  request.body = body
  @connection.request(request)
end