lib/stream_elements/client.rb in streamelements-0.2.0 vs lib/stream_elements/client.rb in streamelements-0.5.0
- old
+ new
@@ -1,75 +1,48 @@
module StreamElements
class Client
- class << self
+ BASE_URL = "https://api.streamelements.com/kappa/v2"
- def connection
- @connection ||= Faraday.new("https://api.streamelements.com/kappa/v2") do |conn|
- conn.request :authorization, :Bearer, StreamElements.config.token
+ attr_reader :access_token
- conn.headers = {
- "User-Agent" => "streamelements/v#{VERSION} (github.com/deanpcmad/streamelements)"
- }
+ def initialize(access_token:)
+ @access_token = access_token
+ end
- conn.request :json
+ def channels
+ ChannelsResource.new(self)
+ end
- conn.response :json, content_type: "application/json"
- end
- end
+ def users
+ UsersResource.new(self)
+ end
+ def activities
+ ActivitiesResource.new(self)
+ end
- def get_request(url, params: {}, headers: {})
- handle_response connection.get(replace_channel(url), params, headers)
- end
+ def tips
+ TipsResource.new(self)
+ end
- def post_request(url, body: {}, headers: {})
- handle_response connection.post(replace_channel(url), body, headers)
- end
+ def song_requests
+ SongRequestsResource.new(self)
+ end
- def put_request(url, body: {}, headers: {})
- handle_response connection.put(replace_channel(url), body, headers)
- end
- def patch_request(url, body:, headers: {})
- handle_response connection.patch(replace_channel(url), body, headers)
- end
+ def connection
+ @connection ||= Faraday.new(BASE_URL) do |conn|
+ conn.request :authorization, :Bearer, access_token
- def delete_request(url, headers: {})
- handle_response connection.delete(replace_channel(url), headers)
- end
+ conn.headers = {
+ "User-Agent" => "streamelements/v#{VERSION} (github.com/deanpcmad/streamelements)"
+ }
- def replace_channel(url)
- url.gsub(":channel", StreamElements.config.channel)
- end
+ conn.request :json
- def handle_response(response)
- case response.status
- when 400
- raise Error, "Error 400: Your request was malformed. '#{response.body["error"]}'"
- when 401
- raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'"
- when 403
- raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]}'"
- when 404
- raise Error, "Error 404: No results were found for your request. '#{response.body["error"]}'"
- when 409
- raise Error, "Error 409: Your request was a conflict. '#{response.body["error"]}'"
- when 429
- raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
- when 500
- raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
- when 503
- raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
- when 501
- raise Error, "Error 501: This resource has not been implemented. '#{response.body["error"]}'"
- when 204
- return true
- end
-
- response
+ conn.response :json, content_type: "application/json"
end
-
end
end
end