soundcloud 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -2
- data/lib/soundcloud.rb +25 -15
- data/lib/soundcloud/hash_response_wrapper.rb +3 -1
- data/lib/soundcloud/version.rb +1 -1
- metadata +8 -8
data/README.md
CHANGED
@@ -46,7 +46,7 @@ It is providing simple methods to handle authorization and to execute HTTP calls
|
|
46
46
|
|
47
47
|
client.authorize_url(:redirect_uri => REDIRECT_URI)
|
48
48
|
# => "https://soundcloud.com/connect?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=http://host/redirect"
|
49
|
-
client.
|
49
|
+
client.exchange_token(:redirect_uri => uri, :code => 'CODE')
|
50
50
|
|
51
51
|
#### OAuth2 refresh token flow, upload a track and print its link
|
52
52
|
# register a new client which will exchange an existing refresh_token for an access_token
|
@@ -117,4 +117,4 @@ Will return true or false depending on if expires_at is in the past.
|
|
117
117
|
|
118
118
|
#### Error Handling
|
119
119
|
In case a request was not successful a Soundcloud::ResponseError will be raise.
|
120
|
-
The original HTTParty response is available through Soundcloud::ResponseError#response.
|
120
|
+
The original HTTParty response is available through Soundcloud::ResponseError#response.
|
data/lib/soundcloud.rb
CHANGED
@@ -5,9 +5,16 @@ require 'hashie'
|
|
5
5
|
require 'uri'
|
6
6
|
|
7
7
|
class Soundcloud
|
8
|
-
class ResponseError < HTTParty::ResponseError
|
8
|
+
class ResponseError < HTTParty::ResponseError
|
9
|
+
def self.message(response)
|
10
|
+
error = response.parsed_response['error'] || response.parsed_response['errors']['error']
|
11
|
+
"HTTP status: #{response.code} error: #{error}"
|
12
|
+
rescue
|
13
|
+
"HTTP status: #{response.code}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
9
17
|
include HTTMultiParty
|
10
|
-
headers 'Accept' => 'application/json'
|
11
18
|
|
12
19
|
# TODO fix when api is ready for client_id
|
13
20
|
CLIENT_ID_PARAM_NAME = :consumer_key
|
@@ -30,11 +37,11 @@ class Soundcloud
|
|
30
37
|
raise ArgumentError, "At least a client_id or an access_token must be present" if client_id.nil? && access_token.nil?
|
31
38
|
end
|
32
39
|
|
33
|
-
def get (path, query={}, options={}); handle_response { self.class.get
|
34
|
-
def post (path,
|
35
|
-
def put (path,
|
36
|
-
def delete(path, query={}, options={});
|
37
|
-
def head (path, query={}, options={}); handle_response { self.class.head
|
40
|
+
def get (path, query={}, options={}); handle_response { self.class.get *construct_query_arguments(path, options.merge(:query => query)) } end
|
41
|
+
def post (path, body={}, options={}); handle_response { self.class.post *construct_query_arguments(path, options.merge(:body => body), :body) } end
|
42
|
+
def put (path, body={}, options={}); handle_response { self.class.put *construct_query_arguments(path, options.merge(:body => body), :body) } end
|
43
|
+
def delete(path, query={}, options={}); handle_response { self.class.delete *construct_query_arguments(path, options.merge(:query => query)) } end
|
44
|
+
def head (path, query={}, options={}); handle_response { self.class.head *construct_query_arguments(path, options.merge(:query => query)) } end
|
38
45
|
|
39
46
|
# accessors for options
|
40
47
|
def client_id; @options[:client_id]; end
|
@@ -58,8 +65,11 @@ class Soundcloud
|
|
58
65
|
def api_host; [API_SUBHOST, host].join('.'); end
|
59
66
|
|
60
67
|
def authorize_url(options={})
|
68
|
+
display = options.delete(:display)
|
61
69
|
store_options(options)
|
62
|
-
"https://#{host}#{AUTHORIZE_PATH}?response_type=
|
70
|
+
url = "https://#{host}#{AUTHORIZE_PATH}?response_type=code_and_token&client_id=#{client_id}&redirect_uri=#{URI.escape redirect_uri}"
|
71
|
+
url << "&display=#{display}" if display
|
72
|
+
url
|
63
73
|
end
|
64
74
|
|
65
75
|
def exchange_token(options={})
|
@@ -91,12 +101,12 @@ private
|
|
91
101
|
def handle_response(refreshing_enabled=true, &block)
|
92
102
|
response = block.call
|
93
103
|
if response && !response.success?
|
94
|
-
if response.code == 401 &&
|
104
|
+
if response.code == 401 && refreshing_enabled
|
95
105
|
exchange_token
|
96
106
|
# TODO it should return the original
|
97
107
|
handle_response(false, &block)
|
98
108
|
else
|
99
|
-
raise ResponseError.new(response),
|
109
|
+
raise ResponseError.new(response), ResponseError.message(response)
|
100
110
|
end
|
101
111
|
elsif response.is_a? Hash
|
102
112
|
HashResponseWrapper.new(response)
|
@@ -115,18 +125,18 @@ private
|
|
115
125
|
end
|
116
126
|
|
117
127
|
|
118
|
-
def construct_query_arguments(path_or_uri, options={})
|
128
|
+
def construct_query_arguments(path_or_uri, options={}, body_or_query=:query)
|
119
129
|
uri = URI.parse(path_or_uri)
|
120
130
|
path = uri.path
|
121
131
|
|
122
132
|
scheme = use_ssl? ? 'https' : 'http'
|
123
133
|
options = options.dup
|
124
|
-
options[
|
125
|
-
|
134
|
+
options[body_or_query] ||= {}
|
135
|
+
options[body_or_query][:format] = "json"
|
126
136
|
if access_token
|
127
|
-
options[
|
137
|
+
options[body_or_query][:oauth_token] = access_token
|
128
138
|
else
|
129
|
-
options[
|
139
|
+
options[body_or_query][CLIENT_ID_PARAM_NAME] = client_id
|
130
140
|
end
|
131
141
|
[
|
132
142
|
"#{scheme}://#{api_host}#{path}#{uri.query ? "?#{uri.query}" : ""}",
|
data/lib/soundcloud/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soundcloud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Johannes Wagener
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-22 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -42,11 +42,11 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
hash:
|
45
|
+
hash: 13
|
46
46
|
segments:
|
47
47
|
- 0
|
48
|
-
-
|
49
|
-
version: "0.
|
48
|
+
- 3
|
49
|
+
version: "0.3"
|
50
50
|
type: :runtime
|
51
51
|
version_requirements: *id002
|
52
52
|
- !ruby/object:Gem::Dependency
|