Last active
December 15, 2015 15:18
-
-
Save romikoops/5280333 to your computer and use it in GitHub Desktop.
Ror_desk migration to Rails3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
rescue_from ::RubyDesk::BadRequest, with: :require_token | |
protected | |
# Used as a before filter for methods that will access oDesk APIs | |
# It guarantees that the user permits this application to use oDesk APIs | |
def odesk_required | |
create_odesk_connector | |
unless session[:odesk_api_token] | |
session[:odesk_back_url] = request.url | |
redirect_to @odesk_connector.auth_url | |
end | |
end | |
# Creates the oDesk connector on each request | |
# It relies on an API token stored in SessionStore | |
def create_odesk_connector | |
@odesk_connector = RubyDesk::Connector.new( | |
Settings.odesk.api_key, | |
Settings.odesk.api_secret, | |
session[:odesk_api_token] | |
) | |
@odesk_connector.auth_user = session[:odesk_auth_user] | |
# Return the created connector | |
@odesk_connector | |
end | |
# Causes the application to acquire a new token | |
# This is usually called when the old token has expired | |
def require_token | |
# Remove any saved tokens | |
session[:odesk_api_token] = nil | |
# Causes the application to redirect to oDesk and ask for a new token | |
odesk_required | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to your config file | |
config.after_initialize do | |
RubyDesk.logger = Rails.logger | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
development: | |
odesk: | |
api_key: 'asdfasdfasdfasdfas' | |
api_secret: 'asdfsasdf' | |
test: | |
odesk: | |
api_key: 'asdfasdfasdfasdfas' | |
api_secret: 'asdfsasdf' | |
production: | |
odesk: | |
api_key: 'asdfasdfasdfasdfas' | |
api_secret: 'asdfsasdf' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
OdeskJobs::Application.routes.draw do | |
root to: "home#index" | |
resource :odesk_auth, only: [:create, :show] | |
get 'example' => 'example#index', as: :time_reports | |
get 'example/search_jobs' => 'example#search_jobs', as: :search_jobs | |
get 'example/team_rooms' => 'example#team_rooms', as: :team_rooms | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Working hours for this week</h2> | |
<div class="time_report"> | |
<table> | |
<% if @time_report.empty? -%> | |
No time logged for this week.<br/> | |
Want to search for cool new jobs? | |
<% form_tag({:action=>'search_jobs'}, :method=>:get) do -%> | |
<%= text_field_tag :query %> | |
<%= submit_tag "search" %> | |
<% end -%> | |
<% else -%> | |
<% @time_report.each do |entry| -%> | |
<tr> | |
<td><%= entry['team_name'] %></td> | |
<td><%= entry['hours'] %></td> | |
</tr> | |
<% end -%> | |
<% end -%> | |
</table> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Available jobs</h1> | |
<% @jobs.each do |job| %> | |
<div class="job"> | |
<%= link_to(job.op_title, "http://www.odesk.com/jobs/#{job.profile_key}") %> | |
</div> | |
<% end -%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Teams</h1> | |
<% @team_rooms.each do |team_room| -%> | |
<%= team_room.company_name %><br/> | |
<% end -%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleController < ApplicationController | |
before_filter :odesk_required | |
def index | |
@time_report = RubyDesk::TimeReport.find(@odesk_connector, | |
:select=>"team_name, sum(hours)", | |
:conditions=>{:worked_on=>Date.today.monday, :provider_id=>@odesk_connector.auth_user.uid}) | |
if @time_report && @time_report.size > 1 | |
total_hours = @time_report.sum{|entry| entry['hours']} | |
@time_report.insert(0, {'team_name'=>"total", 'hours'=>total_hours}) | |
end | |
end | |
def search_jobs | |
@jobs = RubyDesk::Job.search(@odesk_connector, params[:query]) | |
end | |
def team_rooms | |
@team_rooms = RubyDesk::TeamRoom.get_teamrooms(@odesk_connector) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rails', '3.2.13' | |
gem 'ruby_desk' | |
gem "settingslogic" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%div | |
- unless session[:odesk_api_token].blank? | |
%br | |
%a{href: time_reports_path} Show Time Reports | |
%br | |
%a{href: search_jobs_path} Search Jobs | |
%br | |
%a{href: team_rooms_path} Team Room List | |
- else | |
= link_to 'SignIn with oDesk', odesk_auth_path, method: :post |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
class HomeController < ApplicationController | |
def index | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
class Settings < Settingslogic | |
source "#{Rails.root}/config/application.yml" | |
namespace Rails.env | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OdeskAuthsController < ApplicationController | |
before_filter :odesk_required, only: [:create] | |
def create | |
end | |
def show | |
create_odesk_connector | |
@odesk_connector.frob = params[:frob] | |
session[:odesk_api_token] = @odesk_connector.get_token | |
session[:odesk_auth_user] = @odesk_connector.auth_user | |
redirect_to root_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment