File: client.rb

package info (click to toggle)
ruby-asana 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 384 kB
  • ctags: 289
  • sloc: ruby: 1,361; makefile: 3
file content (124 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require_relative 'authentication'
require_relative 'client/configuration'
require_relative 'resources'

module Asana
  # Public: A client to interact with the Asana API. It exposes all the
  # available resources of the Asana API in idiomatic Ruby.
  #
  # Examples
  #
  #   # Authentication with a personal access token
  #   Asana::Client.new do |client|
  #     client.authentication :access_token, '...'
  #   end
  #
  #   # OAuth2 with a plain bearer token (doesn't support auto-refresh)
  #   Asana::Client.new do |client|
  #     client.authentication :oauth2, bearer_token: '...'
  #   end
  #
  #   # OAuth2 with a plain refresh token and client credentials
  #   Asana::Client.new do |client|
  #     client.authentication :oauth2,
  #                           refresh_token: '...',
  #                           client_id: '...',
  #                           client_secret: '...',
  #                           redirect_uri: '...'
  #   end
  #
  #   # OAuth2 with an ::OAuth2::AccessToken object
  #   Asana::Client.new do |client|
  #     client.authentication :oauth2, my_oauth2_access_token_object
  #   end
  #
  #   # Use a custom Faraday network adapter
  #   Asana::Client.new do |client|
  #     client.authentication ...
  #     client.adapter :typhoeus
  #   end
  #
  #   # Use a custom user agent string
  #   Asana::Client.new do |client|
  #     client.authentication ...
  #     client.user_agent '...'
  #   end
  #
  #   # Pass in custom configuration to the Faraday connection
  #   Asana::Client.new do |client|
  #     client.authentication ...
  #     client.configure_faraday { |conn| conn.use MyMiddleware }
  #   end
  #
  class Client
    # Internal: Proxies Resource classes to implement a fluent API on the Client
    # instances.
    class ResourceProxy
      def initialize(client: required('client'), resource: required('resource'))
        @client   = client
        @resource = resource
      end

      def method_missing(m, *args, &block)
        @resource.public_send(m, *([@client] + args), &block)
      end

      def respond_to_missing?(m, *)
        @resource.respond_to?(m)
      end
    end

    # Public: Initializes a new client.
    #
    # Yields a {Asana::Client::Configuration} object as a configuration
    # DSL. See {Asana::Client} for usage examples.
    def initialize
      config = Configuration.new.tap { |c| yield c }.to_h
      @http_client =
        HttpClient.new(authentication: config.fetch(:authentication),
                       adapter:        config[:faraday_adapter],
                       user_agent:     config[:user_agent],
                       debug_mode:     config[:debug_mode],
                       &config[:faraday_configuration])
    end

    # Public: Performs a GET request against an arbitrary Asana URL. Allows for
    # the user to interact with the API in ways that haven't been
    # reflected/foreseen in this library.
    def get(url, *args)
      @http_client.get(url, *args)
    end

    # Public: Performs a POST request against an arbitrary Asana URL. Allows for
    # the user to interact with the API in ways that haven't been
    # reflected/foreseen in this library.
    def post(url, *args)
      @http_client.post(url, *args)
    end

    # Public: Performs a PUT request against an arbitrary Asana URL. Allows for
    # the user to interact with the API in ways that haven't been
    # reflected/foreseen in this library.
    def put(url, *args)
      @http_client.put(url, *args)
    end

    # Public: Performs a DELETE request against an arbitrary Asana URL. Allows
    # for the user to interact with the API in ways that haven't been
    # reflected/foreseen in this library.
    def delete(url, *args)
      @http_client.delete(url, *args)
    end

    # Public: Exposes queries for all top-evel endpoints.
    #
    # E.g. #users will query /users and return a
    # Asana::Resources::Collection<User>.
    Resources::Registry.resources.each do |resource_class|
      define_method(resource_class.plural_name) do
        ResourceProxy.new(client: @http_client,
                          resource: resource_class)
      end
    end
  end
end