From e611e739d7177fbe5ee404e8b77e00bb584721cc Mon Sep 17 00:00:00 2001 From: Jonathan B Date: Wed, 14 Aug 2019 21:39:18 +0200 Subject: Add require --- src/twitch/client.cr | 27 --------------------- src/twitch/mappings/converters.cr | 3 --- src/twitch/mappings/user.cr | 21 ---------------- src/twitch/rest.cr | 50 --------------------------------------- src/twitcr.cr | 3 ++- src/twitcr/client.cr | 27 +++++++++++++++++++++ src/twitcr/mappings/converters.cr | 3 +++ src/twitcr/mappings/user.cr | 21 ++++++++++++++++ src/twitcr/rest.cr | 50 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 103 insertions(+), 102 deletions(-) delete mode 100644 src/twitch/client.cr delete mode 100644 src/twitch/mappings/converters.cr delete mode 100644 src/twitch/mappings/user.cr delete mode 100644 src/twitch/rest.cr create mode 100644 src/twitcr/client.cr create mode 100644 src/twitcr/mappings/converters.cr create mode 100644 src/twitcr/mappings/user.cr create mode 100644 src/twitcr/rest.cr (limited to 'src') diff --git a/src/twitch/client.cr b/src/twitch/client.cr deleted file mode 100644 index 8502b90..0000000 --- a/src/twitch/client.cr +++ /dev/null @@ -1,27 +0,0 @@ -require "./rest" -require "./mappings/*" - -module Twitcr::Client - include REST - - getter token : String - getter client_id : String - - def initialize(@token, @client_id) - @token = "Bearer" + @token - end - - def user?(name : String) - true if get_user_by_login(name) - rescue EMPTY_RESULT - false - end - - def user(name : String) - get_user_by_login(name) - end - - def user(id : Int64) - get_user_by_id(id) - end -end diff --git a/src/twitch/mappings/converters.cr b/src/twitch/mappings/converters.cr deleted file mode 100644 index d9b57e9..0000000 --- a/src/twitch/mappings/converters.cr +++ /dev/null @@ -1,3 +0,0 @@ -module Twitcr - TIME_FORMAT = Time::Format.new("%FT%TZ") -end diff --git a/src/twitch/mappings/user.cr b/src/twitch/mappings/user.cr deleted file mode 100644 index 6ddd08b..0000000 --- a/src/twitch/mappings/user.cr +++ /dev/null @@ -1,21 +0,0 @@ -require "./converters" - -module Twitcr - struct UserList - JSON.mapping({data: Array(User)}) - end - - struct User - JSON.mapping({ - id: {type: UInt64, converter: ID::Converter}, - login: String, - display_name: String, - }) - end -end - -module ID::Converter - def self.from_json(value : JSON::PullParser) : UInt64 - UInt64.new(value.read_string) - end -end diff --git a/src/twitch/rest.cr b/src/twitch/rest.cr deleted file mode 100644 index 1de8d6f..0000000 --- a/src/twitch/rest.cr +++ /dev/null @@ -1,50 +0,0 @@ -require "http" -require "json" -require "./mappings/*" - -module Twitcr::REST - # Mixin for interacting with Twitch's REST API - SSL_CONTEXT = OpenSSL::SSL::Context::Client.new - API_BASE = "https://api.twitch.tv/helix" - - EMPTY_RESULT = Exception.new("Empty Result") - - # Executes an HTTP request against the API_BASE url - def request(method : String, route : String, version = "5", headers = HTTP::Headers.new, body : String? = nil) - headers["Authorization"] = @token - headers["Client-ID"] = @client_id - - response = HTTP::Client.exec( - method, - API_BASE + route, - headers, - tls: SSL_CONTEXT - ) - - response.body - end - - def get_user_by_login(login : String) - response = request( - "GET", - "/users?login=" + login - ) - - list = UserList.from_json(response) - raise EMPTY_RESULT if list.data.empty? - - list.data.first - end - - def get_user_by_id(id : Int64) - response = request( - "GET", - "/users?id=" + id.to_s - ) - - list = UserList.from_json(response) - raise EMPTY_RESULT if list.data.empty? - - list.data.first - end -end diff --git a/src/twitcr.cr b/src/twitcr.cr index 701653d..24af4d4 100644 --- a/src/twitcr.cr +++ b/src/twitcr.cr @@ -1,4 +1,5 @@ -# TODO: Write documentation for `Twitcr` +require "./twitcr/**" + module Twitcr VERSION = "0.1.0" diff --git a/src/twitcr/client.cr b/src/twitcr/client.cr new file mode 100644 index 0000000..8502b90 --- /dev/null +++ b/src/twitcr/client.cr @@ -0,0 +1,27 @@ +require "./rest" +require "./mappings/*" + +module Twitcr::Client + include REST + + getter token : String + getter client_id : String + + def initialize(@token, @client_id) + @token = "Bearer" + @token + end + + def user?(name : String) + true if get_user_by_login(name) + rescue EMPTY_RESULT + false + end + + def user(name : String) + get_user_by_login(name) + end + + def user(id : Int64) + get_user_by_id(id) + end +end diff --git a/src/twitcr/mappings/converters.cr b/src/twitcr/mappings/converters.cr new file mode 100644 index 0000000..d9b57e9 --- /dev/null +++ b/src/twitcr/mappings/converters.cr @@ -0,0 +1,3 @@ +module Twitcr + TIME_FORMAT = Time::Format.new("%FT%TZ") +end diff --git a/src/twitcr/mappings/user.cr b/src/twitcr/mappings/user.cr new file mode 100644 index 0000000..6ddd08b --- /dev/null +++ b/src/twitcr/mappings/user.cr @@ -0,0 +1,21 @@ +require "./converters" + +module Twitcr + struct UserList + JSON.mapping({data: Array(User)}) + end + + struct User + JSON.mapping({ + id: {type: UInt64, converter: ID::Converter}, + login: String, + display_name: String, + }) + end +end + +module ID::Converter + def self.from_json(value : JSON::PullParser) : UInt64 + UInt64.new(value.read_string) + end +end diff --git a/src/twitcr/rest.cr b/src/twitcr/rest.cr new file mode 100644 index 0000000..1de8d6f --- /dev/null +++ b/src/twitcr/rest.cr @@ -0,0 +1,50 @@ +require "http" +require "json" +require "./mappings/*" + +module Twitcr::REST + # Mixin for interacting with Twitch's REST API + SSL_CONTEXT = OpenSSL::SSL::Context::Client.new + API_BASE = "https://api.twitch.tv/helix" + + EMPTY_RESULT = Exception.new("Empty Result") + + # Executes an HTTP request against the API_BASE url + def request(method : String, route : String, version = "5", headers = HTTP::Headers.new, body : String? = nil) + headers["Authorization"] = @token + headers["Client-ID"] = @client_id + + response = HTTP::Client.exec( + method, + API_BASE + route, + headers, + tls: SSL_CONTEXT + ) + + response.body + end + + def get_user_by_login(login : String) + response = request( + "GET", + "/users?login=" + login + ) + + list = UserList.from_json(response) + raise EMPTY_RESULT if list.data.empty? + + list.data.first + end + + def get_user_by_id(id : Int64) + response = request( + "GET", + "/users?id=" + id.to_s + ) + + list = UserList.from_json(response) + raise EMPTY_RESULT if list.data.empty? + + list.data.first + end +end -- cgit v1.2.3