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: '',
  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:



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

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.



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

def connection
  @connection
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#base_urlObject



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

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

#build_credentialsObject



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

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

#check_options!Object



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

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



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

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



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

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