Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onfleet tasks/all endpoint support #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/onfleet-ruby/actions/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ def list(filters = {})
response.compact.map { |item| new(item) }
end

def query_params(filters)
filters && filters
.collect { |key, value| "#{key}=#{URI.encode_www_form_component(value)}" }
.join('&')
end

private

def list_url_for(filters)
[api_url, query_params(filters)].compact.join('?')
end

def query_params(filters)
filters && filters
.collect { |key, value| "#{key}=#{URI.encode_www_form_component(value)}" }
.join('&')
end
end

def self.included(base)
Expand Down
14 changes: 14 additions & 0 deletions lib/onfleet-ruby/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ def complete
Onfleet.request(url, :post, params)
true
end

def self.list_all(filters)
response = Onfleet.request(list_all_url_for(filters), :get)
{
lastId: response['lastId'],
tasks: response['tasks'].compact.map { |item| new(item) }
}
end

private

def self.list_all_url_for(filters)
[api_url + '/all', query_params(filters)].compact.join('?')
end
end
end

2 changes: 1 addition & 1 deletion onfleet-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_dependency('activesupport', '>= 4.2')
s.add_dependency('rest-client', '~> 1.4')
s.add_dependency('rest-client', '~> 2.0')

s.add_development_dependency('rake')
s.add_development_dependency('rspec', '~> 3.3')
Expand Down
19 changes: 19 additions & 0 deletions spec/onfleet/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
end
end

describe ".list_all" do
subject { -> { described_class.list_all(query_params) } }

context "with no filter" do
let(:query_params) { nil }
it_should_behave_like "list all tasks", path: 'tasks/all'
end

context "with query params" do
let(:query_params) { { food: 'pizza', topping: 'mushroom' } }
it_should_behave_like "list all tasks", path: 'tasks/all?food=pizza&topping=mushroom'
end

context "with a URL-unsafe query param" do
let(:query_params) { { food: 'green eggs & ham' } }
it_should_behave_like "list all tasks", path: 'tasks/all?food=green+eggs+%26+ham'
end
end

describe ".create" do
subject { -> { described_class.create(params) } }
it_should_behave_like Onfleet::Actions::Create, path: 'tasks'
Expand Down
6 changes: 6 additions & 0 deletions spec/support/http_requests/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
it_should_behave_like "an action that makes a request to Onfleet", method: :get
end

RSpec.shared_examples_for "list all tasks" do |path:|
set_up_request_stub(:get, path)
let(:response_body) { { 'lastId' => 'some-id', 'tasks' => [{ id: 'an-object' }, { id: 'another-object' }] } }
it_should_behave_like "an action that makes a request to Onfleet", method: :get
end

RSpec.shared_examples_for Onfleet::Actions::Create do |path:|
set_up_request_stub(:post, path)
let(:response_body) { { id: 'an-object' } }
Expand Down