Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jul 12, 2011
1 parent 6ad5238 commit 42de457
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1 +1,2 @@
config.local.yml config.local.yml
tmp
23 changes: 21 additions & 2 deletions app.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
$stderr.puts 'IndexTank search for "%s" (%.3f s)' % [payload[:query], ending - start] $stderr.puts 'IndexTank search for "%s" (%.3f s)' % [payload[:query], ending - start]
end end


strip_params = %w[access_token client_id] strip_params = %w[access_token client_id client_secret]


ActiveSupport::Notifications.subscribe('request.faraday') do |name, start, ending, _, payload| ActiveSupport::Notifications.subscribe('request.faraday') do |name, start, ending, _, payload|
url = payload[:url] url = payload[:url]
if (url.query_values.keys & strip_params).any? if url.query_values and (url.query_values.keys & strip_params).any?
url = url.dup url = url.dup
url.query_values = url.query_values.reject { |k,| strip_params.include? k } url.query_values = url.query_values.reject { |k,| strip_params.include? k }
end end
Expand Down Expand Up @@ -173,6 +173,25 @@ def last_modified_from_photos(photos)
builder :feed, layout: false builder :feed, layout: false
end end


get '/login' do
return_url = request.url.split('?').first
begin
if params[:code]
token_response = Instagram::get_access_token(return_to: return_url, code: params[:code])
user = User.from_token token_response.body
redirect user_url(user.username)
elsif params[:error]
status 401
haml "%h1 Can't login: #{params[:error_description]}"
else
redirect Instagram::authorization_url(return_to: return_url).to_s
end
rescue Faraday::Error::ClientError => error
status 500
haml "%h1 Instagram error: #{error.response[:body]['error_message']}"
end
end

get '/users/:id.atom' do get '/users/:id.atom' do
@user = User[params[:id]] @user = User[params[:id]]
@photos = @user.photos params[:max_id] @photos = @user.photos params[:max_id]
Expand Down
51 changes: 43 additions & 8 deletions instagram.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,7 +19,31 @@ def parse(body)
Hashie::Mash.new(body) Hashie::Mash.new(body)
end end
end end


class OAuthRequest < Faraday::Middleware
def initialize(app, options)
super(app)
@config = options[:config]
end

def call(env)
unless env[:request][:oauth] == false
params = %w[client_id client_secret access_token].each_with_object({}) do |key, hash|
value = @config.send(key)
hash[key] = value if value.present?
end
if env[:method] == :get
url = env[:url]
url.query_values = params.update(url.query_values || {})
env[:url] = url
else
env[:body] = params.update(env[:body] || {})
end
end
@app.call(env)
end
end

class PreserveRawBody < Faraday::Response::Middleware class PreserveRawBody < Faraday::Response::Middleware
def on_complete(env) def on_complete(env)
env[:raw_body] = env[:body] env[:raw_body] = env[:body]
Expand All @@ -30,20 +54,18 @@ module Connection
def connection def connection
@connection ||= begin @connection ||= begin
conn = Faraday.new('https://api.instagram.com/v1/') do |b| conn = Faraday.new('https://api.instagram.com/v1/') do |b|
b.use OAuthRequest, config: self
b.request :url_encoded
b.use Mashify b.use Mashify
b.use FaradayStack::ResponseJSON, content_type: 'application/json'
b.use PreserveRawBody
b.use FaradayStack::Caching, cache, strip_params: %w[access_token client_id] unless cache.nil? b.use FaradayStack::Caching, cache, strip_params: %w[access_token client_id] unless cache.nil?
b.response :raise_error b.response :raise_error
b.use FaradayStack::ResponseJSON, content_type: 'application/json'
b.use PreserveRawBody
b.use FaradayStack::Instrumentation b.use FaradayStack::Instrumentation
b.adapter Faraday.default_adapter b.adapter Faraday.default_adapter
end end


# conn.token_auth access_token unless access_token.nil?
conn.params['access_token'] = access_token unless access_token.nil?
conn.params['client_id'] = client_id
conn.headers['User-Agent'] = 'instagram.heroku.com ruby client' conn.headers['User-Agent'] = 'instagram.heroku.com ruby client'

conn conn
end end
end end
Expand All @@ -54,6 +76,18 @@ def get(path, params = nil)
end end
end end
end end

module OAuthMethods
def authorization_url(options)
connection.build_url '/oauth/authorize', client_id: client_id,
redirect_uri: options[:return_to], response_type: 'code'
end

def get_access_token(options)
connection.post '/oauth/access_token', code: options[:code],
grant_type: 'authorization_code', redirect_uri: options[:return_to]
end
end


module ApiMethods module ApiMethods
def get(path, params = nil) def get(path, params = nil)
Expand Down Expand Up @@ -85,5 +119,6 @@ def tag_recent_media(tag, *args)


extend Configuration extend Configuration
extend Connection extend Connection
extend OAuthMethods
extend ApiMethods extend ApiMethods
end end
12 changes: 12 additions & 0 deletions models.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ def self.[](id)
end end
end end
end end

def self.from_token(token)
id = token.user.id
(first(user_id: id.to_i) || new(user_id: id.to_i)).tap do |user|
if user.username and user.username != token.user.username
user['old_username'] = user.username
end
user.username = token.user.username
user['access_token'] = token.access_token
user.save
end
end


def self.find_by_instagram_url(url) def self.find_by_instagram_url(url)
id = Instagram::Discovery.discover_user_id(url) id = Instagram::Discovery.discover_user_id(url)
Expand Down
9 changes: 7 additions & 2 deletions views/help.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
%section %section
%h2 How do I discover my own Instagram photos? %h2 How do I discover my own Instagram photos?


%p <strong>If you recently tweeted</strong> a link to your Instagram photo, enter your Twitter username and the app will search for that link and try to lookup your Instagram user. %p To see your photo stream, you need to log in:

%p
%a#login{ href: '/login' } Log in with Instagram

%p Alternatively, you can enter a Twitter username of a person who <strong>recently tweeted</strong> a link to their Instagram photo:


%form{ action: '/users/discover', method: 'post', class: 'detect' } %form{ action: '/users/discover', method: 'post', class: 'detect' }
%p %p
Expand All @@ -22,7 +27,7 @@
%input{ type: 'text', name: 'twitter' } %input{ type: 'text', name: 'twitter' }
%input{ type: 'submit', value: 'Search' } %input{ type: 'submit', value: 'Search' }


%p Alternatively, you can paste <strong>a permalink</strong> to one of your Instagram photos: %p Another option is to paste <strong>a permalink</strong> to one of your Instagram photos:


%form{ action: '/users/discover', method: 'post', class: 'detect' } %form{ action: '/users/discover', method: 'post', class: 'detect' }
%p %p
Expand Down
16 changes: 16 additions & 0 deletions views/style.scss
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ article {
max-width: 40em; max-width: 40em;
} }


#login {
display: inline-block;
text-decoration: none;
@include border-radius(5px);
border: 1px solid silver;
padding: .5em 1em;
background: #ddd;
color: black;
text-shadow: rgba(white, .5) 1px 1px 1px;
font-variant: small-caps;
font-size: 1em;
font-weight: 600;
margin: 1.2em 1.5em;
&:hover { background: #eee; color: darkblue }
}

form.detect { form.detect {
padding: .1em 1em; padding: .1em 1em;
background: #f8f8f8; background: #f8f8f8;
Expand Down

0 comments on commit 42de457

Please sign in to comment.