Class: Esa::Client

Inherits:
Object
  • Object
show all
Includes:
ApiMethods
Defined in:
lib/esa/client.rb

Defined Under Namespace

Classes: TooManyRequestError

Constant Summary

Constants included from ApiMethods

ApiMethods::HTTP_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiMethods

#add_comment_star, #add_post_star, #add_watch, #append_post, #batch_move_category, #cancel_invitation, #categories, #comment, #comment_stargazers, #comments, #create_comment, #create_emoji, #create_post, #create_sharing, #delete_comment, #delete_comment_star, #delete_emoji, #delete_member, #delete_post, #delete_post_star, #delete_sharing, #delete_watch, #emojis, #invitation, #member, #members, #pending_invitations, #post, #post_stargazers, #posts, #regenerate_invitation, #send_invitation, #signed_url, #stats, #tags, #team, #teams, #update_comment, #update_post, #upload_attachment, #user, #watchers

Constructor Details

#initialize(access_token: nil, api_endpoint: nil, current_team: nil, default_headers: {}, retry_on_rate_limit_exceeded: true) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/esa/client.rb', line 11

def initialize(access_token: nil, api_endpoint: nil, current_team: nil, default_headers: {}, retry_on_rate_limit_exceeded: true)
  @access_token = access_token
  @api_endpoint = api_endpoint
  @current_team = current_team
  @default_headers = default_headers
  @retry_on_rate_limit_exceeded = retry_on_rate_limit_exceeded
end

Instance Attribute Details

#current_teamObject

Returns the value of attribute current_team.



18
19
20
# File 'lib/esa/client.rb', line 18

def current_team
  @current_team
end

#default_headersObject

Returns the value of attribute default_headers.



18
19
20
# File 'lib/esa/client.rb', line 18

def default_headers
  @default_headers
end

#retry_on_rate_limit_exceededObject

Returns the value of attribute retry_on_rate_limit_exceeded.



18
19
20
# File 'lib/esa/client.rb', line 18

def retry_on_rate_limit_exceeded
  @retry_on_rate_limit_exceeded
end

Instance Method Details

#current_team!Object



20
21
22
23
# File 'lib/esa/client.rb', line 20

def current_team!
  raise TeamNotSpecifiedError, "current_team is not specified" unless @current_team
  current_team
end

#esa_connectionObject



64
65
66
67
68
69
70
# File 'lib/esa/client.rb', line 64

def esa_connection
  @esa_connection ||= Faraday.new(faraday_options) do |c|
    c.request :json
    c.response :json
    c.adapter Faraday.default_adapter
  end
end

#s3_connectionObject



72
73
74
75
76
77
78
79
# File 'lib/esa/client.rb', line 72

def s3_connection
  @s3_connection ||= Faraday.new do |c|
    c.request :multipart
    c.request :url_encoded
    c.response :xml
    c.adapter Faraday.default_adapter
  end
end

#send_delete(path, params = nil, headers = nil) ⇒ Object



41
42
43
# File 'lib/esa/client.rb', line 41

def send_delete(path, params = nil, headers = nil)
  send_request(:delete, path, params, headers)
end

#send_get(path, params = nil, headers = nil) ⇒ Object



25
26
27
# File 'lib/esa/client.rb', line 25

def send_get(path, params = nil, headers = nil)
  send_request(:get, path, params, headers)
end

#send_patch(path, params = nil, headers = nil) ⇒ Object



37
38
39
# File 'lib/esa/client.rb', line 37

def send_patch(path, params = nil, headers = nil)
  send_request(:patch, path, params, headers)
end

#send_post(path, params = nil, headers = nil) ⇒ Object



29
30
31
# File 'lib/esa/client.rb', line 29

def send_post(path, params = nil, headers = nil)
  send_request(:post, path, params, headers)
end

#send_put(path, params = nil, headers = nil) ⇒ Object



33
34
35
# File 'lib/esa/client.rb', line 33

def send_put(path, params = nil, headers = nil)
  send_request(:put, path, params, headers)
end

#send_request(method, path, params = nil, headers = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/esa/client.rb', line 45

def send_request(method, path, params = nil, headers = nil)
  response = esa_connection.send(method, path, params, headers)
  raise TooManyRequestError if retry_on_rate_limit_exceeded && response.status == 429 # too_many_requests
  Esa::Response.new(response)
rescue TooManyRequestError
  wait_sec = response.headers['retry-after'].to_i + 5
  puts "Rate limit exceeded: will retry after #{wait_sec} seconds."
  wait_for(wait_sec)
  retry
end

#send_s3_request(method, path, params = nil, headers = nil) ⇒ Object



56
57
58
# File 'lib/esa/client.rb', line 56

def send_s3_request(method, path, params = nil, headers = nil)
  Esa::Response.new(s3_connection.send(method, path, params, headers))
end

#send_simple_request(method, path, params = nil, headers = nil) ⇒ Object



60
61
62
# File 'lib/esa/client.rb', line 60

def send_simple_request(method, path, params = nil, headers = nil)
  Esa::Response.new(simple_connection.send(method, path, params, headers))
end

#simple_connectionObject



81
82
83
84
85
# File 'lib/esa/client.rb', line 81

def simple_connection
  @simple_connection ||= Faraday.new do |c|
    c.adapter Faraday.default_adapter
  end
end