From 82ce9f488bd26ffae06b7ccc737875e21d8d5720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Freddy=20Rami=CC=81rez?= Date: Thu, 25 Jan 2024 09:13:45 -0600 Subject: [PATCH 1/3] TED-1074/Add create_item --- lib/zesty/client.rb | 14 ++++++++++++ spec/zesty/client_spec.rb | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/zesty/client.rb b/lib/zesty/client.rb index b657ff6..5a83739 100644 --- a/lib/zesty/client.rb +++ b/lib/zesty/client.rb @@ -7,6 +7,7 @@ class Client get_model: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}", get_items: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items", get_item: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items/%{item_zuid}", + create_item: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items", update_item: "https://%{instance_zuid}.api.zesty.io/v1/content/models/%{model_zuid}/items/%{item_zuid}", get_head_tags: "https://%{instance_zuid}.api.zesty.io/v1/web/headtags", create_head_tag: "https://%{instance_zuid}.api.zesty.io/v1/web/headtags", @@ -43,6 +44,19 @@ def get_item(model_zuid, item_zuid) Request.get(url_for(:get_item, model_zuid: model_zuid, item_zuid: item_zuid), headers: { authorization: "Bearer #{@token}" }) end + def create_item(model_zuid, parent_zuid="0", data:, web: {}) + Request.post( + url_for(:create_item, model_zuid: model_zuid, parent_zuid: parent_zuid), + params: { + web: web.merge(parentZUID: parent_zuid).to_camel_case, + data: data + }, + headers: { + authorization: "Bearer #{@token}" + } + ) + end + def update_item(model_zuid, item_zuid, publish: false, data:, meta:, web: {}) # TODO: include `publish: publish` in `url_for` line once API is announced # TODO: add `?publish=%{publish}` to API_URLS `:update_item` diff --git a/spec/zesty/client_spec.rb b/spec/zesty/client_spec.rb index 46b2b05..340be2c 100644 --- a/spec/zesty/client_spec.rb +++ b/spec/zesty/client_spec.rb @@ -3,6 +3,7 @@ let(:instance_zuid) { ENV["INSTANCE_ZUID"] } let(:model_zuid) { ENV["MODEL_ZUID"] } let(:item_zuid) { "7-b29aa3e7ee-0swr1f" } + let(:parent_zuid) { "6-948fb2a6a4-hzsqk1" } let(:token) { Zesty::Auth.get_token(ENV["EMAIL"], ENV["PASSWORD"]) } @@ -174,6 +175,53 @@ end end + describe '#create_item' do + it "creates an item successfully" do + model = VCR.use_cassette("zesty/instances/create_item") do + result = client.create_item( + model_zuid, + parent_zuid, + web: { + metaTitle: "Example Only", + metaLinkText: "Example Link Text", + pathPart: "example-only" + }, + data: { + event_details_title: "postman test only", + charity_name: "postman test", + sweepstakes_datetime: "September 28 5PM", + event_entry_start_date: "August 15, 2023", + event_entry_start_time: "3:00:00 PM", + event_stream_start_date: "December 21, 2023", + event_stream_start_time: "2:59:59 PM", + event_drawing_date: "December 23, 2034", + event_drawing_time: "5:00:00 PM", + event_reveal_date: "December 24, 2034", + event_reveal_time: "6:00:00 PM", + donation_increment_title: "two", + donation_increment_amount: 2, + drawing_1_winning_pattern_name: "Line", + drawing_1_winning_pattern_description: "five (5) Clicked Numbers in any straight line on a Card", + drawing_1_prize_amount: 50, + drawing_1_prize_text: "fifty", + drawing_2_winning_pattern_name: "Stamp", + drawing_2_winning_pattern_description: "sixteen (16) Clicked Numbers around the edges of a Card", + drawing_2_prize_amount: 30, + drawing_2_prize_text: "thirty", + drawing_3_winning_pattern_name: "Corners", + drawing_3_winning_pattern_description: "twenty-five (25) Clicked Numbers on a Card", + drawing_3_prize_amount: 100, + drawing_3_prize_text: "one hundred", + drawings_total_prize_text: "five thousand five hundred fifty", + drawings_total_prize_amount: "5,550" + } + ) + end + + pp model + end + end + describe '#update_item' do it "updates an item successfully" do model = VCR.use_cassette("zesty/instances/update_item") do From dea6819d8b131df5c867740912a640081cddeaf0 Mon Sep 17 00:00:00 2001 From: Javier Julio Date: Fri, 26 Jan 2024 13:35:10 -0500 Subject: [PATCH 2/3] The parent zuid is optional This is also part of the web object so no need for a separate argument, nor the name conversion which `to_camel_case` will handle. --- lib/zesty/client.rb | 6 +++--- spec/zesty/client_spec.rb | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/zesty/client.rb b/lib/zesty/client.rb index 5a83739..2d5996a 100644 --- a/lib/zesty/client.rb +++ b/lib/zesty/client.rb @@ -44,11 +44,11 @@ def get_item(model_zuid, item_zuid) Request.get(url_for(:get_item, model_zuid: model_zuid, item_zuid: item_zuid), headers: { authorization: "Bearer #{@token}" }) end - def create_item(model_zuid, parent_zuid="0", data:, web: {}) + def create_item(model_zuid, data:, web: {}) Request.post( - url_for(:create_item, model_zuid: model_zuid, parent_zuid: parent_zuid), + url_for(:create_item, model_zuid: model_zuid), params: { - web: web.merge(parentZUID: parent_zuid).to_camel_case, + web: web.to_camel_case, data: data }, headers: { diff --git a/spec/zesty/client_spec.rb b/spec/zesty/client_spec.rb index 340be2c..1d3c637 100644 --- a/spec/zesty/client_spec.rb +++ b/spec/zesty/client_spec.rb @@ -3,7 +3,6 @@ let(:instance_zuid) { ENV["INSTANCE_ZUID"] } let(:model_zuid) { ENV["MODEL_ZUID"] } let(:item_zuid) { "7-b29aa3e7ee-0swr1f" } - let(:parent_zuid) { "6-948fb2a6a4-hzsqk1" } let(:token) { Zesty::Auth.get_token(ENV["EMAIL"], ENV["PASSWORD"]) } From 63fbe02c13b6779e8e236be28f4c613b9d58d2f3 Mon Sep 17 00:00:00 2001 From: Javier Julio Date: Fri, 26 Jan 2024 13:37:07 -0500 Subject: [PATCH 3/3] Update create_item test to use existing model --- .../cassettes/zesty/instances/create_item.yml | 144 ++++++++++++++++++ spec/zesty/client_spec.rb | 42 ++--- 2 files changed, 153 insertions(+), 33 deletions(-) create mode 100644 spec/cassettes/zesty/instances/create_item.yml diff --git a/spec/cassettes/zesty/instances/create_item.yml b/spec/cassettes/zesty/instances/create_item.yml new file mode 100644 index 0000000..93a3f00 --- /dev/null +++ b/spec/cassettes/zesty/instances/create_item.yml @@ -0,0 +1,144 @@ +--- +http_interactions: +- request: + method: post + uri: https://auth.api.zesty.io/login + body: + encoding: ASCII-8BIT + string: "-----------------------07b6f0a2033597a319258a45e0a322a991bea61bdf\r\nContent-Disposition: + form-data; name=\"email\"\r\n\r\n\r\n-----------------------07b6f0a2033597a319258a45e0a322a991bea61bdf\r\nContent-Disposition: + form-data; name=\"password\"\r\n\r\n\r\n-----------------------07b6f0a2033597a319258a45e0a322a991bea61bdf--\r\n" + headers: + Accept: + - application/json + Connection: + - close + Content-Type: + - multipart/form-data; boundary=---------------------07b6f0a2033597a319258a45e0a322a991bea61bdf + Host: + - auth.api.zesty.io + User-Agent: + - http.rb/5.1.1 + response: + status: + code: 200 + message: OK + headers: + Date: + - Fri, 26 Jan 2024 18:28:46 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '214' + Vary: + - Accept-Encoding + - Origin + X-Dns-Prefetch-Control: + - 'off' + X-Frame-Options: + - DENY + Strict-Transport-Security: + - max-age=15552000; includeSubDomains + X-Download-Options: + - noopen + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - 1; mode=block + Surrogate-Control: + - no-store + Cache-Control: + - no-store, no-cache, must-revalidate, proxy-revalidate + Pragma: + - no-cache + Expires: + - '0' + Referrer-Policy: + - no-referrer + X-Permitted-Cross-Domain-Policies: + - none + Access-Control-Allow-Credentials: + - 'true' + Set-Cookie: + - APP_SID=; Domain=.zesty.io; Path=/; Secure + Etag: + - W/"d6-WN5CQNLLuZKo33BAXFzKkdIOSC8" + Via: + - 1.1 google + Alt-Svc: + - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 + Connection: + - close + body: + encoding: UTF-8 + string: '{"message":"Logged in successfully.","status":"OK","data":{"data":""},"meta":{"userZuid":"5-d8daa08585-l6620k","token":""},"code":200}' + recorded_at: Fri, 26 Jan 2024 18:28:46 GMT +- request: + method: post + uri: https://.api.zesty.io/v1/content/models/6-d08392dbc9-9gl4bk/items + body: + encoding: ASCII-8BIT + string: '{"web":{"metaTitle":"Example Only","metaLinkText":"Example Link Text"},"data":{"display_order":4,"lottery_name":"Test + Lottery","jackpot_amount":5000000,"next_drawing":"2024-01-26","primary_color":"#ff0000"}}' + headers: + Accept: + - application/json + Authorization: + - Bearer + Connection: + - close + Content-Type: + - application/json; charset=UTF-8 + Host: + - ".api.zesty.io" + User-Agent: + - http.rb/5.1.1 + response: + status: + code: 201 + message: Created + headers: + Date: + - Fri, 26 Jan 2024 18:28:46 GMT + Content-Type: + - text/plain; charset=utf-8 + Content-Length: + - '142' + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - Cookie, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, + accept, origin, Cache-Control, X-Requested-With, X-Auth + Access-Control-Allow-Methods: + - PUT, POST, GET, PATCH, DELETE, OPTIONS + Access-Control-Allow-Origin: + - '' + Content-Security-Policy: + - '' + Feature-Policy: + - '' + Instances-Api-Cache: + - unreachable + Referrer-Policy: + - no-referrer-when-downgrade + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - sameorigin + X-Permitted-Cross-Domain-Policies: + - '' + X-Xss-Protection: + - 1;mode=block + Via: + - 1.1 google + Alt-Svc: + - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 + Connection: + - close + body: + encoding: UTF-8 + string: '{"_meta":{"timestamp":"2024-01-26T18:28:46.52808851Z","totalResults":1,"start":0,"offset":0,"limit":1},"data":{"ZUID":"7-d48aa29bd3-r1nqf7"}} + + ' + recorded_at: Fri, 26 Jan 2024 18:28:46 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/zesty/client_spec.rb b/spec/zesty/client_spec.rb index 1d3c637..892a6e7 100644 --- a/spec/zesty/client_spec.rb +++ b/spec/zesty/client_spec.rb @@ -176,48 +176,24 @@ describe '#create_item' do it "creates an item successfully" do - model = VCR.use_cassette("zesty/instances/create_item") do + item = VCR.use_cassette("zesty/instances/create_item") do result = client.create_item( model_zuid, - parent_zuid, web: { - metaTitle: "Example Only", - metaLinkText: "Example Link Text", - pathPart: "example-only" + meta_title: "Example Only", + meta_link_text: "Example Link Text", }, data: { - event_details_title: "postman test only", - charity_name: "postman test", - sweepstakes_datetime: "September 28 5PM", - event_entry_start_date: "August 15, 2023", - event_entry_start_time: "3:00:00 PM", - event_stream_start_date: "December 21, 2023", - event_stream_start_time: "2:59:59 PM", - event_drawing_date: "December 23, 2034", - event_drawing_time: "5:00:00 PM", - event_reveal_date: "December 24, 2034", - event_reveal_time: "6:00:00 PM", - donation_increment_title: "two", - donation_increment_amount: 2, - drawing_1_winning_pattern_name: "Line", - drawing_1_winning_pattern_description: "five (5) Clicked Numbers in any straight line on a Card", - drawing_1_prize_amount: 50, - drawing_1_prize_text: "fifty", - drawing_2_winning_pattern_name: "Stamp", - drawing_2_winning_pattern_description: "sixteen (16) Clicked Numbers around the edges of a Card", - drawing_2_prize_amount: 30, - drawing_2_prize_text: "thirty", - drawing_3_winning_pattern_name: "Corners", - drawing_3_winning_pattern_description: "twenty-five (25) Clicked Numbers on a Card", - drawing_3_prize_amount: 100, - drawing_3_prize_text: "one hundred", - drawings_total_prize_text: "five thousand five hundred fifty", - drawings_total_prize_amount: "5,550" + display_order: 4, + lottery_name: "Test Lottery", + jackpot_amount: 5_000_000, + next_drawing: "2024-01-26", + primary_color: "#ff0000" } ) end - pp model + pp item end end