Permalink
Browse files

some api calls working locally, very hardcoded

  • Loading branch information...
1 parent abe0dbf commit b437defc31af69247b9beb1e54834ab8db171e9b @cohuman committed Mar 21, 2012
Showing with 42 additions and 19 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 Gemfile
  3. +4 −0 Gemfile.lock
  4. +36 −19 coho.rb
View
@@ -1,2 +1,3 @@
config/cohuman.yml
.bundle
+.idea/*
View
@@ -3,3 +3,4 @@ gem "sinatra"
gem "oauth"
gem "erector"
gem "json"
+gem 'rest-client'
View
@@ -5,10 +5,13 @@ GEM
rake
treetop (>= 1.2.3)
json (1.5.1)
+ mime-types (1.17.2)
oauth (0.4.4)
polyglot (0.3.1)
rack (1.2.1)
rake (0.8.7)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
sinatra (1.1.2)
rack (~> 1.1)
tilt (~> 1.2)
@@ -23,4 +26,5 @@ DEPENDENCIES
erector
json
oauth
+ rest-client
sinatra
View
55 coho.rb
@@ -7,6 +7,7 @@
require 'json'
require 'oauth'
require 'oauth/consumer'
+require 'rest-client'
require './page'
@@ -26,30 +27,27 @@ def site
class Cohuman
def self.credentials
- @credentials ||= if ENV['COHUMAN_API_KEY']
- {:key => ENV['COHUMAN_API_KEY'], :secret => ENV['COHUMAN_API_SECRET']}
- else
- begin
- here = File.expand_path(File.dirname(__FILE__))
- YAML.load( File.read( "#{here}/config/cohuman.yml") )
- rescue Errno::ENOENT
- nil
- end
- end
+ {
+ :key => '29f16b7cef111cb8223614ca38080c8f6fdb2651',
+ :secret => '82916a0c268d306ec8252367db6f9758d8f0625b'
+ }
end
def self.consumer
+ puts "---===---"
+ puts credentials[:key]
+ puts credentials[:secret]
@consumer ||= OAuth::Consumer.new( credentials[:key], credentials[:secret], {
- :site => 'http://api.cohuman.com',
+ :site => 'http://localhost:3000',
:request_token_path => '/api/token/request',
:authorize_path => '/api/authorize',
:access_token_path => '/api/token/access'
- })
+ })
end
def self.api_url(path)
path.gsub!(/^\//,'') # removes leading slash from path
- url = "http://api.cohuman.com/#{path}"
+ url = "http://localhost:3000/#{path}"
end
end
@@ -66,8 +64,13 @@ def render_page(query = nil, result = nil)
get "/authorize" do
if Cohuman.credentials
- request_token = Cohuman.consumer.get_request_token(:oauth_callback=>"#{request.site}/authorized")
+ request_token = Cohuman.consumer.get_request_token(
+ :oauth_callback=>"#{request.site}/api/authorize"
+ )
session[:request_token] = request_token
+ puts "______ Request Token"
+ puts request_token.inspect
+ puts "Redirecting to " + request_token.authorize_url
redirect request_token.authorize_url
else
erector {
@@ -128,18 +131,32 @@ def render_page(query = nil, result = nil)
def get_and_render(path)
url = Cohuman.api_url(path)
response = session[:access_token].get(url, {"Content-Type" => "application/json"})
- result = JSON.parse(response.body)
- render_page(url, result)
+ render_page(url, JSON.parse(response.body))
end
+# Do it through the Oauth gem
get "/tasks" do
- get_and_render "/tasks"
+ url = "http://localhost:3000/tasks"
+ response = session[:access_token].get(url, {"Content-Type" => "application/json"})
+ render_page(url, JSON.parse(response.body))
end
+# Do it with rest-client
get "/users" do
- get_and_render "/users?limit=0"
+ RestClient.add_before_execution_proc do |req, params|
+ session[:access_token].sign! req
+ end
+ url = "http://localhost:3000/users?limit=5"
+ response = RestClient.get(url, "Content-Type" => "application/json")
+ puts response.inspect
+ render_page(url, JSON.parse(response))
end
+# Do a POST with the oauth gem
get "/projects" do
- get_and_render "/projects?limit=0"
+ url = 'http://localhost:3000/task/1643785/comment'
+ response = session[:access_token].post(url, {:text => 'Hello Then', :format => 'json'}, {"Content-Type" => "application/json"})
+ render_page(url, JSON.parse(response.body))
end
+
+

0 comments on commit b437def

Please sign in to comment.