soundcloud 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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.exchange_code(:redirect_uri => uri, :code => 'CODE')
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.
@@ -5,9 +5,16 @@ require 'hashie'
5
5
  require 'uri'
6
6
 
7
7
  class Soundcloud
8
- class ResponseError < HTTParty::ResponseError; end
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 *construct_query_arguments(path, options.merge(:query => query)) } end
34
- def post (path, query={}, options={}); handle_response { self.class.post *construct_query_arguments(path, options.merge(:query => query)) } end
35
- def put (path, query={}, options={}); handle_response { self.class.put *construct_query_arguments(path, options.merge(:query => query)) } end
36
- def delete(path, query={}, options={}); handle_response { self.class.delete *construct_query_arguments(path, options.merge(:query => query)) } end
37
- def head (path, query={}, options={}); handle_response { self.class.head *construct_query_arguments(path, options.merge(:query => query)) } end
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=code&client_id=#{client_id}&redirect_uri=#{URI.escape redirect_uri}"
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 && response["error"] == "invalid_grant" && refreshing_enabled
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), "HTTP Status #{response.code}"
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[:query] ||= {}
125
-
134
+ options[body_or_query] ||= {}
135
+ options[body_or_query][:format] = "json"
126
136
  if access_token
127
- options[:query][:oauth_token] = access_token
137
+ options[body_or_query][:oauth_token] = access_token
128
138
  else
129
- options[:query][CLIENT_ID_PARAM_NAME] = client_id
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}" : ""}",
@@ -1,7 +1,9 @@
1
1
  class Soundcloud::HashResponseWrapper < Hashie::Mash
2
2
  attr_reader :response
3
3
  def initialize(response, *args)
4
- super(response, *args)
4
+ super(response, *args) do |x|
5
+ raise NoMethodError
6
+ end
5
7
  @response = response
6
8
  end
7
9
  end
@@ -1,3 +1,3 @@
1
1
  class Soundcloud
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
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: 9
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 9
10
- version: 0.1.9
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-11 00:00:00 -05:00
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: 15
45
+ hash: 13
46
46
  segments:
47
47
  - 0
48
- - 2
49
- version: "0.2"
48
+ - 3
49
+ version: "0.3"
50
50
  type: :runtime
51
51
  version_requirements: *id002
52
52
  - !ruby/object:Gem::Dependency