Class: BugherdClient::Client
- Inherits:
-
Object
- Object
- BugherdClient::Client
- 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
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #base_url ⇒ Object
- #build_credentials ⇒ Object
- #check_options! ⇒ Object
- #establish_connection! ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
- #resource(name, opts = {}) ⇒ Object
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 |
# File 'lib/bugherd_client/client.rb', line 19 def initialize(={}, &block) @options = DEFAULT_OPTIONS.merge() yield(self) if block_given? establish_connection! end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
17 18 19 |
# File 'lib/bugherd_client/client.rb', line 17 def connection @connection end |
#options ⇒ Object
Returns the value of attribute options.
17 18 19 |
# File 'lib/bugherd_client/client.rb', line 17 def @options end |
Instance Method Details
#base_url ⇒ Object
38 39 40 |
# File 'lib/bugherd_client/client.rb', line 38 def base_url File.join([:base_url], "api_v#{[:api_version]}") end |
#build_credentials ⇒ Object
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 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! 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.[:api_version] klass = Object.const_get("BugherdClient::Resources::V#{version}::#{name.to_s.classify}") klass.new(self.connection, self.) end |