spty_api 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +1 -0
- data/README.md +13 -2
- data/bin/console +1 -1
- data/lib/spty_api/authorization.rb +64 -0
- data/lib/spty_api/http/client.rb +99 -0
- data/lib/spty_api/version.rb +1 -1
- data/lib/spty_api.rb +12 -1
- data/spty_api.gemspec +2 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ff0541668998ec258814bfecedf829d3f769d2f
|
4
|
+
data.tar.gz: 17dc493027d2235902950b7dfca889c4e9e11fce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6f453161d6f12243c1d8d561b56fda6fcdff4fb43b3033aa13039e24400a8b5e4ae3ae064438e5aa2a99186848d5023ba2e73112bfaf79e49c70e3a1c1f8be9
|
7
|
+
data.tar.gz: 841b3618d86973092d178e2a23d121114fc6eb7fa51ba1465633dbe3da53fa0c2f1087b97facfdcfdab5c79b2dc223ad9771fdcd40779738dd14eef51984e33a
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -10,6 +10,7 @@ before_script:
|
|
10
10
|
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
11
11
|
- chmod +x ./cc-test-reporter
|
12
12
|
- ./cc-test-reporter before-build
|
13
|
+
- cp spec/spec_config_helper.sample.rb spec/spec_config_helper.rb
|
13
14
|
script:
|
14
15
|
- bundle exec rspec
|
15
16
|
after_script:
|
data/README.md
CHANGED
@@ -38,8 +38,19 @@ for the 'Authorization Code Flow' will be added in later versions.
|
|
38
38
|
## Development
|
39
39
|
|
40
40
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
41
|
-
rake spec` to run the tests. You can also run `bin/console` for an
|
42
|
-
prompt that will allow you to experiment.
|
41
|
+
`bundle exec rake spec` to run the tests. You can also run `bin/console` for an
|
42
|
+
interactive prompt that will allow you to experiment.
|
43
|
+
|
44
|
+
Run `VCR_RECORD=true bundle exec rake spec` to re-record the VCR cassettes. This
|
45
|
+
will make the test hit the actual API and VCR will record the response. You will
|
46
|
+
need to setup your credentials first in order to the test to get the correct
|
47
|
+
result.
|
48
|
+
|
49
|
+
To setup your credentials, copy the file `spec/spec_config_helper.sample.rb` to
|
50
|
+
`spec/spec_config_helper.rb` and add your API `client_id` and `secret_key`.
|
51
|
+
|
52
|
+
You don't need to record new VCR cassettes, unless you are adding the new
|
53
|
+
endpoint are that the API response has change.
|
43
54
|
|
44
55
|
## Contributing
|
45
56
|
|
data/bin/console
CHANGED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module SptyAPI
|
4
|
+
class Authorization
|
5
|
+
attr_accessor :access_token, :token_type, :expires_in, :scope,
|
6
|
+
:authorized, :errors
|
7
|
+
|
8
|
+
ACCOUNT_API = 'https://accounts.spotify.com/api/'
|
9
|
+
|
10
|
+
def initialize(type:)
|
11
|
+
if type == 'client_credentials'
|
12
|
+
self.class.via_client_credentials(self)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.via_client_credentials(auth)
|
17
|
+
auth.login
|
18
|
+
end
|
19
|
+
|
20
|
+
def login
|
21
|
+
client_id = SptyAPI.configuration.client_id
|
22
|
+
secret_key = SptyAPI.configuration.secret_key
|
23
|
+
auth_cred = Base64.strict_encode64("#{client_id}:#{secret_key}")
|
24
|
+
|
25
|
+
http = SptyAPI::HTTP::Client.new(
|
26
|
+
http_method: 'post',
|
27
|
+
token_type: 'Basic',
|
28
|
+
access_token: auth_cred,
|
29
|
+
endpoint: 'token'
|
30
|
+
)
|
31
|
+
|
32
|
+
http.api_overwrite = ACCOUNT_API
|
33
|
+
http.content_type_overwrite = 'application/x-www-form-urlencoded'
|
34
|
+
http.body_parameter = 'grant_type=client_credentials'
|
35
|
+
|
36
|
+
res = JSON.parse(http.request.read_body)
|
37
|
+
|
38
|
+
res['access_token'] ? authorize(res) : reject(res)
|
39
|
+
end
|
40
|
+
|
41
|
+
def client_is_authorized?
|
42
|
+
authorized
|
43
|
+
end
|
44
|
+
|
45
|
+
def authorize(res)
|
46
|
+
self.authorized = true
|
47
|
+
self.access_token = res['access_token']
|
48
|
+
self.token_type = res['token_type']
|
49
|
+
self.expires_in = Time.now + res['expires_in']
|
50
|
+
self.scope = res['scope']
|
51
|
+
end
|
52
|
+
|
53
|
+
def reject(res)
|
54
|
+
self.authorized = false
|
55
|
+
self.errors = res
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def setup_credentials
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'openssl'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module SptyAPI::HTTP
|
7
|
+
class Client
|
8
|
+
attr_accessor :path_parameter, :query_parameter, :body_parameter,
|
9
|
+
:http_method, :token_type, :access_token, :endpoint,
|
10
|
+
:api_overwrite, :content_type_overwrite
|
11
|
+
|
12
|
+
DEFAULT_API = 'https://api.spotify.com/v1/'
|
13
|
+
CONTENT_TYPE = 'application/json'
|
14
|
+
|
15
|
+
def initialize(http_method:, token_type:, access_token: nil, endpoint:)
|
16
|
+
self.http_method = http_method
|
17
|
+
self.token_type = token_type
|
18
|
+
self.access_token = access_token
|
19
|
+
self.endpoint = endpoint
|
20
|
+
end
|
21
|
+
|
22
|
+
def request
|
23
|
+
uri = prepare_uri
|
24
|
+
http = prepare_http(uri)
|
25
|
+
http.request(prepare_request(uri))
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def prepare_uri
|
31
|
+
id = (path_parameter.nil?) ? '' : path_parameter[:id]
|
32
|
+
api = (api_overwrite.nil?) ? DEFAULT_API : api_overwrite
|
33
|
+
uri = "#{api}#{endpoint}"
|
34
|
+
uri += "/#{id}" if id != ''
|
35
|
+
uri += "?#{prepare_query_parameter}"
|
36
|
+
URI.parse(uri)
|
37
|
+
end
|
38
|
+
|
39
|
+
def prepare_query_parameter
|
40
|
+
return '' if query_parameter.nil?
|
41
|
+
|
42
|
+
q_params = ''
|
43
|
+
query_parameter.each_with_index do |(key, value), index|
|
44
|
+
q_params += '&' if index > 0
|
45
|
+
q_params += "#{key}=#{value}"
|
46
|
+
end
|
47
|
+
|
48
|
+
q_params
|
49
|
+
end
|
50
|
+
|
51
|
+
def prepare_http(uri)
|
52
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
53
|
+
http.use_ssl = true
|
54
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
55
|
+
|
56
|
+
http
|
57
|
+
end
|
58
|
+
|
59
|
+
def prepare_request(uri)
|
60
|
+
req = self.send("setup_request_#{http_method}", uri)
|
61
|
+
req['Authorization'] = "#{token_type} #{access_token}"
|
62
|
+
|
63
|
+
req
|
64
|
+
end
|
65
|
+
|
66
|
+
def prepare_content_type
|
67
|
+
(content_type_overwrite.nil?) ? CONTENT_TYPE : content_type_overwrite
|
68
|
+
end
|
69
|
+
|
70
|
+
def prepare_body_parameter
|
71
|
+
return body_parameter.to_json if content_type_overwrite.nil?
|
72
|
+
body_parameter
|
73
|
+
end
|
74
|
+
|
75
|
+
def setup_request_get(uri)
|
76
|
+
Net::HTTP::Get.new(uri.request_uri)
|
77
|
+
end
|
78
|
+
|
79
|
+
def setup_request_post(uri)
|
80
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
81
|
+
req.body = prepare_body_parameter
|
82
|
+
req["Content-Type"] = prepare_content_type
|
83
|
+
|
84
|
+
req
|
85
|
+
end
|
86
|
+
|
87
|
+
def setup_request_put(uri)
|
88
|
+
req = Net::HTTP::Put.new(uri.request_uri)
|
89
|
+
req.body = prepare_body_parameter
|
90
|
+
req["Content-Type"] = prepare_content_type
|
91
|
+
|
92
|
+
req
|
93
|
+
end
|
94
|
+
|
95
|
+
def setup_request_delete(uri)
|
96
|
+
Net::HTTP::Delete.new(uri.request_uri)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/lib/spty_api/version.rb
CHANGED
data/lib/spty_api.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
require 'spty_api/version'
|
2
2
|
require 'spty_api/configuration'
|
3
|
+
require 'spty_api/authorization'
|
4
|
+
require 'spty_api/http/client'
|
3
5
|
|
4
6
|
module SptyAPI
|
5
7
|
class << self
|
6
|
-
attr_accessor :configuration
|
8
|
+
attr_accessor :configuration, :authorization
|
7
9
|
|
8
10
|
def configure
|
9
11
|
self.configuration ||= Configuration.new
|
10
12
|
yield(configuration)
|
11
13
|
end
|
14
|
+
|
15
|
+
def client_authorize
|
16
|
+
self.authorization ||= Authorization.new(type: 'client_credentials')
|
17
|
+
end
|
18
|
+
|
19
|
+
def reset
|
20
|
+
self.configuration = nil
|
21
|
+
self.authorization = nil
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
data/spty_api.gemspec
CHANGED
@@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency 'rspec', "~> 3.0"
|
29
29
|
spec.add_development_dependency 'simplecov'
|
30
30
|
spec.add_development_dependency 'simplecov-console'
|
31
|
+
spec.add_development_dependency 'webmock'
|
32
|
+
spec.add_development_dependency 'vcr'
|
31
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spty_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J Shamsul
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description:
|
84
112
|
email:
|
85
113
|
- jibone@gmail.com
|
@@ -100,7 +128,9 @@ files:
|
|
100
128
|
- bin/console
|
101
129
|
- bin/setup
|
102
130
|
- lib/spty_api.rb
|
131
|
+
- lib/spty_api/authorization.rb
|
103
132
|
- lib/spty_api/configuration.rb
|
133
|
+
- lib/spty_api/http/client.rb
|
104
134
|
- lib/spty_api/version.rb
|
105
135
|
- spty_api.gemspec
|
106
136
|
homepage: https://github.com/jibone/sptyapi_rb
|