shelbytv 0.0.1

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 ADDED
@@ -0,0 +1,80 @@
1
+ -----------
2
+ < Shelby.tv >
3
+ -----------
4
+ \
5
+ \
6
+ /\___)o<
7
+ | \
8
+ | O . O|
9
+ \_____/
10
+
11
+ # Shelby.tv
12
+
13
+ This is a Ruby wrapper for the [Shelby.tv](http://shelby.tv/) API. It uses objects instead of hashes and add associations.
14
+
15
+ ## Installation
16
+
17
+ To install this gem systemwide:
18
+
19
+ gem install shelbytv
20
+
21
+ Install it as a gem (in your `Gemfile`) and its dependencies:
22
+
23
+ gem "shelbytv"
24
+
25
+ ## Usage
26
+
27
+ Get a Shelby object:
28
+
29
+ shelbytv = Shelbytv::Base.new("CONSUMER_KEY", "CONSUMER_SECRET",
30
+ "AUTH_TOKEN", "AUTH_SECRET")
31
+
32
+ Get current user:
33
+
34
+ user = shelbytv.user # Returns a Shelbytv::User object
35
+
36
+ Get a user by id:
37
+
38
+ user = shelbytv.users.find('ID') # Returns a Shelbytv::User object
39
+
40
+ Get the current users channels:
41
+
42
+ shelbytv.channels.all # Returns an array for Shelbytv::Channel objects
43
+
44
+ # or
45
+
46
+ user.channels # Returns an array for Shelbytv::Channel objects
47
+
48
+ Get a channels broadcasts:
49
+
50
+ channel.broadcasts # Returns an array for Shelbytv::Broadcast objects
51
+
52
+ ### Authentication
53
+
54
+ First, you need to [register your application](http://dev.shelby.tv/myapps).
55
+
56
+ #### Web server application
57
+
58
+ Get a Shelby object:
59
+
60
+ shelbytv = Shelbytv::Base.new("CONSUMER_KEY", "CONSUMER_SECRET")
61
+
62
+ Redirect users to the Shelby authentication page. You need to pass your `callback_url`. Get the url to redirect to with:
63
+
64
+ shelbytv.authorize_url("CALLBACK_SESSION_URL")
65
+
66
+ Then Shelby.tv will redirect the user to your callback url with a code parameter in the url. Exchange this code for an access token using:
67
+
68
+ access_token = shelbytv.access_token("OAUTH_VERIFIER")
69
+
70
+ Now you can get a Shelby using only an access token and make requests on user's behalf:
71
+
72
+ shelbytv.auth_token = access_token.token
73
+ shelbytv.auth_secret = access_token.secret
74
+
75
+ #### License
76
+
77
+ See `LICENSE`
78
+
79
+ #### Built at [Hackday.tv](http://hackday.tv/)
80
+
@@ -0,0 +1,134 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'sinatra'
4
+
5
+ $:.unshift '../lib'
6
+
7
+ require 'shelbytv'
8
+
9
+ CONSUMER_KEY = '<INSERT YOUR KEY HERE>'
10
+ CONSUMER_SECRET = '<INSERT YOUR SECRET HERE>'
11
+
12
+ enable :sessions
13
+
14
+ get '/' do
15
+ erb :index
16
+ end
17
+
18
+ # Authentication
19
+
20
+ get '/auth' do
21
+ url = client.authorize_url
22
+ session[:request_token] = client.request_token
23
+ redirect url
24
+ end
25
+
26
+ get '/auth/callback' do
27
+ client.request_token = session.delete(:request_token)
28
+ access_token = client.access_token(params[:oauth_verifier])
29
+
30
+ session[:auth_token] = access_token.token
31
+ session[:auth_secret] = access_token.secret
32
+
33
+ redirect '/user'
34
+ end
35
+
36
+ # Utility
37
+
38
+ get '/logout' do
39
+ session.delete(:auth_token)
40
+ session.delete(:auth_secret)
41
+ session.delete(:user)
42
+ redirect '/'
43
+ end
44
+
45
+ # API / Fun Stuff!
46
+
47
+ get '/user' do
48
+ @user = session[:user] = client.user
49
+ erb :user
50
+ end
51
+
52
+ get '/user/channels' do
53
+ @channels = session[:user].channels
54
+ @user = session[:user]
55
+ erb :channels
56
+ end
57
+
58
+ get '/user/channels/:channel_id' do
59
+ @channel = client.channels.find(params[:channel_id])
60
+ @broadcasts = @channel.broadcasts
61
+ erb :channel
62
+ end
63
+
64
+ helpers do
65
+
66
+ def client
67
+ @_client ||= Shelbytv::Base.new(CONSUMER_KEY, CONSUMER_SECRET, session[:auth_token], session[:auth_secret])
68
+ end
69
+
70
+ end
71
+
72
+ __END__
73
+
74
+ @@ layout
75
+ <!DOCTYPE html>
76
+ <html>
77
+ <head>
78
+ <title>Simple App</title>
79
+ <style type='text/css'>
80
+ * { margin: 0; padding: 0; }
81
+
82
+ body {
83
+ font-family: 'Helvetica Neue', Helvetica, Arial, san-serif;
84
+ }
85
+
86
+ #container {
87
+ margin: 15px auto;
88
+ width: 800px;
89
+ }
90
+ </style>
91
+ </head>
92
+ <body>
93
+ <div id='container'>
94
+ <%= yield %>
95
+ </div>
96
+ </body>
97
+ </html
98
+
99
+ @@ index
100
+
101
+ <a href='/auth'>Sign in with Shelby</a>
102
+
103
+ @@ user
104
+
105
+ <h2>Welcome <%= @user.name %></h2>
106
+
107
+ <a href='/user/channels'>View channels</a>
108
+
109
+ @@ channels
110
+
111
+ <h2><%= @user.name %>'s Channels</h2>
112
+ <ul>
113
+ <% @channels.each do |channel| %>
114
+ <li>
115
+ <a href='/user/channels/<%= channel.id %>'><%= channel.name %></a>
116
+ </li>
117
+ <% end %>
118
+ </ul>
119
+
120
+ @@ channel
121
+
122
+ <h2><%= @channel.name %></h2>
123
+ <ul>
124
+ <% @broadcasts.each do |broadcast| %>
125
+ <li>
126
+ <strong><%= broadcast.name %></strong>
127
+ <%= broadcast.description %>
128
+ <br />
129
+ <a href='http://shelby.tv/#!/<%= broadcast.user_nickname %>/broadcasts/<%= broadcast.id %>'>
130
+ link
131
+ </a>
132
+ </li>
133
+ <% end %>
134
+ </ul>
data/lib/shelbytv.rb ADDED
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+
3
+ require "rubygems"
4
+ require "json"
5
+ require "time"
6
+ require "shelbytv/base"
7
+ require "shelbytv/broadcast"
8
+ require "shelbytv/channel"
9
+ require "shelbytv/channel_proxy"
10
+ require "shelbytv/user"
11
+ require "shelbytv/user_proxy"
12
+
13
+ module Shelbytv
14
+ end
@@ -0,0 +1,58 @@
1
+ require 'oauth/signature/plaintext'
2
+
3
+ module Shelbytv
4
+ class Base
5
+ API = "http://api.shelby.tv/v2/"
6
+
7
+ attr_accessor :auth_token, :auth_secret, :request_token
8
+
9
+ def initialize(*args)
10
+ if args.size == 2
11
+ @consumer_key, @consumer_secret = args
12
+ elsif args.size == 4
13
+ @consumer_key, @consumer_secret, @auth_token, @auth_secret = args
14
+ else
15
+ raise ArgumentError, "You need to pass in the consumer tokens as well as the auth tokens"
16
+ end
17
+ end
18
+
19
+ def channels
20
+ Shelbytv::ChannelProxy.new(self)
21
+ end
22
+
23
+ def user
24
+ Shelbytv::User.new(self, get('users.json').first)
25
+ end
26
+
27
+ def users
28
+ Shelbytv::UserProxy.new(self)
29
+ end
30
+
31
+ def get(path, params={})
32
+ query = params.map { |k,v| "#{k}=#{v}" }.join("&")
33
+ JSON.parse(client.get(API + path + "?" + OAuth::Helper.escape(query)).body)
34
+ end
35
+
36
+ def post(path, params={})
37
+ JSON.parse(client.get(API + path, params))
38
+ end
39
+
40
+ def authorize_url
41
+ consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, { :site=>"http://dev.shelby.tv" })
42
+ @request_token = consumer.get_request_token
43
+ @request_token.authorize_url
44
+ end
45
+
46
+ def access_token(oauth_verifier)
47
+ @request_token.get_access_token(:oauth_verifier => oauth_verifier)
48
+ end
49
+
50
+ private
51
+
52
+ def client
53
+ consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, { :signature_method => 'PLAINTEXT' })
54
+ OAuth::AccessToken.new(consumer, @auth_token, @auth_secret )
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,63 @@
1
+ module Shelbytv
2
+ class Broadcast
3
+ FIELDS = [ :channel_id,
4
+ :description,
5
+ :name,
6
+ :total_plays,
7
+ :user_id,
8
+ :user_nickname,
9
+ :user_thumbnail,
10
+ :video_description,
11
+ :video_id_at_provider,
12
+ :video_originator_user_image,
13
+ :video_originator_user_name,
14
+ :video_originator_user_nickname,
15
+ :video_player,
16
+ :video_provider_name,
17
+ :video_thumbnail_url,
18
+ :video_title,
19
+ :video_user_id,
20
+ :video_user_nickname,
21
+ :video_user_thumbnail,
22
+ :video_user_thumbnail,
23
+ :watched_by_owner ]
24
+
25
+ attr_reader :json
26
+
27
+ def initialize(shelbytv, json)
28
+ @shelbytv, @json = shelbytv, json
29
+ end
30
+
31
+ def id
32
+ @json['_id']
33
+ end
34
+
35
+ def channel
36
+ @shelbytv.channels.find(channel_id)
37
+ end
38
+
39
+ def created_at
40
+ Time.parse(@json['created_at'])
41
+ end
42
+
43
+ def method_missing(method_name, *args)
44
+ if FIELDS.include?(method_name)
45
+ @json[method_name.to_s]
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ def updated_at
52
+ Time.parse(@json['created_at'])
53
+ end
54
+
55
+ def user
56
+ @shelbytv.users.find(user_id)
57
+ end
58
+
59
+ def watched_by_owner?
60
+ watched_by_owner
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ module Shelbytv
2
+ class Channel
3
+ FIELDS = [ :name, :user_id ]
4
+
5
+ attr_reader :json
6
+
7
+ def initialize(shelbytv, json)
8
+ @shelbytv, @json = shelbytv, json
9
+ end
10
+
11
+ def id
12
+ @json['_id']
13
+ end
14
+
15
+ def broadcasts(options={})
16
+ @shelbytv.get("channels/#{id}/broadcasts.json", options).map do |item|
17
+ Shelbytv::Broadcast.new(@shelbytv, item)
18
+ end
19
+ end
20
+
21
+ def created_at
22
+ Time.parse(@json['created_at'])
23
+ end
24
+
25
+ def is_public
26
+ @json['public']
27
+ end
28
+
29
+ def is_public?
30
+ is_public
31
+ end
32
+
33
+ def method_missing(method_name, *args)
34
+ if FIELDS.include?(method_name)
35
+ @json[method_name.to_s]
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def updated_at
42
+ Time.parse(@json['created_at'])
43
+ end
44
+
45
+ def user
46
+ @shelbytv.users.find(user_id)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module Shelbytv
2
+ class ChannelProxy
3
+ def initialize(shelbytv)
4
+ @shelbytv = shelbytv
5
+ end
6
+
7
+ def all(options={})
8
+ @shelbytv.get('channels.json', options).map do |item|
9
+ Shelbytv::Channel.new(@shelbytv, item)
10
+ end
11
+ end
12
+
13
+ def find(id)
14
+ Shelbytv::Channel.new(@shelbytv, @shelbytv.get("channels/#{id}.json").first)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ module Shelbytv
2
+ class User
3
+ FIELDS = [ :name,
4
+ :nickname,
5
+ :total_videos_played,
6
+ :user_image ]
7
+
8
+ attr_reader :json
9
+
10
+ def initialize(shelbytv, json)
11
+ @shelbytv, @json = shelbytv, json
12
+ end
13
+
14
+ def id
15
+ @json['_id']
16
+ end
17
+
18
+ def created_at
19
+ Time.parse(@json['created_at'])
20
+ end
21
+
22
+ def channels(options={})
23
+ @shelbytv.get("channels.json", options).map do |item|
24
+ Shelbytv::Channel.new(@shelbytv, item)
25
+ end
26
+ end
27
+
28
+ def method_missing(method_name, *args)
29
+ if FIELDS.include?(method_name)
30
+ @json[method_name.to_s]
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def profile_image
37
+ user_image
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ module Shelbytv
2
+ class UserProxy
3
+ def initialize(shelbytv)
4
+ @shelbytv = shelbytv
5
+ end
6
+
7
+ def find(id)
8
+ Shelbytv::User.new(@shelbytv, @shelbytv.get("users/#{id}.json").first)
9
+ end
10
+ end
11
+ end
data/spec/UHHHHHH ADDED
@@ -0,0 +1 @@
1
+ Check back later?
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shelbytv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benny Wong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth
16
+ requirement: &70129727750880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.4.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70129727750880
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70129727746900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129727746900
36
+ description:
37
+ email: benny@bwong.net
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - examples/simple_app.rb
43
+ - lib/shelbytv/base.rb
44
+ - lib/shelbytv/broadcast.rb
45
+ - lib/shelbytv/channel.rb
46
+ - lib/shelbytv/channel_proxy.rb
47
+ - lib/shelbytv/user.rb
48
+ - lib/shelbytv/user_proxy.rb
49
+ - lib/shelbytv.rb
50
+ - README.md
51
+ - spec/UHHHHHH
52
+ homepage: https://github.com/bdotdub/shelbytv
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A Shelby.tv API wrapper
76
+ test_files: []