Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tasks creation.

  • Loading branch information...
commit 1808e38e9085144cfdfc0ae9a77cde9f88a9ad8b 1 parent 9a206f8
@jrichardlai authored
View
1  .rspec
@@ -1,3 +1,2 @@
--colour
---format=Fuubar
--debug
View
10 lib/taskrabbit/api.rb
@@ -22,11 +22,15 @@ def initialize(user_token = nil, attrs = {})
self.user_token = user_token if user_token
end
- def request_params(transformer)
+ def request_params(transformer, options = {})
{
:transform => transformer,
+ :extra_body => options,
:extra_request => {
- :headers => {'X-Client-Application' => client_secret.to_s},
+ :headers => {
+ 'X-Client-Application' => client_secret.to_s,
+ 'Authorization' => "OAuth #{user_token.to_s}"
+ },
:endpoint => endpoint.to_s,
:base_uri => base_uri.to_s
}
@@ -40,7 +44,7 @@ def check_response_errors(response)
end
def request(method, path, transformer, options = {})
- send(method, path, request_params(transformer).merge(options))
+ send(method, path, request_params(transformer, options))
end
def users
View
4 lib/taskrabbit/proxy.rb
@@ -17,7 +17,7 @@ def all(options={})
def find(param, options={})
return all(options) if param == :all
- return @target.find(param, @api) if @target.respond_to?(:find)
+ return @target.find(@api, param) if @target.respond_to?(:find)
# return proxy_found(options).detect { |document| document.id == param }
end
@@ -32,7 +32,7 @@ def find(param, options={})
# end
def create(args)
- @target.create(args, api) if @target.respond_to?(:create)
+ @target.create(@api, args) if @target.respond_to?(:create)
# object = @target.new(args.merge({:api => @api}))
# if obj = object.create
# return obj
View
6 lib/taskrabbit/task.rb
@@ -8,7 +8,7 @@ def all(api, options = {})
@found = api.request('get', 'tasks', Api::collection_transformers[self], options)
end
- def find(id, api)
+ def find(api, id)
# if @found
# find from users tasks if loaded
# @found.detect { |document| document.id == id }
@@ -16,6 +16,10 @@ def find(id, api)
api.request('get', "tasks/#{id.to_s}", self)
# end
end
+
+ def create(api, params)
+ api.request('post', "tasks", self, :task => params)
+ end
end
end
end
View
6 spec/support/cassettes/tasks.yml
@@ -5,6 +5,8 @@
uri: http://localhost:3000/api/v1/tasks?
body:
headers:
+ oauth:
+ - ""
x-client-application:
- euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
response: !ruby/struct:VCR::Response
@@ -17,11 +19,11 @@
content-type:
- application/json; charset=utf-8
x-runtime:
- - "1205"
+ - "5825"
server:
- WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
date:
- - Sun, 30 Oct 2011 21:06:48 GMT
+ - Sun, 30 Oct 2011 22:57:14 GMT
content-length:
- "7860"
cache-control:
View
32 spec/support/cassettes/tasks/create.yml
@@ -0,0 +1,32 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://localhost:3000/api/v1/tasks?
+ body: task[name]=My%20First%20Task&task[named_price]=20&task[city_id]=4
+ headers:
+ x-client-application:
+ - euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
+ authorization:
+ - OAuth cNPyB6KypcdDjFb8KhQ7EOZzBpwcenMMvnQBbSHM
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ etag:
+ - "\"50964bfb7611739f90813907c251242f\""
+ content-type:
+ - application/json; charset=utf-8
+ x-runtime:
+ - "3800"
+ server:
+ - WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
+ date:
+ - Mon, 31 Oct 2011 02:46:50 GMT
+ content-length:
+ - "612"
+ cache-control:
+ - private, max-age=0, must-revalidate
+ body: "{\"name\":\"My First Task\",\"city\":{\"name\":\"New York City\",\"lng\":-74.005973,\"id\":4,\"links\":{\"get\":\"/api/v1/cities/4\"},\"lat\":40.714353},\"named_price\":20,\"complete_by_time\":1320087600,\"id\":22549,\"state_changed_at\":1320029209,\"cost_in_cents\":0,\"links\":{\"html\":\"http://local.taskrabbit.com/tasks/my-first-task--3\",\"get\":\"/api/v1/tasks/22549\",\"delete\":\"/api/v1/tasks/22549\",\"put\":\"/api/v1/tasks/22549\",\"authenticated\":\"http://local.taskrabbit.com/go/a8c54394b14a3bd493fb73dd4fdf93da99421faf864ff9db32279584b58d603775d9fad96a236b34a736adbd3da524a513b0\"},\"state_label\":\"posted\",\"assign_by_time\":1320073200,\"state\":\"opened\"}"
+ http_version: "1.1"
View
30 spec/support/cassettes/tasks/create/with_invalid_params.yml
@@ -0,0 +1,30 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://localhost:3000/api/v1/tasks?
+ body:
+ headers:
+ x-client-application:
+ - euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
+ authorization:
+ - OAuth cNPyB6KypcdDjFb8KhQ7EOZzBpwcenMMvnQBbSHM
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 422
+ message:
+ headers:
+ content-type:
+ - application/json; charset=utf-8
+ x-runtime:
+ - "331"
+ server:
+ - WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
+ date:
+ - Mon, 31 Oct 2011 02:48:16 GMT
+ content-length:
+ - "365"
+ cache-control:
+ - no-cache
+ body: "{\"errors\":{\"messages\":[\"Task title can't be blank\",\"Amount you are willing to pay needs to be a whole dollar amount greater than zero\"],\"fields\":[[\"name\",\"can't be blank\"],[\"named_price\",\"needs to be a whole dollar amount greater than zero\"]]},\"error\":\"Task title can't be blank, \\nAmount you are willing to pay needs to be a whole dollar amount greater than zero\"}"
+ http_version: "1.1"
View
30 spec/support/cassettes/tasks/create/without_credit_card.yml
@@ -0,0 +1,30 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://localhost:3000/api/v1/tasks?
+ body: task[name]=My%20First%20Task&task[named_price]=20&task[city_id]=4
+ headers:
+ x-client-application:
+ - euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
+ authorization:
+ - OAuth sjCuNHsxMRkFiJGpLWZzYJksDjfnXtDvDcPuuDkn
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 201
+ message: Created
+ headers:
+ content-type:
+ - application/json; charset=utf-8
+ x-runtime:
+ - "2530"
+ server:
+ - WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
+ date:
+ - Mon, 31 Oct 2011 02:59:19 GMT
+ content-length:
+ - "691"
+ cache-control:
+ - no-cache
+ body: "{\"name\":\"My First Task\",\"city\":{\"name\":\"New York City\",\"lng\":-74.005973,\"id\":4,\"links\":{\"get\":\"/api/v1/cities/4\"},\"lat\":40.714353},\"named_price\":20,\"complete_by_time\":1320087600,\"id\":22550,\"state_changed_at\":1320029957,\"cost_in_cents\":0,\"links\":{\"html\":\"http://local.taskrabbit.com/tasks/my-first-task--4\",\"get\":\"/api/v1/tasks/22550\",\"delete\":\"/api/v1/tasks/22550\",\"put\":\"/api/v1/tasks/22550\",\"authenticated\":\"http://local.taskrabbit.com/go/961bedc745ac28afd9ff9117d4daf3af67662f2249a7dd6d541ed975c4c4be8f21434d71828a3c1bdeb76edbc172d88a58a7\",\"redirect\":\"https://local.taskrabbit.com/tasks/my-first-task--4?card=true\"},\"state_label\":\"not posted\",\"assign_by_time\":1320073200,\"state\":\"unpaid\"}"
+ http_version: "1.1"
View
30 spec/support/cassettes/tasks/create/without_user.yml
@@ -0,0 +1,30 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :post
+ uri: http://localhost:3000/api/v1/tasks?
+ body:
+ headers:
+ oauth:
+ - ""
+ x-client-application:
+ - euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 401
+ message: Unauthorized
+ headers:
+ content-type:
+ - application/json; charset=utf-8
+ x-runtime:
+ - "88"
+ server:
+ - WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
+ date:
+ - Sun, 30 Oct 2011 22:57:16 GMT
+ content-length:
+ - "63"
+ cache-control:
+ - no-cache
+ body: "{\"error\":\"There must be an authenticated user for this action\"}"
+ http_version: "1.1"
View
6 spec/support/cassettes/find_task.yml → spec/support/cassettes/tasks/find.yml
@@ -5,6 +5,8 @@
uri: http://localhost:3000/api/v1/tasks/22545?
body:
headers:
+ oauth:
+ - ""
x-client-application:
- euqmQpzV04GmN1dJTY639PdI7eiSjCjI3lKTkPWn
response: !ruby/struct:VCR::Response
@@ -17,11 +19,11 @@
content-type:
- application/json; charset=utf-8
x-runtime:
- - "334"
+ - "609"
server:
- WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
date:
- - Sun, 30 Oct 2011 21:06:49 GMT
+ - Sun, 30 Oct 2011 22:57:15 GMT
content-length:
- "1066"
cache-control:
View
6 spec/support/cassettes/tasks_without_client.yml → spec/support/cassettes/tasks/without_client.yml
@@ -5,6 +5,8 @@
uri: http://localhost:3000/api/v1/tasks?
body:
headers:
+ oauth:
+ - ""
x-client-application:
- ""
response: !ruby/struct:VCR::Response
@@ -15,11 +17,11 @@
content-type:
- application/json; charset=utf-8
x-runtime:
- - "28"
+ - "80"
server:
- WEBrick/1.3.1 (Ruby/1.8.7/2010-04-19)
date:
- - Sun, 30 Oct 2011 21:06:46 GMT
+ - Sun, 30 Oct 2011 22:57:08 GMT
content-length:
- "44"
cache-control:
View
46 spec/taskrabbit/api_spec.rb
@@ -14,50 +14,4 @@
}.to_not raise_error
end
end
-
- describe "api endpoints" do
- #tr.tasks wont do the request
- #tr.tasks.first will do the request
- #tr.tasks.all
- #tr.tasks.anything will do the request exect if using find !
- describe "#tasks" do
- before do
- @secret = Taskrabbit.client_secret
- end
-
- after do
- Taskrabbit.client_secret = @secret
- end
-
- it "should return an error if the client is not set" do
- Taskrabbit.client_secret = nil
- tr = Taskrabbit::Api.new
- VCR.use_cassette('tasks_without_client', :record => :new_episodes) do
- expect { tr.tasks.all }.to raise_error(Taskrabbit::Error, 'Missing valid client application')
- end
- end
-
- it "should fetch tasks" do
- tr = Taskrabbit::Api.new
- VCR.use_cassette('tasks', :record => :new_episodes) do
- tr_tasks = nil
- expect { tr_tasks = tr.tasks.all }.to_not raise_error
- tr_tasks.first.should be_instance_of(Taskrabbit::Task)
- end
- end
- end
-
- describe "#find" do
-
- it "should fetch tasks" do
- tr = Taskrabbit::Api.new
- VCR.use_cassette('find_task', :record => :new_episodes) do
- tr_task = nil
- expect { tr_task = tr.tasks.find(22545) }.to_not raise_error
- tr_task.should be_instance_of(Taskrabbit::Task)
- end
- end
-
- end
- end
end
View
100 spec/taskrabbit/task_spec.rb
@@ -1,4 +1,104 @@
require 'spec_helper'
describe Taskrabbit::Task do
+ describe "api endpoints" do
+ #tr.tasks wont do the request
+ #tr.tasks.first will do the request
+ #tr.tasks.all
+ #tr.tasks.anything will do the request exect if using find !
+ describe "#tasks" do
+ before do
+ @secret = Taskrabbit.client_secret
+ end
+
+ after do
+ Taskrabbit.client_secret = @secret
+ end
+
+ it "should return an error if the client is not set" do
+ Taskrabbit.client_secret = nil
+ tr = Taskrabbit::Api.new
+ VCR.use_cassette('tasks/without_client', :record => :new_episodes) do
+ expect { tr.tasks.all }.to raise_error(Taskrabbit::Error, 'Missing valid client application')
+ end
+ end
+
+ it "should fetch tasks" do
+ tr = Taskrabbit::Api.new
+ VCR.use_cassette('tasks', :record => :new_episodes) do
+ tr_tasks = nil
+ expect { tr_tasks = tr.tasks.all }.to_not raise_error
+ tr_tasks.first.should be_instance_of(Taskrabbit::Task)
+ end
+ end
+ end
+
+ describe "#find" do
+
+ it "should fetch tasks" do
+ tr = Taskrabbit::Api.new
+ VCR.use_cassette('tasks/find', :record => :new_episodes) do
+ tr_task = nil
+ expect { tr_task = tr.tasks.find(22545) }.to_not raise_error
+ tr_task.should be_instance_of(Taskrabbit::Task)
+ end
+ end
+
+ end
+
+ describe "#create" do
+ context "with valid params" do
+ let(:valid_params) {
+ {
+ "name" => "My First Task",
+ "named_price" => 20,
+ "city_id" => 4
+ }
+ }
+
+ it "should return an error if the user is not logged in" do
+ tr = Taskrabbit::Api.new
+ VCR.use_cassette('tasks/create/without_user', :record => :new_episodes) do
+ tr_task = nil
+ expect { tr_task = tr.tasks.create(valid_params) }.to raise_error(Taskrabbit::Error, 'There must be an authenticated user for this action')
+ tr_task.should be_nil
+ end
+ end
+
+ it "should create the task if the user is authenticated but does not have a credit card" do
+ tr = Taskrabbit::Api.new('sjCuNHsxMRkFiJGpLWZzYJksDjfnXtDvDcPuuDkn')
+ VCR.use_cassette('tasks/create/without_credit_card', :record => :new_episodes) do
+ tr_task = nil
+ expect { tr_task = tr.tasks.create(valid_params) }.to_not raise_error
+ tr_task.should be_instance_of(Taskrabbit::Task)
+ end
+ end
+
+ it "should create the task if the user is authenticated and has a credit card" do
+ tr = Taskrabbit::Api.new('cNPyB6KypcdDjFb8KhQ7EOZzBpwcenMMvnQBbSHM')
+ VCR.use_cassette('tasks/create', :record => :new_episodes) do
+ tr_task = nil
+ expect { tr_task = tr.tasks.create(valid_params) }.to_not raise_error
+ tr_task.should be_instance_of(Taskrabbit::Task)
+ # tr_task.unpaid?.should == true
+ end
+ end
+ end
+
+ context "with invalid params" do
+ let(:invalid_params) { {} }
+
+ it "should create the task if the user is authenticated and has a credit card" do
+ tr = Taskrabbit::Api.new('cNPyB6KypcdDjFb8KhQ7EOZzBpwcenMMvnQBbSHM')
+ VCR.use_cassette('tasks/create/with_invalid_params', :record => :new_episodes) do
+ tr_task = nil
+ expect { tr_task = tr.tasks.create(invalid_params) }.to
+ raise_error(Taskrabbit::Error, "Task title can't be blank,\n" +
+ "Amount you are willing to pay needs to be a whole dollar amount greater than zero")
+ tr_task.should be_nil
+ end
+ end
+ end
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.