twitchrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e6d24ae98033340c3a32007d15326393e2eb630d9bf617a22dcd86c3bd96205b
4
+ data.tar.gz: 4099ce7790604f9d77dfb4e7e5c22252573ef6adcfde1806b99ea8b966d984cf
5
+ SHA512:
6
+ metadata.gz: 63a0c047cd6803daf0e094bdf88e0fb6ac73fa1959beb84f43c38634190bb5a53ce2e2242993ead65b8361ca4673a601a608a7769eac4026c320b83f9e57a3cc
7
+ data.tar.gz: c04701b41259897d3a871df91037b801fb6c36f3e15f8bb764863dc96435b1da5671a97611570e11f5244d94dc851275f1f7cf0b4a69dcd929822548d2061157
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in twitch.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twitchrb (0.1.0)
5
+ httparty (~> 0.18.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ httparty (0.18.1)
11
+ mime-types (~> 3.0)
12
+ multi_xml (>= 0.5.2)
13
+ mime-types (3.3.1)
14
+ mime-types-data (~> 3.2015)
15
+ mime-types-data (3.2020.0512)
16
+ minitest (5.14.1)
17
+ multi_xml (0.6.0)
18
+ rake (12.3.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ minitest (~> 5.0)
25
+ rake (~> 12.0)
26
+ twitchrb!
27
+
28
+ BUNDLED WITH
29
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dean Perry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # TwitchRB
2
+
3
+ **This library is a work in progress**
4
+
5
+ This RubyGem is a library for intereacting with the Twitch API.
6
+
7
+ It will allow you to easily interact with both the Kraken and Helix APIs.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "twitchrb", require: "twitch"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+
22
+ ## Contributing
23
+
24
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/twitchrb.
25
+
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twitch"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ require "time"
2
+ require "httparty"
3
+ require "json"
4
+
5
+ require "twitch/version"
6
+
7
+ require "twitch/client"
8
+ require "twitch/initializable"
9
+
10
+ # Kraken (v5) API
11
+ require "twitch/kraken/user"
12
+ require "twitch/kraken/users"
13
+ require "twitch/kraken/channels"
14
+ require "twitch/kraken/clips"
15
+
16
+ module Twitch
17
+
18
+ class << self
19
+ attr_reader :client
20
+
21
+ def access_details(client_id)
22
+ @client = Client.new(client_id)
23
+ # @client.access_token = access_token if access_token
24
+ end
25
+ end
26
+
27
+ # Error classes to raise
28
+ class Error < StandardError; end
29
+ module Errors
30
+ class ServiceUnavailable < Error; end
31
+ class AccessDenied < Error; end
32
+ class NotFound < Error; end
33
+ class CommunicationError < Error; end
34
+ class ValidationError < Error; end
35
+ end
36
+
37
+ end
@@ -0,0 +1,89 @@
1
+ require "httparty"
2
+
3
+ module Twitch
4
+ class Client
5
+
6
+ # def initialize(client_id, client_secret, access_token=nil)
7
+ def initialize(client_id)
8
+ puts "initialize"
9
+
10
+ @client_id = client_id
11
+
12
+ # if client_id && client_secret
13
+ # @client_id = client_id
14
+ # @client_secret = client_secret
15
+ # @access_token = access_token
16
+ # else
17
+ # raise Twitch::ClientError.new('Client ID or Client Secret not set')
18
+ # end
19
+
20
+ end
21
+
22
+
23
+ def get(kind, url, params={})
24
+ # puts url
25
+ # puts params
26
+ # puts @client_id
27
+ response = HTTParty.get("https://api.twitch.tv/#{kind}/#{url}", {
28
+ headers: {
29
+ "Client-ID" => @client_id,
30
+ "Accept" => "application/vnd.twitchtv.v5+json",
31
+ # "Authorization" => "OAuth #{@access_token}"
32
+ }
33
+ })
34
+ # puts response
35
+
36
+ # JSON.parse(response.body)
37
+
38
+ success = case response.code
39
+ when 200
40
+ JSON.parse(response.body)
41
+ when 400
42
+ json = JSON.parse(response.body)
43
+ raise Twitch::Errors::NotFound, json['error']
44
+ when 503
45
+ raise Twitch::Errors::ServiceUnavailable
46
+ when 401, 403
47
+ puts response.body.inspect
48
+ raise Twitch::Errors::AccessDenied, "Access Denied for '#{@client_id}'"
49
+ when 400
50
+ json = JSON.parse(response.body)
51
+ raise Twitch::Errors::ValidationError, json['errors'].to_s
52
+ else
53
+ raise Twitch::Errors::CommunicationError, response.body
54
+ end
55
+
56
+
57
+ # if response.code == 200
58
+ # JSON.parse(response.body)
59
+ # elsif response.code == 404
60
+ # Twitch::NotFoundError.new(response.body)
61
+ # elsif response.code == 500
62
+ # Twitch::ServerError.new(response.body)
63
+ # end
64
+
65
+
66
+
67
+ # when Net::HTTPClientError
68
+ # json = JSON.parse(http_result.body)
69
+ # raise Twitch::Errors::ValidationError, json['errors'].to_s
70
+ # else
71
+ # raise Twitch::Errors::CommunicationError, http_result.body
72
+ # end
73
+
74
+
75
+ end
76
+
77
+
78
+ # private
79
+
80
+ # def request(method, path, options={})
81
+ # # if @access_token
82
+
83
+
84
+
85
+ # end
86
+
87
+
88
+ end
89
+ end
@@ -0,0 +1,16 @@
1
+ module Twitch
2
+ module Initializable
3
+
4
+ def initialize(params = {})
5
+ params.each do |key, value|
6
+ if key == "_id"
7
+ key = "id"
8
+ end
9
+
10
+ setter = "#{key}="
11
+ send(setter, value) if respond_to?(setter.to_sym, false)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Twitch
2
+ module Kraken
3
+ class Channels
4
+
5
+ include Initializable
6
+
7
+ attr_accessor :id, :name, :display_name, :broadcaster_language, :followers, :game, :language, :logo, :mature, :partner, :profile_banner, :profile_banner_background_colour, :status, :url, :video_banner, :views, :created_at, :updated_at
8
+
9
+ class << self
10
+
11
+ # Gets a specified channel object
12
+ def get_channel_by_id(id)
13
+ response = Twitch.client.get(:kraken, "channels/#{id}")
14
+
15
+ new(response)
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module Twitch
2
+ module Kraken
3
+ class Clips
4
+
5
+ include Initializable
6
+
7
+ attr_accessor :id, :title, :slug, :url, :embed_url, :game, :views, :duration, :language, :broadcaster, :curator, :vod, :created_at, :updated_at
8
+
9
+ class << self
10
+
11
+ # Gets details about a specified clip
12
+ def get(slug)
13
+ response = Twitch.client.get(:kraken, "clips/#{slug}")
14
+
15
+ new(response)
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module Twitch
2
+ module Kraken
3
+ class User
4
+
5
+ include Initializable
6
+
7
+ attr_accessor :id, :name, :display_name, :email, :email_verified, :partnered, :type, :bio, :logo, :created_at, :updated_at
8
+
9
+ class << self
10
+
11
+ # Gets a user object based on the OAuth token provided
12
+ # Scopes required: user_read
13
+ def get
14
+ response = Twitch.client.get("user")
15
+
16
+ new(response)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module Twitch
2
+ module Kraken
3
+ class Users
4
+
5
+ include Initializable
6
+
7
+ attr_accessor :id, :name, :display_name, :type, :bio, :logo, :created_at, :updated_at
8
+
9
+ class << self
10
+
11
+ # Gets a specified user object
12
+ def get_user_by_id(id)
13
+ response = Twitch.client.get(:kraken, "users/#{id}")
14
+
15
+ new(response)
16
+ end
17
+
18
+ # Gets the user objects for the specified Twitch login names
19
+ def get_users(username)
20
+ response = Twitch.client.get(:kraken, "users?login=#{username}")
21
+
22
+ if response["users"].count >= 1
23
+ new(response["users"].first)
24
+ else
25
+ raise Twitch::Errors::NotFound
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Twitch
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/twitch/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "twitchrb"
5
+ spec.version = Twitch::VERSION
6
+ spec.authors = ["Dean Perry"]
7
+ spec.email = ["dean@deanpcmad.com"]
8
+
9
+ spec.summary = "A Ruby library for interacting with the Twitch API"
10
+ spec.homepage = "https://twitchrb.com"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/deanpcmad/twitchrb"
16
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency "httparty", "~> 0.18.1"
28
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitchrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.1
27
+ description:
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/twitch.rb
44
+ - lib/twitch/client.rb
45
+ - lib/twitch/initializable.rb
46
+ - lib/twitch/kraken/channels.rb
47
+ - lib/twitch/kraken/clips.rb
48
+ - lib/twitch/kraken/user.rb
49
+ - lib/twitch/kraken/users.rb
50
+ - lib/twitch/version.rb
51
+ - twitchrb.gemspec
52
+ homepage: https://twitchrb.com
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://twitchrb.com
57
+ source_code_uri: https://github.com/deanpcmad/twitchrb
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.1.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: A Ruby library for interacting with the Twitch API
77
+ test_files: []