Class: TrackerApi::Client

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

Constant Summary collapse

USER_AGENT =
"Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM}; #{RUBY_ENGINE}) TrackerApi/#{TrackerApi::VERSION} Faraday/#{Faraday::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create Pivotal Tracker API client.

Examples:

Creating a Client

Client.new token: 'my-super-special-token'

Parameters:

  • options (Hash) (defaults to: {})

    the connection options

Options Hash (options):

  • :token (String)

    API token to use for requests

  • :url (String)

    Main HTTP API root

  • :api_version (String)

    The API version URL path

  • :logger (String)

    Custom logger

  • :adapter (String)

    Custom http adapter to configure Faraday with

  • :connection_options (String)

    Connection options to pass to Faraday



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
# File 'lib/tracker_api/client.rb', line 19

def initialize(options={})
  url  = options[:url] || 'https://www.pivotaltracker.com'
  @url = URI.parse(url).to_s

  @api_version       = options[:api_version] || '/services/v5'
  @logger            = options[:logger] || Logger.new(nil)
  adapter            = options[:adapter] || :net_http
  connection_options = options[:connection_options] || { ssl: { verify: true } }
  @token             = options[:token]

  raise 'Missing required options: :token' unless @token

  @connection = Faraday.new({ url: @url }.merge(connection_options)) do |builder|
    # response
    builder.use Faraday::Response::RaiseError
    builder.response :json

    # request
    builder.request :multipart
    builder.request :json

    builder.use TrackerApi::Logger, @logger
    builder.adapter adapter
  end
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



5
6
7
# File 'lib/tracker_api/client.rb', line 5

def api_version
  @api_version
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/tracker_api/client.rb', line 5

def connection
  @connection
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/tracker_api/client.rb', line 5

def logger
  @logger
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/tracker_api/client.rb', line 5

def token
  @token
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/tracker_api/client.rb', line 5

def url
  @url
end

Instance Method Details

#project(id, params = {}) ⇒ Object



67
68
69
# File 'lib/tracker_api/client.rb', line 67

def project(id, params={})
  Endpoints::Project.new(self).get(id, params)
end

#projects(params = {}) ⇒ Object



63
64
65
# File 'lib/tracker_api/client.rb', line 63

def projects(params={})
  Endpoints::Projects.new(self).get(params)
end

#request(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tracker_api/client.rb', line 45

def request(options={})
  method  = options[:method] || :get
  url     = options[:url] || File.join(@url, @api_version, options[:path])
  token   = options[:token] || @token
  params  = options[:params] || {}
  body    = options[:body]
  headers = { 'User-Agent' => USER_AGENT, 'X-TrackerToken' => token }.merge(options[:headers] || {})

  connection.send(method) do |req|
    req.url url
    req.headers.merge!(headers)
    req.params.merge!(params)
    req.body = body
  end
rescue Faraday::Error::ClientError => e
  raise TrackerApi::Error.new(e)
end