From a96d95654446c0262b8d7d73f72f2a5ab4909d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Gald=C3=A1mez?= Date: Fri, 21 Feb 2025 08:26:14 -0300 Subject: [PATCH 1/2] List deliveries --- lib/ruber.rb | 1 + lib/ruber/collection.rb | 21 ++ lib/ruber/resources/delivery_resource.rb | 6 + test/fixtures/deliveries.json | 301 ++++++++++++++++++ .../ruber/resources/delivery_resource_test.rb | 20 ++ 5 files changed, 349 insertions(+) create mode 100644 lib/ruber/collection.rb create mode 100644 test/fixtures/deliveries.json diff --git a/lib/ruber.rb b/lib/ruber.rb index 047b6dd..9b26a01 100644 --- a/lib/ruber.rb +++ b/lib/ruber.rb @@ -16,6 +16,7 @@ module Ruber autoload :DeliveryResource, "ruber/resources/delivery_resource" + autoload :Collection, "ruber/collection" autoload :Delivery, "ruber/objects/delivery" DEFAULT_API_BASE = "https://api.uber.com/v1" diff --git a/lib/ruber/collection.rb b/lib/ruber/collection.rb new file mode 100644 index 0000000..3eb0492 --- /dev/null +++ b/lib/ruber/collection.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Ruber + class Collection + attr_reader :data, :next_href + + def self.from_response(response, type:) + body = response.body + + new( + data: body[:data].map { |attrs| type.new(attrs) }, + next_href: body[:next_href] + ) + end + + def initialize(data:, next_href:) + @data = data + @next_href = next_href + end + end +end diff --git a/lib/ruber/resources/delivery_resource.rb b/lib/ruber/resources/delivery_resource.rb index 7a5c563..f4ee16e 100644 --- a/lib/ruber/resources/delivery_resource.rb +++ b/lib/ruber/resources/delivery_resource.rb @@ -3,6 +3,12 @@ module Ruber class DeliveryResource class << self + def all + response = Request.new("customers/#{Ruber.customer_id}/deliveries").get + + Collection.from_response(response, type: Delivery) + end + def find(id) response = Request.new("customers/#{Ruber.customer_id}/deliveries/#{id}").get diff --git a/test/fixtures/deliveries.json b/test/fixtures/deliveries.json new file mode 100644 index 0000000..b1e1327 --- /dev/null +++ b/test/fixtures/deliveries.json @@ -0,0 +1,301 @@ +{ + "data": [ + { + "id": "del_gDcyfRgyQcyN0obpqqS_1B", + "quote_id": "quote_id", + "complete": false, + "courier": { + "name": "Alex I.", + "rating": 0, + "vehicle_type": "car", + "phone_number": "+16283337630", + "location": { + "lat": 37.765987, + "lng": -122.43153 + }, + "img_href": "https://d1w2poirtb3as9.cloudfront.net/default.jpeg?Expires=1683022587&Signature=SDlYXhlrNUXVibLEi1veShGrVQqn~5cHlDmKHmirytOvtRPLUzMICPLoLXL8vn7XREKo6XmRjYbE~tMeCqBf3Pjyc4OKqsAyukoUr5ew7S9ipbJ-O0PCp5ThZ4j46yZquza1s~qOQISHopH734F17BaNxod9E9QPJJ3phFcrl4COKYSt3qPsn9Y9PQxyKecJ5Sr0RveI5hGqtQR~u2d3EZgXmJxuGUdk26WX-Ho1h3ziIpnIBu6P6upbOtlnxLqn-dG7tl15pnn~bW1w0qAemFUUdqWzzWfjVGLHonDqE4LM5xE~t1T~1zJCNQ-uzijjI6tnEfIHJ9LgWRRhX3sKXQ__&Key-Pair-Id=K36LFL06Z5BT10", + "public_phone_info": { + "formatted_phone_number": "+15555555557,48023618", + "phone_number": "+15555555557", + "pin_code": "48023618" + } + }, + "courier_imminent": true, + "created": "2023-05-01T22:15:13.646Z", + "currency": "usd", + "deliverable_action": "deliverable_action_meet_at_door", + "dropoff": { + "name": "Joan P.", + "phone_number": "+16283337630", + "address": "2200 Market St, San Francisco, CA 94114, US", + "detailed_address": { + "street_address_1": "2200 Market St", + "street_address_2": "", + "city": "San Francisco", + "state": "CA", + "zip_code": "94114", + "country": "US" + }, + "notes": "", + "seller_notes": "", + "courier_notes": "", + "location": { + "lat": 37.765987, + "lng": -122.43153 + }, + "verification": { + "signature": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb", + "name": "Joan", + "signer_relationship": "SELF" + }, + "barcodes": [ + { + "type": "CODE128", + "value": "1234567890", + "scan_result": { + "outcome": "SUCCESS", + "timestamp": "2023-05-02T04:34:04.957Z" + } + } + ], + "picture": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb" + }, + "identification": { + "min_age_verified": false + }, + "pin_code": { + "entered": "3902" + }, + "completion_location": { + "lat": 37.765987, + "lng": -122.43153 + } + }, + "verification_requirements": { + "signature": true, + "signature_requirement": { + "enabled": true, + "collect_signer_name": true, + "collect_signer_relationship": true + }, + "barcodes": [ + { + "value": "string", + "type": "CODE39" + } + ], + "pincode": { + "enabled": true, + "value": "1234" + }, + "identification": { + "min_age": 21, + "no_sobriety_check": true + }, + "picture": true + }, + "status": "completed", + "status_timestamp": "2023-05-02T02:30:07.560Z", + "external_store_id": "my_store_123" + }, + "dropoff_deadline": "2023-05-01T23:01:37.000Z", + "dropoff_eta": "2023-05-01T22:30:00.077Z", + "dropoff_identifier": "", + "dropoff_ready": "2023-05-01T22:15:13.000Z", + "external_id": "EI000001", + "fee": 1099, + "kind": "delivery", + "live_mode": true, + "manifest": { + "reference": "EI000001", + "description": "1 x Package 1\n", + "total_value": 1099 + }, + "manifest_items": { + "name": "Bow tie", + "quantity": 1, + "size": "small", + "dimensions": { + "length": 20, + "height": 20, + "depth": 20 + }, + "price": 100, + "weight": 300, + "vat_percentage": 1250000 + }, + "pickup": { + "name": "Joan P.", + "phone_number": "+16283337630", + "address": "2200 Market St, San Francisco, CA 94114, US", + "detailed_address": { + "street_address_1": "2200 Market St", + "street_address_2": "", + "city": "San Francisco", + "state": "CA", + "zip_code": "94114", + "country": "US" + }, + "notes": "", + "seller_notes": "", + "courier_notes": "", + "location": { + "lat": 37.765987, + "lng": -122.43153 + }, + "verification": { + "signature": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb", + "name": "Joan", + "signer_relationship": "SELF" + }, + "barcodes": [ + { + "type": "CODE128", + "value": "1234567890", + "scan_result": { + "outcome": "SUCCESS", + "timestamp": "2023-05-02T04:34:04.957Z" + } + } + ], + "picture": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb" + }, + "identification": { + "min_age_verified": false + }, + "pin_code": { + "entered": "3902" + }, + "completion_location": { + "lat": 37.765987, + "lng": -122.43153 + } + }, + "verification_requirements": { + "signature": true, + "signature_requirement": { + "enabled": true, + "collect_signer_name": true, + "collect_signer_relationship": true + }, + "barcodes": [ + { + "value": "string", + "type": "CODE39" + } + ], + "pincode": { + "enabled": true, + "value": "1234" + }, + "identification": { + "min_age": 21, + "no_sobriety_check": true + }, + "picture": true + }, + "status": "completed", + "status_timestamp": "2023-05-02T02:30:07.560Z", + "external_store_id": "my_store_123" + }, + "pickup_deadline": "2023-05-01T22:35:13.000Z", + "pickup_eta": "2023-05-01T22:16:21.749Z", + "pickup_ready": "2023-05-01T22:20:13.000Z", + "related_deliveries": [ + { + "id": "ret_BiymjKtjTwKpoS2kSIuxlv", + "relationship": "return" + } + ], + "status": "pending", + "tip": 500, + "tracking_url": "https://delivery.uber.com/orders/62105936-e413-40d5-aecd-4c335c3cd3c2", + "undeliverable_action": "", + "undeliverable_reason": "", + "updated": "2023-05-01T22:16:20.023Z", + "uuid": "62105936E41340D5AECD4C335C3CD3C2", + "return": { + "name": "Joan P.", + "phone_number": "+16283337630", + "address": "2200 Market St, San Francisco, CA 94114, US", + "detailed_address": { + "street_address_1": "2200 Market St", + "street_address_2": "", + "city": "San Francisco", + "state": "CA", + "zip_code": "94114", + "country": "US" + }, + "notes": "", + "seller_notes": "", + "courier_notes": "", + "location": { + "lat": 37.765987, + "lng": -122.43153 + }, + "verification": { + "signature": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb", + "name": "Joan", + "signer_relationship": "SELF" + }, + "barcodes": [ + { + "type": "CODE128", + "value": "1234567890", + "scan_result": { + "outcome": "SUCCESS", + "timestamp": "2023-05-02T04:34:04.957Z" + } + } + ], + "picture": { + "image_url": "https://tb-static.uber.com/prod/file-upload/uploads/courier-task-platform/82a0d88e-100d-40d6-a0d0-ea9c4a2e55bb" + }, + "identification": { + "min_age_verified": false + }, + "pin_code": { + "entered": "3902" + }, + "completion_location": { + "lat": 37.765987, + "lng": -122.43153 + } + }, + "verification_requirements": { + "signature": true, + "signature_requirement": { + "enabled": true, + "collect_signer_name": true, + "collect_signer_relationship": true + }, + "barcodes": [ + { + "value": "string", + "type": "CODE39" + } + ], + "pincode": { + "enabled": true, + "value": "1234" + }, + "identification": { + "min_age": 21, + "no_sobriety_check": true + }, + "picture": true + }, + "status": "completed", + "status_timestamp": "2023-05-02T02:30:07.560Z", + "external_store_id": "my_store_123" + } + } + ], + "next_href": "api.uber.com/v1/customers/d553004e-a505-4c48-8887-194e57769172/deliveries?offset=30", + "total_count": -1 +} diff --git a/test/ruber/resources/delivery_resource_test.rb b/test/ruber/resources/delivery_resource_test.rb index 014db48..c371b38 100644 --- a/test/ruber/resources/delivery_resource_test.rb +++ b/test/ruber/resources/delivery_resource_test.rb @@ -3,6 +3,15 @@ require File.expand_path("../../test_helper", __dir__) class DelvieryResourceTest < Minitest::Test + def test_all + stub_deliveries_request + deliveries = Ruber::DeliveryResource.all + + assert_equal Ruber::Collection, deliveries.class + assert_equal Ruber::Delivery, deliveries.data.first.class + assert_equal "quote_id", deliveries.data.first.quote_id + end + def test_find stub_delivery_request delivery = Ruber::DeliveryResource.find("del_some_id") @@ -23,4 +32,15 @@ def stub_delivery_request body: File.new("test/fixtures/delivery.json").read ) end + + def stub_deliveries_request + stub_token_request + stub_request(:get, %r{.*customers/#{Ruber.customer_id}/deliveries}) + .with(headers: { "Authorization" => "Bearer #{Ruber::Authenticator.access_token}" }) + .to_return( + headers: { "Content-Type" => "application/json" }, + status: 200, + body: File.new("test/fixtures/deliveries.json").read + ) + end end From a2747b3e83c96654efdbab2089bcc15d0b0eefa7 Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 21 Feb 2025 09:34:28 -0300 Subject: [PATCH 2/2] Better assertion Co-authored-by: bruno costanzo <82187643+bruno-costanzo@users.noreply.github.com> --- test/ruber/resources/delivery_resource_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ruber/resources/delivery_resource_test.rb b/test/ruber/resources/delivery_resource_test.rb index c371b38..c99bfc4 100644 --- a/test/ruber/resources/delivery_resource_test.rb +++ b/test/ruber/resources/delivery_resource_test.rb @@ -7,8 +7,8 @@ def test_all stub_deliveries_request deliveries = Ruber::DeliveryResource.all - assert_equal Ruber::Collection, deliveries.class - assert_equal Ruber::Delivery, deliveries.data.first.class + assert_instance_of Ruber::Collection, deliveries + assert_instance_of Ruber::Delivery, deliveries.data.first assert_equal "quote_id", deliveries.data.first.quote_id end