Class: BugherdClient::Client
- Inherits:
-
Object
- Object
- BugherdClient::Client
- Defined in:
- lib/bugherd_client/client.rb
Constant Summary collapse
- API_VERSIONS =
[1,2].freeze
- RESOURCE_NAMES =
[ :organization, :user, :project, :task, :comment, :webhook, :attachment ]
- DEFAULT_OPTIONS =
{ base_url: 'https://www.bugherd.com', api_version: 2, username: nil, password: nil, api_key: nil, api_rate_limiting_token: 'x', debug: false }.freeze
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(opts = {}, &block) ⇒ Client
constructor
A new instance of Client.
- #resource(name) ⇒ Object
Constructor Details
#initialize(opts = {}, &block) ⇒ Client
Returns a new instance of Client.
36 37 38 39 40 41 42 43 |
# File 'lib/bugherd_client/client.rb', line 36 def initialize(opts={}, &block) mutex = Monitor.new mutex.synchronize do @options = DEFAULT_OPTIONS.merge(opts) yield(self) if block_given? establish_connection! end end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
29 30 31 |
# File 'lib/bugherd_client/client.rb', line 29 def connection @connection end |
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/bugherd_client/client.rb', line 29 def @options end |
Instance Method Details
#base_url ⇒ Object
58 59 60 |
# File 'lib/bugherd_client/client.rb', line 58 def base_url File.join([:base_url], "api_v#{[:api_version]}") end |
#build_credentials ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/bugherd_client/client.rb', line 71 def build_credentials if @options[:api_key].present? [@options[:api_key], @options[:api_rate_limiting_token]] else [@options[:username], @options[:password]] end end |
#check_options! ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/bugherd_client/client.rb', line 62 def if !@options[:api_key] && !(@options[:username] && @options[:password]) raise BugherdClient::Errors::InvalidOption, 'api_key or username and password is required' end unless API_VERSIONS.include?(@options[:api_version]) raise(BugherdClient::Errors::InvalidOption.new("api_version must be #{API_VERSIONS.join(',')}")) end end |
#establish_connection! ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bugherd_client/client.rb', line 45 def establish_connection! username, password = build_credentials if @options[:debug] RestClient.log = ::Logger.new($stderr) RestClient.log.level = ::Logger::DEBUG end resource_klass = Class.new(RestClient::Resource) self.connection = resource_klass.new(base_url, user: username, password: password) end |
#resource(name) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bugherd_client/client.rb', line 79 def resource(name) version = self.[:api_version] version_base = "BugherdClient::Resources::V#{version}".constantize klass_name = name.to_s.classify.to_sym unless version_base.constants.include?(klass_name) raise(BugherdClient::Errors::NotAvailable.new(version, klass_name)) end klass = version_base.const_get(klass_name) klass.new(self.connection, @options) end |