Skip to content

Commit 276e187

Browse files
authored
gig bulk archive (#8)
* gig bulk archive * temporary till my PR gets approved * versioning
1 parent 68e95e3 commit 276e187

File tree

11 files changed

+244
-48
lines changed

11 files changed

+244
-48
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ gemspec
55

66
gem 'rake', '~> 12.0'
77
gem 'rspec', '~> 3.0'
8+
# tempprary till my PR gets approved in original gem
9+
gem 'json_api_client', git: 'https://github.com/inderps/json_api_client'
810

911
group :development do
1012
gem 'faker'

Gemfile.lock

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
GIT
2+
remote: https://github.com/inderps/json_api_client
3+
revision: 675e1c8b1bf9978ca4722c2aa19314fb0aa5860b
4+
specs:
5+
json_api_client (1.17.1)
6+
activemodel (>= 3.2.0)
7+
activesupport (>= 3.2.0)
8+
addressable (~> 2.2)
9+
faraday (>= 0.15.2, < 1.2.0)
10+
faraday_middleware (>= 0.9.0, < 1.2.0)
11+
rack (>= 0.2)
12+
113
PATH
214
remote: .
315
specs:
416
cirro-ruby-client (1.1.0)
517
faraday_middleware
6-
json_api_client
718
jwt
819

920
GEM
1021
remote: https://rubygems.org/
1122
specs:
12-
activemodel (6.0.3.3)
13-
activesupport (= 6.0.3.3)
14-
activesupport (6.0.3.3)
23+
activemodel (6.0.3.4)
24+
activesupport (= 6.0.3.4)
25+
activesupport (6.0.3.4)
1526
concurrent-ruby (~> 1.0, >= 1.0.2)
1627
i18n (>= 0.7, < 2)
1728
minitest (~> 5.1)
@@ -33,13 +44,6 @@ GEM
3344
hashdiff (1.0.1)
3445
i18n (1.8.5)
3546
concurrent-ruby (~> 1.0)
36-
json_api_client (1.17.1)
37-
activemodel (>= 3.2.0)
38-
activesupport (>= 3.2.0)
39-
addressable (~> 2.2)
40-
faraday (>= 0.15.2, < 1.2.0)
41-
faraday_middleware (>= 0.9.0, < 1.2.0)
42-
rack (>= 0.2)
4347
jwt (2.2.2)
4448
method_source (1.0.0)
4549
minitest (5.14.2)
@@ -84,21 +88,22 @@ GEM
8488
rubocop (~> 0.87)
8589
ruby-progressbar (1.10.1)
8690
thread_safe (0.3.6)
87-
tzinfo (1.2.7)
91+
tzinfo (1.2.8)
8892
thread_safe (~> 0.1)
8993
unicode-display_width (1.7.0)
9094
webmock (3.9.1)
9195
addressable (>= 2.3.6)
9296
crack (>= 0.3.2)
9397
hashdiff (>= 0.4.0, < 2.0.0)
94-
zeitwerk (2.4.0)
98+
zeitwerk (2.4.2)
9599

96100
PLATFORMS
97101
ruby
98102

99103
DEPENDENCIES
100104
cirro-ruby-client!
101105
faker
106+
json_api_client!
102107
pry
103108
rake (~> 12.0)
104109
rspec (~> 3.0)

cirro-ruby-client.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ Gem::Specification.new do |spec|
2626
spec.require_paths = ['lib']
2727

2828
spec.add_runtime_dependency 'jwt'
29-
spec.add_runtime_dependency 'json_api_client'
3029
spec.add_runtime_dependency 'faraday_middleware'
3130
end

lib/cirro_io/client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
require 'cirro_io/client/app_worker'
1010
require 'cirro_io/client/worker_invitation_filter'
1111
require 'cirro_io/client/gig_task'
12+
require 'cirro_io/client/gig_result'
13+
require 'cirro_io/client/gig_time_activity'
1214
require 'cirro_io/client/gig'
1315

1416
module CirroIO

lib/cirro_io/client/gig.rb

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,43 @@ module Client
33
class Gig < Base
44
has_one :worker_invitation_filter
55
has_many :gig_tasks
6+
has_many :gig_results
7+
has_many :gig_time_activities
68

79
# rubocop:disable Metrics/AbcSize
8-
def bulk_create_with(attrs)
9-
payload = { data: { attributes: attributes } }
10+
def bulk_create_with(worker_invitation_filter, gig_tasks)
11+
payload = { data: { attributes: attributes, relationships: {} } }
12+
payload[:data][:relationships][:gig_tasks] = gig_tasks.map(&:attributes)
13+
payload[:data][:relationships][:worker_invitation_filter] = worker_invitation_filter.attributes
1014

11-
if attrs[:gig_tasks]
12-
payload[:data][:relationships] ||= {}
13-
payload[:data][:relationships][:gig_tasks] = attrs[:gig_tasks].map(&:attributes)
14-
end
15+
response = self.class.custom_post('bulk/gigs', format_to_dashed_keys(payload))
16+
17+
self.class.parser.parse(self.class, response).first
18+
end
1519

16-
if attrs[:worker_invitation_filter]
17-
payload[:data][:relationships] ||= {}
18-
payload[:data][:relationships][:worker_invitation_filter] = attrs[:worker_invitation_filter].attributes
20+
def bulk_archive_with(gig_results, gig_time_activities)
21+
payload = { data: { relationships: {} } }
22+
payload[:data][:relationships][:gig_results] = gig_results.map do |result|
23+
result.attributes.merge({ relationships: {
24+
'app-worker': { data: result.app_worker.attributes },
25+
'gig-task': { data: result.gig_task.attributes },
26+
} })
27+
end
28+
payload[:data][:relationships][:gig_time_activities] = gig_time_activities.map do |activity|
29+
activity.attributes.merge({ relationships: { 'app-worker': { data: activity.app_worker.attributes } } })
1930
end
2031

21-
response = self.class.custom_post('bulk/gigs', payload)
32+
response = self.class.custom_post("bulk/gigs/#{id}/archive", format_to_dashed_keys(payload))
2233

2334
self.class.parser.parse(self.class, response).first
2435
end
2536
# rubocop:enable Metrics/AbcSize
37+
38+
private
39+
40+
def format_to_dashed_keys(params)
41+
params.deep_transform_keys { |key| key.to_s.gsub('_', '-') }
42+
end
2643
end
2744
end
2845
end

lib/cirro_io/client/gig_result.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module CirroIO
2+
module Client
3+
class GigResult < Base
4+
has_one :app_worker
5+
has_one :gig_task
6+
end
7+
end
8+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module CirroIO
2+
module Client
3+
class GigTimeActivity < Base
4+
has_one :app_worker
5+
end
6+
end
7+
end

lib/cirro_io/client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# rubocop:disable Style/MutableConstant
22
module CirroIO
33
module Client
4-
VERSION = '1.1.0'
4+
VERSION = '1.2.0'
55
end
66
end
77
# rubocop:enable Style/MutableConstant

spec/cirro_io/client/gig_spec.rb

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,67 @@
2121
stub_request(:post, "#{test_site}/v1/bulk/gigs")
2222
.to_return(body: File.read('./spec/fixtures/gig_with_filter_and_gig_tasks.json'), headers: { 'Content-Type' => 'application/json' })
2323

24-
created_gig = gig.bulk_create_with(gig_tasks: [gig_task1, gig_task2], worker_invitation_filter: worker_invitation_filter)
24+
created_gig = gig.bulk_create_with(worker_invitation_filter, [gig_task1, gig_task2])
2525

2626
expect(created_gig).to be_valid
27-
expect(created_gig.id).to eq('12')
28-
expect(created_gig.worker_invitation_filter.id).to eq('13')
29-
expect(created_gig.gig_tasks.map(&:id)).to eq(%w[18 19])
27+
expect(created_gig.id).to eq('15')
28+
expect(created_gig.worker_invitation_filter.id).to eq('20')
29+
expect(created_gig.gig_tasks.map(&:id)).to eq(%w[24 25])
3030
end
3131
end
32+
33+
# rubocop:disable RSpec/MultipleMemoizedHelpers
34+
describe 'bulk_archive_with' do
35+
before do
36+
configure_api_client
37+
end
38+
39+
let(:gig_task) { CirroIO::Client::GigTask.new(id: 14) }
40+
41+
let(:gig) do
42+
described_class.new(id: 10, gig_tasks: [gig_task])
43+
end
44+
45+
let(:app_worker1) { CirroIO::Client::AppWorker.new(id: 8) }
46+
let(:app_worker2) { CirroIO::Client::AppWorker.new(id: 9) }
47+
48+
let(:gig_results) do
49+
[CirroIO::Client::GigResult.new(app_worker: app_worker1,
50+
gig_task: gig_task,
51+
title: Faker::Hipster.sentence,
52+
description: Faker::Hipster.paragraph,
53+
quantity: 2),
54+
CirroIO::Client::GigResult.new(app_worker: app_worker2,
55+
gig_task: gig_task,
56+
title: Faker::Hipster.sentence,
57+
description: Faker::Hipster.paragraph,
58+
quantity: 4)]
59+
end
60+
61+
let(:gig_time_activities) do
62+
[CirroIO::Client::GigTimeActivity.new(app_worker: app_worker1,
63+
description: Faker::Hipster.paragraph,
64+
date: Time.current,
65+
duration_in_ms: 66000),
66+
CirroIO::Client::GigTimeActivity.new(app_worker: app_worker2,
67+
description: Faker::Hipster.paragraph,
68+
date: Time.current,
69+
duration_in_ms: 56000)]
70+
end
71+
72+
it 'archives a gig with gig results and gig time activites' do
73+
stub_request(:post, "#{test_site}/v1/bulk/gigs/10/archive")
74+
.to_return(body: File.read('./spec/fixtures/archived_gig_with_gig_results_and_gig_time_activities.json'),
75+
headers: { 'Content-Type' => 'application/json' })
76+
77+
archived_gig = gig.bulk_archive_with(gig_results, gig_time_activities)
78+
79+
expect(archived_gig).to be_valid
80+
expect(archived_gig.id).to eq('10')
81+
expect(archived_gig.archive_at).to eq('2020-11-30T10:30:56.000Z')
82+
expect(archived_gig.gig_results.map(&:id)).to eq(%w[1 2])
83+
expect(archived_gig.gig_time_activities.map(&:id)).to eq(%w[1 2])
84+
end
85+
end
86+
# rubocop:enable RSpec/MultipleMemoizedHelpers
3287
end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"data": {
3+
"id": "10",
4+
"type": "gigs",
5+
"links": {
6+
"self": "/api/v1/gigs/10"
7+
},
8+
"attributes": {
9+
"title": "Beard tacos fanny pack echo whatever blog.",
10+
"description": "Tote bag ennui vice cronut craft beer. Neutra brooklyn street distillery scenester cliche meditation. Listicle squid banh mi pinterest wolf 3 wolf moon.",
11+
"url": "http://muller.org/dean.baumbach",
12+
"automatic-invites": true,
13+
"total-seats": 10,
14+
"archive-at": "2020-11-30T10:30:56.000Z"
15+
},
16+
"relationships": {
17+
"gig-tasks": {
18+
"links": {
19+
"self": "/api/v1/gigs/10/relationships/gig-tasks",
20+
"related": "/api/v1/gigs/10/gig-tasks"
21+
}
22+
},
23+
"gig-results": {
24+
"data": [
25+
{
26+
"type": "gig-results",
27+
"id": "1"
28+
},
29+
{
30+
"type": "gig-results",
31+
"id": "2"
32+
}
33+
]
34+
},
35+
"gig-time-activities": {
36+
"data": [
37+
{
38+
"type": "gig-time-activities",
39+
"id": "1"
40+
},
41+
{
42+
"type": "gig-time-activities",
43+
"id": "2"
44+
}
45+
]
46+
}
47+
}
48+
},
49+
"included": [
50+
{
51+
"id": "1",
52+
"type": "gig-results",
53+
"links": {
54+
"self": "/api/v1/gig-results/1"
55+
},
56+
"attributes": {
57+
"title": "Vice street locavore kogi craft beer readymade wolf actually truffaut.",
58+
"description": "Knausgaard yolo xoxo etsy. Selfies pinterest seitan. 90's aesthetic wayfarers roof lomo echo. Kinfolk schlitz mustache franzen cornhole keytar brunch.",
59+
"quantity": 2,
60+
"multiplier": 100
61+
}
62+
},
63+
{
64+
"id": "2",
65+
"type": "gig-results",
66+
"links": {
67+
"self": "/api/v1/gig-results/2"
68+
},
69+
"attributes": {
70+
"title": "Selvage godard pbr\\u0026b wayfarers.",
71+
"description": "Five dollar toast hella pbr\\u0026b carry echo normcore gluten-free. Literally heirloom godard portland. Thundercats cliche typewriter readymade cornhole butcher pitchfork banjo. Green juice hella wes anderson flexitarian cardigan. Meh mixtape yr before they sold out art party plaid bespoke fingerstache.",
72+
"quantity": 4,
73+
"multiplier": 100
74+
}
75+
},
76+
{
77+
"id": "1",
78+
"type": "gig-time-activities",
79+
"links": {
80+
"self": "/api/v1/gig-time-activities/1"
81+
},
82+
"attributes": {
83+
"description": "Viral swag selvage tofu. Taxidermy readymade franzen vinegar schlitz stumptown. Pabst butcher swag 3 wolf moon stumptown phlogiston literally.",
84+
"duration-in-ms": 66000,
85+
"date": "2020-11-27"
86+
}
87+
},
88+
{
89+
"id": "2",
90+
"type": "gig-time-activities",
91+
"links": {
92+
"self": "/api/v1/gig-time-activities/2"
93+
},
94+
"attributes": {
95+
"description": "Tacos meggings sustainable heirloom humblebrag austin cleanse portland. Kickstarter pop-up echo cray shoreditch. Gentrify williamsburg bitters thundercats crucifix. Ethical hammock venmo disrupt.",
96+
"duration-in-ms": 56000,
97+
"date": "2020-11-27"
98+
}
99+
}
100+
]
101+
}

0 commit comments

Comments
 (0)