Class: TrackerApi::Client

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

Defined Under Namespace

Classes: Pagination

Constant Summary collapse

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

Header keys that can be passed in options hash to #get,#paginate

Set.new([:accept, :content_type])

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

  • :auto_paginate (Boolean)

    Client should perform pagination automatically. Default true.

  • :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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tracker_api/client.rb', line 23

def initialize(options={})
  url                = options.fetch(:url, 'https://www.pivotaltracker.com')
  @url               = Addressable::URI.parse(url).to_s
  @api_version       = options.fetch(:api_version, '/services/v5')
  @logger            = options.fetch(:logger, ::Logger.new(nil))
  adapter            = options.fetch(:adapter, :net_http)
  connection_options = options.fetch(:connection_options, { ssl: { verify: true } })
  @auto_paginate     = options.fetch(:auto_paginate, 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 (readonly)

Returns the value of attribute api_version.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def api_version
  @api_version
end

#auto_paginateObject (readonly)

Returns the value of attribute auto_paginate.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def auto_paginate
  @auto_paginate
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def connection
  @connection
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def last_response
  @last_response
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/tracker_api/client.rb', line 8

def url
  @url
end

Instance Method Details

#get(path, options = {}) ⇒ Faraday::Response

Make a HTTP GET request

Parameters:

  • path (String)

    The path, relative to api endpoint

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

    Query and header params for request

Returns:

  • (Faraday::Response)


53
54
55
# File 'lib/tracker_api/client.rb', line 53

def get(path, options = {})
  request(:get, parse_query_and_convenience_headers(path, options))
end

#meTrackerApi::Resources::Me

Get information about the authenticated user



122
123
124
# File 'lib/tracker_api/client.rb', line 122

def me
  Endpoints::Me.new(self).get
end

#paginate(path, options = {}, &block) ⇒ Array

Make one or more HTTP GET requests, optionally fetching the next page of results from information passed back in headers based on value in #auto_paginate.

Parameters:

  • path (String)

    The path, relative to #api_endpoint

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

    Query and header params for request

  • block (Block)

    Block to perform the data concatenation of the multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.

Returns:

  • (Array)

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tracker_api/client.rb', line 77

def paginate(path, options = {}, &block)
  opts           = parse_query_and_convenience_headers path, options.dup
  auto_paginate  = opts[:params].delete(:auto_paginate) { |k| @auto_paginate }
  @last_response = request :get, opts
  data           = @last_response.body
  raise TrackerApi::Errors::UnexpectedData, 'Array expected' unless data.is_a? Array

  if auto_paginate
    pager = Pagination.new @last_response.headers

    while pager.more?
      opts[:params].update(pager.next_page_params)

      @last_response = request :get, opts
      pager          = Pagination.new @last_response.headers
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.body) if @last_response.body.is_a?(Array)
      end
    end
  end

  data
end

#post(path, options = {}) ⇒ Faraday::Response

Make a HTTP POST request

Parameters:

  • path (String)

    The path, relative to api endpoint

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

    Query and header params for request

Returns:

  • (Faraday::Response)


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

def post(path, options = {})
  request(:post, parse_query_and_convenience_headers(path, options))
end

#project(id, params = {}) ⇒ TrackerApi::Resources::Project

Get project

Parameters:

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

Returns:



115
116
117
# File 'lib/tracker_api/client.rb', line 115

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

#projects(params = {}) ⇒ Array[TrackerApi::Resources::Project]

Get projects

Parameters:

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

Returns:



107
108
109
# File 'lib/tracker_api/client.rb', line 107

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

#story(story_id) ⇒ TrackerApi::Resources::Story

Get information about a client story without knowing what project the story belongs to

Parameters:

  • story_id (String)

Returns:



130
131
132
# File 'lib/tracker_api/client.rb', line 130

def story(story_id)
  Endpoints::Story.new(self).get_story(story_id)
end