Class: BugherdClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bugherd_client/client.rb

Constant Summary collapse

VALID_API_VERSIONS =
[1,2].freeze
DEFAULT_OPTIONS =
{
  base_url: 'https://www.bugherd.com',
  api_version: 2,
  username: '',
  password: '',
  api_key: '',
  api_rate_limiting_token: 'x',
  debug: false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
# File 'lib/bugherd_client/client.rb', line 20

def initialize(options={}, &block)
  @options = DEFAULT_OPTIONS.merge(options)
  yield(self) if block_given?
  establish_connection!
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



18
19
20
# File 'lib/bugherd_client/client.rb', line 18

def connection
  @connection
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/bugherd_client/client.rb', line 18

def options
  @options
end

Instance Method Details

#base_urlObject



39
40
41
# File 'lib/bugherd_client/client.rb', line 39

def base_url
  File.join(options[:base_url], "api_v#{options[:api_version]}")
end

#build_credentialsObject



52
53
54
55
56
57
58
# File 'lib/bugherd_client/client.rb', line 52

def build_credentials
  if @options[:api_key]
    [@options[:api_key], @options[:api_rate_limiting_token]]
  else
    [@options[:username], @options[:password]]
  end
end

#check_options!Object



43
44
45
46
47
48
49
50
# File 'lib/bugherd_client/client.rb', line 43

def check_options!
  if !@options[:api_key] && !(@options[:username] && @options[:password])
    raise BugherdClient::Errors::InvalidOption, "api_key or username and password is required"
  end
  unless VALID_API_VERSIONS.include?(@options[:api_version])
    raise BugherdClient::Errors::InvalidOption, "api_version must be #{VALID_API_VERSIONS.join(',')}"
  end
end

#establish_connection!Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bugherd_client/client.rb', line 26

def establish_connection!
  check_options!
  username, password = build_credentials
  
  if @options[:debug]
    RestClient.log        = ::Logger.new($stderr)
    RestClient.log.level  = ::Logger::DEBUG
  end

  self.connection = RestClient::Resource.new(base_url, user: username, password: password)
  
end

#resource(name, opts = {}) ⇒ Object



60
61
62
63
64
# File 'lib/bugherd_client/client.rb', line 60

def resource(name, opts={})
  version = self.options[:api_version]
  klass = "BugherdClient::Resources::V#{version}::#{name.to_s.classify}".constantize
  klass.new(self.connection, self.options)
end