foursquare_rails_generators 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +2 -0
- data/foursquare_rails_generators.gemspec +12 -0
- data/lib/foursquare_rails_generators.rb +4 -0
- data/lib/generators/foursquare/authentication/authentication_generator.rb +61 -0
- data/lib/generators/foursquare/authentication/templates/controllers/clients/foursquare_clients_controller.rb +24 -0
- data/lib/generators/foursquare/authentication/templates/controllers/users_controller.rb +14 -0
- data/lib/generators/foursquare/authentication/templates/migrations/create_foursquare_user.rb +11 -0
- data/lib/generators/foursquare/authentication/templates/models/foursquare_client.rb +51 -0
- data/lib/generators/foursquare/authentication/templates/models/foursquare_user.rb +23 -0
- data/lib/generators/foursquare/push/push_generator.rb +17 -0
- data/lib/generators/foursquare/push/templates/controllers/checkins_controller.rb +11 -0
- metadata +14 -2
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/README.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'foursquare_rails_generators'
|
3
|
+
s.version = '0.1.2'
|
4
|
+
s.date = '2012-10-26'
|
5
|
+
s.summary = "Foursquare Rails Generators"
|
6
|
+
s.description = "Collection of Rails generators for the Foursquare API"
|
7
|
+
s.authors = ["Pierre Valade"]
|
8
|
+
s.email = 'pierre@foursquare.com'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.require_paths = ['lib']
|
11
|
+
s.homepage = 'http://rubygems.org/gems/foursquare_rails_generators'
|
12
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Foursquare
|
2
|
+
module Generators
|
3
|
+
class AuthenticationGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
desc "Adds Foursquare Connect to your app"
|
7
|
+
argument :settings, type: :hash, banner: "client_id:id client_secret:secret"
|
8
|
+
|
9
|
+
def validates_settings
|
10
|
+
raise "incorret params" if settings['client_id'].blank? || settings['client_secret'].blank?
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_oauth2_gem
|
14
|
+
gem 'oauth2'
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_foursquare_user_model
|
18
|
+
migration_template 'migrations/create_foursquare_user.rb'
|
19
|
+
template 'models/foursquare_user.rb', File.join('app/models/', 'foursquare_user.rb')
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_foursquare_client_model
|
23
|
+
template 'models/foursquare_client.rb', File.join('app/models/', 'foursquare_client.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
def run_migrations
|
27
|
+
rake 'db:migrate'
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_client_controller
|
31
|
+
template 'controllers/clients/foursquare_clients_controller.rb', File.join('app/controllers/clients', 'foursquare_clients_controller.rb')
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_root_routes
|
35
|
+
route "root :to => 'users#new'"
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_clients_routes
|
39
|
+
route(
|
40
|
+
%Q{
|
41
|
+
scope module: 'clients' do
|
42
|
+
resources :foursquare_clients, only: ['new'], path: 'clients/foursquare' do
|
43
|
+
collection do
|
44
|
+
get 'callback'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
}
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_users_controller
|
53
|
+
template 'controllers/users_controller.rb', File.join('app/controllers/', 'users_controller.rb')
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_user_routes
|
57
|
+
route "resource :user"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Clients::FoursquareClientsController < ApplicationController
|
2
|
+
def new
|
3
|
+
# https://developer.foursquare.com/overview/auth
|
4
|
+
options = {
|
5
|
+
redirect_uri: callback_foursquare_clients_url,
|
6
|
+
}
|
7
|
+
redirect_to client.auth_code.authorize_url options
|
8
|
+
end
|
9
|
+
|
10
|
+
def callback
|
11
|
+
if params[:error]
|
12
|
+
# TODO: manage error
|
13
|
+
end
|
14
|
+
token = client.auth_code.get_token params[:code], redirect_uri: callback_foursquare_clients_url
|
15
|
+
user = FoursquareUser.find_or_create_by_access_token(token.token)
|
16
|
+
session[:user_id] = user.id
|
17
|
+
redirect_to user_path
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def client
|
22
|
+
FoursquareClient.oauth2_client
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class UsersController < ApplicationController
|
2
|
+
def new
|
3
|
+
render text: "<a href='#{new_foursquare_client_path}'><img src='https://playfoursquare.s3.amazonaws.com/press/logo/connect-black.png' /></a>"
|
4
|
+
end
|
5
|
+
|
6
|
+
def show
|
7
|
+
render text: "hello #{current_user.name}"
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def current_user
|
12
|
+
@current_user ||= FoursquareUser.find(session[:user_id])
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# https://developer.foursquare.com/docs/
|
2
|
+
class FoursquareClient
|
3
|
+
attr_accessor :access_token
|
4
|
+
|
5
|
+
def initialize(access_token)
|
6
|
+
@access_token = access_token
|
7
|
+
end
|
8
|
+
|
9
|
+
def user_info
|
10
|
+
@user_info ||= get('users/self').parsed['response']['user']
|
11
|
+
end
|
12
|
+
|
13
|
+
def user_id
|
14
|
+
user_info['id']
|
15
|
+
end
|
16
|
+
|
17
|
+
def user_name
|
18
|
+
[user_info['firstName'], user_info['lastName']].join(' ')
|
19
|
+
end
|
20
|
+
|
21
|
+
def oauth2_token
|
22
|
+
@oauth2_token ||= OAuth2::AccessToken.from_hash(self.class.oauth2_client, {access_token: access_token}.merge(mode: :query, param_name: 'oauth_token'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(path, params = {})
|
26
|
+
oauth2_token.get(self.class.endpoint + path, params)
|
27
|
+
end
|
28
|
+
|
29
|
+
def post(path, params = {})
|
30
|
+
oauth2_token.post(self.class.endpoint + path, params)
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_checkin_reply(checkin_id, options = {})
|
34
|
+
post("checkins/#{checkin_id}/reply", params: options)
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self
|
38
|
+
def endpoint
|
39
|
+
'https://api.foursquare.com/v2/'
|
40
|
+
end
|
41
|
+
|
42
|
+
def oauth2_client
|
43
|
+
options = {
|
44
|
+
authorize_url: 'https://foursquare.com/oauth2/authenticate',
|
45
|
+
token_url: 'https://foursquare.com/oauth2/access_token',
|
46
|
+
raise_errors: true,
|
47
|
+
}
|
48
|
+
OAuth2::Client.new("<%= settings['client_id'] %>", "<%= settings['client_secret'] %>", options)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class FoursquareUser < ActiveRecord::Base
|
2
|
+
def name
|
3
|
+
client.user_name
|
4
|
+
end
|
5
|
+
|
6
|
+
def client
|
7
|
+
@client ||= FoursquareClient.new(access_token)
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def find_or_create_by_access_token(access_token)
|
12
|
+
client = FoursquareClient.new(access_token)
|
13
|
+
|
14
|
+
unless user = self.find_by_foursquare_id(client.user_id)
|
15
|
+
user = self.new
|
16
|
+
user.foursquare_id = client.user_id
|
17
|
+
user.access_token = client.access_token
|
18
|
+
user.save!
|
19
|
+
end
|
20
|
+
user
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Foursquare
|
2
|
+
module Generators
|
3
|
+
class PushGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
desc "Receive push from foursquare into your app"
|
7
|
+
|
8
|
+
def add_routes
|
9
|
+
route "resources :checkins"
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_checkins_controller
|
13
|
+
template 'controllers/checkins_controller.rb', File.join('app/controllers/', 'checkins_controller.rb')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class CheckinsController < ApplicationController
|
2
|
+
skip_before_filter :verify_authenticity_token
|
3
|
+
|
4
|
+
def create
|
5
|
+
checkin = JSON.parse(params['checkin'])
|
6
|
+
# TODO: verify push secret
|
7
|
+
user = FoursquareUser.find_by_foursquare_id(checkin['user']['id'])
|
8
|
+
user.client.create_checkin_reply(checkin['id'], text: "hello world!")
|
9
|
+
return head :ok
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foursquare_rails_generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -16,7 +16,19 @@ email: pierre@foursquare.com
|
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
|
-
files:
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- README.md
|
22
|
+
- foursquare_rails_generators.gemspec
|
23
|
+
- lib/foursquare_rails_generators.rb
|
24
|
+
- lib/generators/foursquare/authentication/authentication_generator.rb
|
25
|
+
- lib/generators/foursquare/authentication/templates/controllers/clients/foursquare_clients_controller.rb
|
26
|
+
- lib/generators/foursquare/authentication/templates/controllers/users_controller.rb
|
27
|
+
- lib/generators/foursquare/authentication/templates/migrations/create_foursquare_user.rb
|
28
|
+
- lib/generators/foursquare/authentication/templates/models/foursquare_client.rb
|
29
|
+
- lib/generators/foursquare/authentication/templates/models/foursquare_user.rb
|
30
|
+
- lib/generators/foursquare/push/push_generator.rb
|
31
|
+
- lib/generators/foursquare/push/templates/controllers/checkins_controller.rb
|
20
32
|
homepage: http://rubygems.org/gems/foursquare_rails_generators
|
21
33
|
licenses: []
|
22
34
|
post_install_message:
|