songdrop 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bcd1d7531ad8c8af6585eb4450d222ce6f4ee428
4
+ data.tar.gz: ebb350a68b4f0115ddd99c95c5adba4d7221e3ce
5
+ SHA512:
6
+ metadata.gz: 223a43cdad7bb3be2aa8291df8af96c6f18caa06147e432c201d4d66b23310bc35cc7b1d164d7c4da3d4228caffd58cc49a8c2e1e14647880d5316850be1cf77
7
+ data.tar.gz: 8e2a1d7a612d1bb82d78096fca8bd6edd83888f48ac419c15d17807acd8e84325d6739cd528fdc7c7af40fead84e55e40a9990e29b0b89eeec248f708e576302
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Richard Taylor
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ Ruby client library for the Songdrop.com API. More docs to come...
@@ -0,0 +1,13 @@
1
+ module Songdrop
2
+ class Client
3
+
4
+ def login(params, &block)
5
+ post('/session', params) do |user|
6
+ @auth_token = user.auth_token
7
+ block.call user if block_given?
8
+ user
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Songdrop
2
+ class Client
3
+ attr_accessor :auth_token
4
+
5
+ def initialize(options={})
6
+ @token = options[:token]
7
+ @endpoint = options[:endpoint] || 'https://songdrop.com/v1'
8
+ @auth_token = options[:auth_token]
9
+ end
10
+
11
+ def get(path, params={}, &block)
12
+ puts "[Songdrop::Client] GET #{path} with #{params.inspect} block? #{block_given?}"
13
+ params.merge!(:client => @token, :token => @auth_token)
14
+ HTTP.get(full_url(path), params) do |response, error|
15
+ handle_response(response, error, &block)
16
+ end
17
+ end
18
+
19
+ def put(path, params={}, &block)
20
+ puts "[Songdrop::Client] PUT #{path} with #{params.inspect} block? #{block_given?}"
21
+ params.merge!(:client => @token, :token => @auth_token)
22
+ HTTP.put(full_url(path), params) do |response, error|
23
+ handle_response(response, error, &block)
24
+ end
25
+ end
26
+
27
+ def post(path, params={}, &block)
28
+ puts "[Songdrop::Client] POST #{path} with #{params.inspect} block? #{block_given?}"
29
+ params.merge!(:client => @token, :token => @auth_token)
30
+ HTTP.post(full_url(path), params) do |response, error|
31
+ handle_response(response, error, &block)
32
+ end
33
+ end
34
+
35
+ def delete(path, params={}, &block)
36
+ puts "[Songdrop::Client] DELETE #{path} with #{params.inspect} block? #{block_given?}"
37
+ params.merge!(:client => @token, :token => @auth_token)
38
+ HTTP.delete(full_url(path), params) do |response, error|
39
+ handle_response(response, error, &block)
40
+ end
41
+ end
42
+
43
+ private
44
+ def full_url(path)
45
+ "#{@endpoint}#{path}"
46
+ end
47
+
48
+ def handle_response(response, error, &block)
49
+ target = response || error
50
+ JSON.parse(target) do |obj|
51
+ res = Parser.parse(obj)
52
+ res = res.first if res.is_a? Array and res.size == 1
53
+ block.call res if block_given?
54
+ res
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,41 @@
1
+ module Songdrop
2
+ class HTTP
3
+
4
+ def self.get(url, params={}, &block)
5
+ http_request(:get, url, params, &block)
6
+ end
7
+
8
+ def self.post(url, params={}, &block)
9
+ http_request(:post, url, params, &block)
10
+ end
11
+
12
+ def self.put(url, params={}, &block)
13
+ http_request(:put, url, params, &block)
14
+ end
15
+
16
+ def self.delete(url, params={}, &block)
17
+ http_request(:delete, url, params, &block)
18
+ end
19
+
20
+ def self.http_request(method, url, options={}, &block)
21
+ bw_options = {:payload => options}
22
+ bw_options.merge!({:headers => {:Accept => "application/json"}})
23
+
24
+ puts "[Songdrop::HTTP] #{method} #{url} with: #{bw_options.inspect}"
25
+
26
+ BubbleWrap::HTTP.send(method, url, bw_options) do |response|
27
+ if response.ok?
28
+ begin #in case we don't receive JSON back, we don't want the app to crash:
29
+ json = BubbleWrap::JSON.parse(response.body.to_str)
30
+ block.call response, json
31
+ rescue
32
+ block.call response, nil
33
+ end
34
+ else
35
+ block.call response, nil
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ module Songdrop
2
+ class HTTP
3
+
4
+ def self.get(url, params={}, &block)
5
+ begin
6
+ block.call(RestClient.get(url, :params => params), nil)
7
+ rescue => e
8
+ puts "[Songdrop::HTTP] Error: #{e.inspect}"
9
+ block.call(nil, e.response)
10
+ end
11
+ end
12
+
13
+ def self.put(url, params={}, &block)
14
+ begin
15
+ block.call(RestClient.put(url, params), nil)
16
+ rescue => e
17
+ puts "[Songdrop::HTTP] Error: #{e.inspect}"
18
+ block.call nil, e.response
19
+ end
20
+ end
21
+
22
+ def self.post(url, params={}, &block)
23
+ begin
24
+ block.call(RestClient.post(url, params), nil)
25
+ rescue => e
26
+ puts "[Songdrop::HTTP] Error: #{e.inspect}"
27
+ block.call(nil, e.response)
28
+ end
29
+ end
30
+
31
+ def self.delete(url, params={}, &block)
32
+ begin
33
+ block.call(RestClient.delete(url, params), nil)
34
+ rescue => e
35
+ puts "[Songdrop::HTTP] Error: #{e.inspect}"
36
+ block.call(nil, e.response)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ module Songdrop
2
+ class JSON
3
+
4
+ def self.parse(response, &block)
5
+ block.call ::BubbleWrap::JSON.parse(response.body.to_str)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Songdrop
2
+ class JSON
3
+
4
+ def self.parse(json, &block)
5
+ block.call ::JSON.parse(json)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Songdrop
2
+ class Artist < Base
3
+ include ImageHelper
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module Songdrop
2
+ class Base
3
+ attr_reader :properties
4
+
5
+ def initialize(properties={})
6
+ @properties = properties
7
+ end
8
+
9
+ def method_missing(method, *args, &block)
10
+ @properties[method.to_sym]
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Songdrop
2
+ class Drop < Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Songdrop
2
+ class Error < Base
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ module Songdrop
2
+ module ImageHelper
3
+
4
+ def image_url(options={})
5
+ options[:action] ||= 'crop'
6
+ options[:size] ||= '50x50'
7
+ self.image.gsub('ACTION', options[:action]).gsub('WIDTHxHEIGHT', options[:size])
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Songdrop
2
+ class Mix < Base
3
+ include ImageHelper
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Songdrop
2
+ class Song < Base
3
+ include ImageHelper
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Songdrop
2
+ class User < Base
3
+ include ImageHelper
4
+
5
+ def auth_token
6
+ "#{self.id}-#{self.api_token}"
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,68 @@
1
+ module Songdrop
2
+ class Parser
3
+
4
+ def self.parse(response)
5
+
6
+ if response.is_a?(Hash) and response['object']
7
+
8
+ # we only got one object back
9
+ return objectize(response['object'], parse_object(response))
10
+
11
+ elsif response.is_a?(Hash)
12
+
13
+ # we got a hash pointer to objects
14
+ return response.keys.collect do |object|
15
+ objectize(object, parse_object(response[object]))
16
+ end
17
+
18
+ elsif response.is_a? Array
19
+
20
+ # we got an array of objects back
21
+ return response.collect do |object|
22
+ objectize(object['object'], parse_object(object))
23
+ end
24
+
25
+ else
26
+ puts "[Songdrop::Parser] Unexpected response type #{response.class}"
27
+ return response
28
+ end
29
+ end
30
+
31
+ def self.parse_object(hash)
32
+ # puts "[Songdrop::Parser] PARSE #{hash.inspect}"
33
+ properties = {}
34
+
35
+ hash.keys.each do |property|
36
+ # puts "[Songdrop::Parser] #{property} is a #{property.class}"
37
+ if hash[property].is_a? Array
38
+ # puts "[Songdrop::Parser] parsing array #{property}"
39
+ objects = []
40
+ hash[property].each do |el|
41
+ objects << parse_object(el)
42
+ end
43
+ properties[property.to_sym] = objects
44
+ elsif hash[property].is_a? Hash
45
+ # puts "[Songdrop::Parser] parsing hash #{property} of type #{hash[property]['object']}"
46
+ object = objectize(hash[property]['object'], parse_object(hash[property]))
47
+ properties[property.to_sym] = object
48
+ else
49
+ properties[property.to_sym] = hash[property]
50
+ end
51
+ end
52
+
53
+ properties
54
+ end
55
+
56
+ def self.objectize(type, properties)
57
+ case type.to_sym
58
+ when :user then User.new(properties)
59
+ when :drop then Drop.new(properties)
60
+ when :song then Song.new(properties)
61
+ when :mix then Mix.new(properties)
62
+ when :artist then Artist.new(properties)
63
+ else "[Songdrop::Parser] Don't know how to objectize #{type}"
64
+ end
65
+ end
66
+
67
+ end
68
+ end
data/lib/songdrop.rb ADDED
@@ -0,0 +1,32 @@
1
+ if defined? Motion::Project::Config
2
+
3
+ Motion::Project::App.setup do |app|
4
+ Dir.glob(File.join(File.dirname(__FILE__), 'songdrop/*.rb')).each do |file|
5
+ app.files.unshift(file)
6
+ end
7
+ Dir.glob(File.join(File.dirname(__FILE__), 'songdrop/*/*.rb')).each do |file|
8
+ app.files.unshift(file)
9
+ end
10
+ end
11
+
12
+ else
13
+
14
+ require 'rest-client'
15
+ require 'json'
16
+
17
+ require_relative './songdrop/parser'
18
+ require_relative './songdrop/client'
19
+ require_relative './songdrop/client/methods'
20
+ require_relative './songdrop/objects/base'
21
+ require_relative './songdrop/objects/image_helper'
22
+ require_relative './songdrop/objects/user'
23
+ require_relative './songdrop/objects/drop'
24
+ require_relative './songdrop/objects/song'
25
+ require_relative './songdrop/objects/mix'
26
+ require_relative './songdrop/objects/artist'
27
+ require_relative './songdrop/http/rest-client'
28
+ require_relative './songdrop/json/json'
29
+ end
30
+
31
+ module Songdrop
32
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: songdrop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: songdrop is a Ruby client for the Songdrop.com API.
14
+ email: moomerman@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/songdrop.rb
22
+ - lib/songdrop/client.rb
23
+ - lib/songdrop/parser.rb
24
+ - lib/songdrop/client/methods.rb
25
+ - lib/songdrop/http/bubblewrap.rb
26
+ - lib/songdrop/http/rest-client.rb
27
+ - lib/songdrop/json/bubblewrap.rb
28
+ - lib/songdrop/json/json.rb
29
+ - lib/songdrop/objects/artist.rb
30
+ - lib/songdrop/objects/base.rb
31
+ - lib/songdrop/objects/drop.rb
32
+ - lib/songdrop/objects/error.rb
33
+ - lib/songdrop/objects/image_helper.rb
34
+ - lib/songdrop/objects/mix.rb
35
+ - lib/songdrop/objects/song.rb
36
+ - lib/songdrop/objects/user.rb
37
+ homepage: https://api.songdrop.com/
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --inline-source
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project: songdrop
58
+ rubygems_version: 2.0.0
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: songdrop is a Ruby client for the Songdrop.com API.
62
+ test_files: []