Skip to content

Commit

Permalink
Merge pull request #2 from NicheProject/followers
Browse files Browse the repository at this point in the history
Followers + authentication errors
  • Loading branch information
Jay Stakelon committed Feb 18, 2014
2 parents fb64ddb + e8709b8 commit 27187f1
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 170 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ It pretty much goes without saying that this wasn't authorized by Vine or anyone
client.user_timeline('908082141764657152')
client.user_timeline('908082141764657152', :page => 2)

# Get a user's followers/following by their user ID
client.followers('908082141764657152')
client.following('908082141764657152', :page => 2)

# Get popular and promoted videos
client.popular
client.popular(:page => 2)
Expand Down
40 changes: 36 additions & 4 deletions lib/redvine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

class Redvine

class Error < StandardError; end
class ConnectionError < Redvine::Error
attr_reader :code

def initialize(code)
@code = code
end
end

class AuthenticationRequiredError < Redvine::Error
def initialize(msg="You must authenticate as a valid Vine user (call #connect) before accessing other API methods")
super(msg)
end
end

attr_reader :vine_key, :username, :user_id

@@baseUrl = 'https://api.vineapp.com/'
Expand All @@ -15,9 +30,14 @@ def connect(opts={})
query = {username: opts[:email], password: opts[:password], deviceToken: @@deviceToken}
headers = {'User-Agent' => @@userAgent}
response = HTTParty.post(@@baseUrl + 'users/authenticate', {body: query, headers: headers})
@vine_key = response.parsed_response['data']['key']
@username = response.parsed_response['data']['username']
@user_id = response.parsed_response['data']['userId']

if opts[:skip_exception] || response['success']
@vine_key = response.parsed_response['data']['key']
@username = response.parsed_response['data']['username']
@user_id = response.parsed_response['data']['userId']
else
raise Redvine::ConnectionError.new(response['code'].to_i), response['error']
end
end

def search(tag, opts={})
Expand All @@ -37,6 +57,16 @@ def timeline(opts={})
get_request_data('timelines/graph', opts)
end

def following(uid,opts={})
raise(ArgumentError, 'You must specify a user id') if !uid
get_request_data("users/#{uid}/following", opts)
end

def followers(uid,opts={})
raise(ArgumentError, 'You must specify a user id') if !uid
get_request_data("users/#{uid}/followers", opts)
end

def user_profile(uid)
raise(ArgumentError, 'You must specify a user id') if !uid
get_request_data('users/profiles/' + uid, {}, false)
Expand Down Expand Up @@ -71,11 +101,13 @@ def session_headers
end

def get_request_data(endpoint, query={}, records=true)
raise Redvine::AuthenticationRequiredError unless @vine_key

query.merge!(:size => 20) if query.has_key?(:page) && !query.has_key?(:size)
args = {:headers => session_headers}
args.merge!(:query => query) if query != {}
response = HTTParty.get(@@baseUrl + endpoint, args).parsed_response
return Hashie::Mash.new(JSON.parse('{"success": false}')) if response.kind_of?(String)
return Hashie::Mash.new(JSON.parse('{"success": false}')) if response.kind_of?(String)
if response['success'] == false
response['error'] = true
return Hashie::Mash.new(response)
Expand Down
1 change: 1 addition & 0 deletions redvine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'httparty'
gem.add_dependency 'hashie'

gem.add_development_dependency 'debugger'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'webmock'
Expand Down
Loading

0 comments on commit 27187f1

Please sign in to comment.