Class: Rubygems::API::Client

Inherits:
Object
  • Object
show all
Includes:
Activities, GemOwners, Gems, Utilities, Webhooks
Defined in:
lib/rubygems_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Webhooks

#fire_webhook, #register_webhook, #remove_webhook, #view_webhooks

Methods included from Gems

#add_gem_owner, #gem_dependencies, #gem_downloads, #gem_info, #gem_owners, #gem_search, #gem_versions, #gems_by_owner, #my_gems, #remove_gem_owner, #submit_gem, #unyank_gem, #yank_gem

Methods included from GemOwners

#add_gem_owner, #gem_owners, #gems_by_owner, #remove_gem_owner

Methods included from Activities

#just_updated, #latest_activity, #rubygems_total_downloads

Methods included from Utilities

#format_body, #format_json_body, #format_yaml_body, #get, #ssl, #validate_format, #yank_api

Constructor Details

#initialize(arguments = {}) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubygems_api/client.rb', line 21

def initialize(arguments = {})
  @api_key ||= arguments[:api_key] || \
               nil

  @host = 'https://rubygems.org/'
  @api_endpoint = '/api/v1'

  @client = Hurley::Client.new 'https://rubygems.org/api/v1/'
  ssl(arguments)
  @client.request_options.redirection_limit = 10

  @client.header[:Authorization] = @api_key unless @api_key.nil?

  @client
end

Instance Attribute Details

#api_key(key = nil) ⇒ Object

Returns the value of attribute api_key.



19
20
21
# File 'lib/rubygems_api/client.rb', line 19

def api_key
  @api_key
end

#clientObject

Returns the value of attribute client.



19
20
21
# File 'lib/rubygems_api/client.rb', line 19

def client
  @client
end

Instance Method Details

#set_api_key(username, password, format = 'json', args = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubygems_api/client.rb', line 44

def set_api_key(username, password, format = 'json', args = {})
  creds = CGI.escape(username) + ':' + CGI.escape(password)
  if validate_format(format)
    response = @client.get("https://#{creds}@rubygems.org/api/v1/api_key.#{format}") do |req|
      req.header[:Authorization] = req.url.basic_auth.gsub!(/(\n|\r|\t)+/, '')
    end

    if response.success?
      format_body response: response, skip_format: args[:skip_format],
                  format: format

      api_key(response.body['rubygems_api_key'])
    end
  end

  response
end