Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit c4539e4

Browse files
committed
Initial scaffolding and basic response
0 parents  commit c4539e4

File tree

7 files changed

+105
-0
lines changed

7 files changed

+105
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby 2.7.1

Gemfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'puma'
4+
gem 'sinatra'
5+
gem 'faraday'
6+
gem 'faraday_middleware'
7+
8+
group :development do
9+
gem 'dotenv'
10+
end

Gemfile.lock

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
dotenv (2.7.6)
5+
faraday (1.0.1)
6+
multipart-post (>= 1.2, < 3)
7+
faraday_middleware (1.0.0)
8+
faraday (~> 1.0)
9+
multipart-post (2.1.1)
10+
mustermann (1.1.1)
11+
ruby2_keywords (~> 0.0.1)
12+
nio4r (2.5.3)
13+
puma (4.3.6)
14+
nio4r (~> 2.0)
15+
rack (2.2.3)
16+
rack-protection (2.1.0)
17+
rack
18+
ruby2_keywords (0.0.2)
19+
sinatra (2.1.0)
20+
mustermann (~> 1.0)
21+
rack (~> 2.2)
22+
rack-protection (= 2.1.0)
23+
tilt (~> 2.0)
24+
tilt (2.0.10)
25+
26+
PLATFORMS
27+
ruby
28+
29+
DEPENDENCIES
30+
dotenv
31+
faraday
32+
faraday_middleware
33+
puma
34+
sinatra
35+
36+
BUNDLED WITH
37+
2.1.4

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: ruby spotifeed.rb

Rakefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
desc "Open an IRB session"
2+
task :console do
3+
# Load all gems
4+
require 'rubygems'
5+
require 'bundler/setup'
6+
Bundler.require(:default)
7+
8+
# Load the envs
9+
require 'dotenv'
10+
Dotenv.load!
11+
12+
# Load IRB
13+
require 'irb'
14+
require 'irb/completion'
15+
16+
IRB.conf[:AUTO_INDENT] = true
17+
18+
ARGV.clear
19+
IRB.start
20+
end

spotifeed.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'rubygems'
2+
require 'bundler'
3+
4+
Bundler.require
5+
6+
$conn = Faraday.new 'https://api.spotify.com/v1/' do |conn|
7+
conn.response :json
8+
end
9+
10+
class Token
11+
def initialize
12+
@token_expiry = Time.now
13+
end
14+
15+
def refresh
16+
return unless @token_expiry <= Time.now
17+
18+
res = $conn.post(
19+
'https://accounts.spotify.com/api/token',
20+
'grant_type=client_credentials',
21+
'Authorization' => "Basic #{Base64.strict_encode64("#{ENV['SPOTIFY_CLIENT_ID']}:#{ENV['SPOTIFY_CLIENT_SECRET']}")}"
22+
)
23+
24+
token_expiry = Time.now + res.body["expires_in"]
25+
$conn.headers['Authorization'] = "Bearer #{res.body["access_token"]}"
26+
end
27+
end
28+
29+
token = Token.new
30+
31+
get '/' do
32+
token.refresh
33+
34+
JSON.generate $conn.get("shows/#{ENV['SHOW_ID']}?market=US").body
35+
end

0 commit comments

Comments
 (0)