Class: AnsibleGalaxy::API

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

Overview

Wrapper for Galaxy HTTP API

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



12
13
14
15
16
# File 'lib/ansible_galaxy/api.rb', line 12

def initialize(options = {})
  username = options.fetch 'username', ENV['ANSIBLE_GALAXY_USERNAME']
  password = options.fetch 'password', ENV['ANSIBLE_GALAXY_PASSWORD']
  @auth = "#{username}:#{password}"
end

Instance Method Details

#requestObject

Contract None => Hash NoContractBecause: Contracts break __callee__ by changing method names.



20
21
22
23
24
25
26
27
# File 'lib/ansible_galaxy/api.rb', line 20

def request
  endpoint = "https://galaxy.ansible.com/api/v1/#{__callee__}/"
  request = Typhoeus::Request.new(endpoint, userpwd: @auth)
  request.run
  JSON.parse(request.response.body).tap do |response|
    fail AuthenticationError, response['detail'] if response['detail']
  end
end