Class: OctocatHerder::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/octocat_herder/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.

Raises:

  • (ArgumentError)


15
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
# File 'lib/octocat_herder/connection.rb', line 15

def initialize(options={})
  raise ArgumentError.new(
    "OctocatHerder::Connection does not accept: #{options.class}"
  ) unless options.is_a? Hash

  options.keys.each do |k|
    raise ArgumentError.new("Unknown option: '#{k}'") unless [
      :user_name, :password, :oauth2_token
    ].include? k
  end

  if options.keys.include?(:user_name) or options.keys.include?(:password)
    raise ArgumentError.new("When providing :user_name or :password, both are required") unless
      options.keys.include?(:user_name) and options.keys.include?(:password)
  end

  if options.keys.include?(:oauth2_token) and options.keys.include?(:user_name)
    raise ArgumentError.new('Cannot provide both an OAuth2 Token, and a user name and password')
  end

  @user_name    = options[:user_name]
  @password     = options[:password]
  @oauth2_token = options[:oauth2_token]

  if oauth2_token
    @httparty_options = { :headers => { 'Authorization' => "token #{oauth2_token}" } }
  elsif user_name
    @httparty_options = { :basic_auth => { :username => user_name, :password => password } }
  end
end

Instance Attribute Details

#oauth2_tokenObject (readonly)

Returns the value of attribute oauth2_token.



9
10
11
# File 'lib/octocat_herder/connection.rb', line 9

def oauth2_token
  @oauth2_token
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/octocat_herder/connection.rb', line 9

def password
  @password
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



9
10
11
# File 'lib/octocat_herder/connection.rb', line 9

def user_name
  @user_name
end

Instance Method Details

#get(end_point, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/octocat_herder/connection.rb', line 46

def get(end_point, options={})
  request_options = options.merge(httparty_options)
  if httparty_options.has_key?(:headers) and options.has_key(:headers)
    request_options[:headers] = options[:headers].merge(httparty_options[:headers])
  end

  OctocatHerder::Connection.get(end_point, request_options)
end

#httparty_optionsObject



11
12
13
# File 'lib/octocat_herder/connection.rb', line 11

def httparty_options
  @httparty_options || {}
end