Class: Abt::Providers::Asana::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/abt/providers/asana/api.rb

Constant Summary collapse

API_ENDPOINT =
"https://app.asana.com/api/1.0"
VERBS =
[:get, :post, :put].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:) ⇒ Api

Returns a new instance of Api.



12
13
14
# File 'lib/abt/providers/asana/api.rb', line 12

def initialize(access_token:)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/abt/providers/asana/api.rb', line 10

def access_token
  @access_token
end

Instance Method Details

#connectionObject



48
49
50
51
52
53
# File 'lib/abt/providers/asana/api.rb', line 48

def connection
  @connection ||= Faraday.new(API_ENDPOINT) do |connection|
    connection.headers["Authorization"] = "Bearer #{access_token}"
    connection.headers["Content-Type"] = "application/json"
  end
end

#get_paged(path, query = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/abt/providers/asana/api.rb', line 22

def get_paged(path, query = {})
  records = []

  loop do
    result = request(:get, path, query.merge(limit: 100))
    records += result["data"]
    break if result["next_page"].nil?

    path = result["next_page"]["path"][1..-1]
  end

  records
end

#request(*args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/abt/providers/asana/api.rb', line 36

def request(*args)
  response = connection.public_send(*args)

  if response.success?
    Oj.load(response.body)
  else
    error_class = Abt::HttpError.error_class_for_status(response.status)
    encoded_response_body = response.body.force_encoding("utf-8")
    raise error_class, "Code: #{response.status}, body: #{encoded_response_body}"
  end
end