bookingsync-api 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/Gemfile +2 -1
  4. data/Guardfile +3 -6
  5. data/README.md +11 -3
  6. data/bookingsync-api.gemspec +2 -2
  7. data/lib/bookingsync/api.rb +0 -2
  8. data/lib/bookingsync/api/client.rb +151 -54
  9. data/lib/bookingsync/api/client/amenities.rb +22 -0
  10. data/lib/bookingsync/api/client/billing_addresses.rb +22 -0
  11. data/lib/bookingsync/api/client/bookings.rb +7 -9
  12. data/lib/bookingsync/api/client/bookings_payments.rb +22 -0
  13. data/lib/bookingsync/api/client/clients.rb +43 -0
  14. data/lib/bookingsync/api/client/destinations.rb +22 -0
  15. data/lib/bookingsync/api/client/inquiries.rb +2 -2
  16. data/lib/bookingsync/api/client/payments.rb +53 -0
  17. data/lib/bookingsync/api/client/periods.rb +54 -0
  18. data/lib/bookingsync/api/client/photos.rb +22 -0
  19. data/lib/bookingsync/api/client/rates.rb +23 -0
  20. data/lib/bookingsync/api/client/rates_rules.rb +22 -0
  21. data/lib/bookingsync/api/client/rates_tables.rb +53 -0
  22. data/lib/bookingsync/api/client/rental_agreements.rb +50 -0
  23. data/lib/bookingsync/api/client/rentals.rb +39 -1
  24. data/lib/bookingsync/api/client/reviews.rb +31 -0
  25. data/lib/bookingsync/api/client/seasons.rb +53 -0
  26. data/lib/bookingsync/api/client/special_offers.rb +54 -0
  27. data/lib/bookingsync/api/relation.rb +87 -0
  28. data/lib/bookingsync/api/resource.rb +58 -0
  29. data/lib/bookingsync/api/response.rb +79 -0
  30. data/lib/bookingsync/api/serializer.rb +100 -0
  31. data/lib/bookingsync/api/version.rb +1 -1
  32. data/spec/bookingsync/api/client/amenities_spec.rb +12 -0
  33. data/spec/bookingsync/api/client/billing_addresses_spec.rb +12 -0
  34. data/spec/bookingsync/api/client/billings_payments_spec.rb +12 -0
  35. data/spec/bookingsync/api/client/bookings_spec.rb +10 -8
  36. data/spec/bookingsync/api/client/clients_spec.rb +55 -0
  37. data/spec/bookingsync/api/client/destinations_spec.rb +12 -0
  38. data/spec/bookingsync/api/client/inquiries_spec.rb +1 -1
  39. data/spec/bookingsync/api/client/payments_spec.rb +56 -0
  40. data/spec/bookingsync/api/client/periods_spec.rb +61 -0
  41. data/spec/bookingsync/api/client/photos_spec.rb +12 -0
  42. data/spec/bookingsync/api/client/rates_rules_spec.rb +13 -0
  43. data/spec/bookingsync/api/client/rates_spec.rb +12 -0
  44. data/spec/bookingsync/api/client/rates_tables_spec.rb +59 -0
  45. data/spec/bookingsync/api/client/rental_agreements_spec.rb +64 -0
  46. data/spec/bookingsync/api/client/rentals_spec.rb +56 -6
  47. data/spec/bookingsync/api/client/reviews_spec.rb +33 -0
  48. data/spec/bookingsync/api/client/seasons_spec.rb +61 -0
  49. data/spec/bookingsync/api/client/special_offers_spec.rb +63 -0
  50. data/spec/bookingsync/api/client_spec.rb +31 -10
  51. data/spec/bookingsync/api/relation_spec.rb +72 -0
  52. data/spec/bookingsync/api/resource_spec.rb +79 -0
  53. data/spec/bookingsync/api/response_spec.rb +66 -0
  54. data/spec/cassettes/BookingSync_API_Client_Amenities/_amenities/returns_amenities.yml +72 -0
  55. data/spec/cassettes/BookingSync_API_Client_BillingAddresses/_billing_addresses/returns_billing_addresses.yml +65 -0
  56. data/spec/cassettes/BookingSync_API_Client_Bookings/_bookings/pagination/with_a_block/yields_block_with_batch_of_bookings.yml +12 -12
  57. data/spec/cassettes/BookingSync_API_Client_Bookings/_bookings/pagination/with_auto_paginate_true/returns_all_bookings_joined_from_many_requests.yml +12 -12
  58. data/spec/cassettes/BookingSync_API_Client_Bookings/_bookings/pagination/with_per_page_setting/returns_limited_number_of_bookings.yml +3 -3
  59. data/spec/cassettes/BookingSync_API_Client_Bookings/_clients/returns_clients.yml +66 -0
  60. data/spec/cassettes/BookingSync_API_Client_Bookings/_create_booking/creates_a_booking.yml +1 -1
  61. data/spec/cassettes/BookingSync_API_Client_Bookings/_create_client/creates_a_new_client.yml +64 -0
  62. data/spec/cassettes/BookingSync_API_Client_Bookings/_edit_booking/updates_given_booking_by_ID.yml +18 -14
  63. data/spec/cassettes/BookingSync_API_Client_Bookings/_edit_client/updates_given_client_by_ID.yml +62 -0
  64. data/spec/cassettes/BookingSync_API_Client_BookingsPayments/_bookings_payments/returns_bookings_payments.yml +65 -0
  65. data/spec/cassettes/BookingSync_API_Client_Destinations/_destinations/returns_destinations.yml +64 -0
  66. data/spec/cassettes/BookingSync_API_Client_Inquiries/_create_inquiry/creates_a_new_inquiry.yml +1 -1
  67. data/spec/cassettes/BookingSync_API_Client_Inquiries/_inquiries/returns_inquiries.yml +1 -1
  68. data/spec/cassettes/BookingSync_API_Client_Payments/_cancel_payment/cancels_given_payment.yml +57 -0
  69. data/spec/cassettes/BookingSync_API_Client_Payments/_create_payment/creates_a_new_payment.yml +63 -0
  70. data/spec/cassettes/BookingSync_API_Client_Payments/_edit_payment/updates_given_payment_by_ID.yml +61 -0
  71. data/spec/cassettes/BookingSync_API_Client_Payments/_payments/returns_payments.yml +64 -0
  72. data/spec/cassettes/BookingSync_API_Client_Periods/_create_period/creates_a_new_period.yml +63 -0
  73. data/spec/cassettes/BookingSync_API_Client_Periods/_delete_period/deletes_given_period.yml +70 -0
  74. data/spec/cassettes/BookingSync_API_Client_Periods/_edit_period/updates_given_period_by_ID.yml +61 -0
  75. data/spec/cassettes/BookingSync_API_Client_Periods/_periods/returns_periods.yml +64 -0
  76. data/spec/cassettes/BookingSync_API_Client_Photos/_photos/returns_photos.yml +72 -0
  77. data/spec/cassettes/BookingSync_API_Client_Rates/_rates/returns_rates.yml +64 -0
  78. data/spec/cassettes/BookingSync_API_Client_RatesRules/_rates_rules/returns_rates_rules.yml +64 -0
  79. data/spec/cassettes/BookingSync_API_Client_RatesTables/_create_rates_table/creates_a_new_rates_table.yml +64 -0
  80. data/spec/cassettes/BookingSync_API_Client_RatesTables/_delete_rates_table/deletes_given_rates_table.yml +57 -0
  81. data/spec/cassettes/BookingSync_API_Client_RatesTables/_edit_rates_table/updates_given_rates_table_by_ID.yml +62 -0
  82. data/spec/cassettes/BookingSync_API_Client_RatesTables/_rates_tables/returns_rates_tables.yml +74 -0
  83. data/spec/cassettes/BookingSync_API_Client_RentalAgreements/_create_rental_agreement/creates_a_new_rental_agreement.yml +63 -0
  84. data/spec/cassettes/BookingSync_API_Client_RentalAgreements/_create_rental_agreement_for_booking/creates_a_new_rental_agreement.yml +63 -0
  85. data/spec/cassettes/BookingSync_API_Client_RentalAgreements/_create_rental_agreement_for_rental/creates_a_new_rental_agreement.yml +63 -0
  86. data/spec/cassettes/BookingSync_API_Client_RentalAgreements/_rental_agreements/returns_rental_agreements.yml +64 -0
  87. data/spec/cassettes/BookingSync_API_Client_Rentals/_cancel_rental/cancels_given_rental.yml +57 -0
  88. data/spec/cassettes/BookingSync_API_Client_Rentals/_create_rental/creates_a_new_rental.yml +64 -0
  89. data/spec/cassettes/BookingSync_API_Client_Rentals/_edit_rental/updates_given_rental_by_ID.yml +64 -0
  90. data/spec/cassettes/BookingSync_API_Client_Rentals/_rental/returns_a_single_rental.yml +67 -0
  91. data/spec/cassettes/BookingSync_API_Client_Rentals/_rentals/links/returns_associated_photos.yml +152 -0
  92. data/spec/cassettes/BookingSync_API_Client_Rentals/_rentals/links/returns_links_to_associated_resources.yml +91 -0
  93. data/spec/cassettes/BookingSync_API_Client_Rentals/_rentals/returns_rentals.yml +45 -20
  94. data/spec/cassettes/BookingSync_API_Client_Reviews/_create_review/creates_a_new_review.yml +64 -0
  95. data/spec/cassettes/BookingSync_API_Client_Reviews/_reviews/returns_reviews.yml +83 -0
  96. data/spec/cassettes/BookingSync_API_Client_Seasons/_create_season/creates_a_new_season.yml +64 -0
  97. data/spec/cassettes/BookingSync_API_Client_Seasons/_delete_season/deletes_given_season.yml +57 -0
  98. data/spec/cassettes/BookingSync_API_Client_Seasons/_edit_season/updates_given_season_by_ID.yml +62 -0
  99. data/spec/cassettes/BookingSync_API_Client_Seasons/_seasons/returns_seasons.yml +74 -0
  100. data/spec/cassettes/BookingSync_API_Client_SpecialOffers/_create_special_offer/creates_a_new_special_offer.yml +63 -0
  101. data/spec/cassettes/BookingSync_API_Client_SpecialOffers/_delete_special_offer/deletes_given_special_offer.yml +57 -0
  102. data/spec/cassettes/BookingSync_API_Client_SpecialOffers/_edit_special_offer/updates_given_special_offer_by_ID.yml +61 -0
  103. data/spec/cassettes/{BookingSync_API_Client_Rentals/_rentals/with_specified_fields_in_options/returns_rentals_with_filtered_fields.yml → BookingSync_API_Client_SpecialOffers/_special_offers/returns_special_offers.yml} +17 -10
  104. data/spec/spec_helper.rb +1 -0
  105. metadata +143 -7
  106. data/lib/bookingsync/ext/resource.rb +0 -9
@@ -5,16 +5,66 @@ describe BookingSync::API::Client::Rentals do
5
5
 
6
6
  describe ".rentals", :vcr do
7
7
  it "returns rentals" do
8
- expect(client.rentals).not_to be_nil
8
+ expect(client.rentals).not_to be_empty
9
9
  assert_requested :get, bs_url("rentals")
10
10
  end
11
11
 
12
- context "with specified fields in options" do
13
- it "returns rentals with filtered fields" do
14
- rentals = client.rentals(fields: :name)
15
- expect(rentals).not_to be_nil
16
- assert_requested :get, bs_url("rentals?fields=name")
12
+ describe "links" do
13
+ it "returns associated photos" do
14
+ rental = client.rentals.first
15
+ expect(rental.photos).not_to be_empty
17
16
  end
18
17
  end
19
18
  end
19
+
20
+ describe ".rental", :vcr do
21
+ it "returns a single rental" do
22
+ rental = client.rental(2)
23
+ expect(rental.name).to eql("0 est")
24
+ end
25
+ end
26
+
27
+ describe ".create_rental", :vcr do
28
+ let(:attributes) { {
29
+ name: "New rental",
30
+ sleeps: 2
31
+ } }
32
+
33
+ it "creates a new rental" do
34
+ client.create_rental(attributes)
35
+ assert_requested :post, bs_url("rentals"),
36
+ body: { rentals: [attributes] }.to_json
37
+ end
38
+
39
+ it "returns newly created rental" do
40
+ VCR.use_cassette('BookingSync_API_Client_Rentals/_create_rental/creates_a_new_rental') do
41
+ rental = client.create_rental(attributes)
42
+ expect(rental.name).to eql("New rental")
43
+ expect(rental.sleeps).to eql(2)
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ".edit_rental", :vcr do
49
+ it "updates given rental by ID" do
50
+ client.edit_rental(2, name: 'Updated Rental name')
51
+ assert_requested :put, bs_url("rentals/2"),
52
+ body: { rentals: [{ name: 'Updated Rental name' }] }.to_json
53
+ end
54
+
55
+ it "returns updated rental" do
56
+ VCR.use_cassette('BookingSync_API_Client_Rentals/_edit_rental/updates_given_rental_by_ID') do
57
+ rental = client.edit_rental(2, name: 'Updated Rental name')
58
+ expect(rental).to be_kind_of(BookingSync::API::Resource)
59
+ expect(rental.name).to eq('Updated Rental name')
60
+ end
61
+ end
62
+ end
63
+
64
+ describe ".cancel_rental", :vcr do
65
+ it "cancels given rental" do
66
+ client.cancel_rental(4)
67
+ assert_requested :delete, bs_url("rentals/4")
68
+ end
69
+ end
20
70
  end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe BookingSync::API::Client::Reviews do
4
+ let(:client) { BookingSync::API::Client.new(test_access_token) }
5
+
6
+ describe ".reviews", :vcr do
7
+ it "returns reviews" do
8
+ expect(client.reviews).not_to be_empty
9
+ assert_requested :get, bs_url("reviews")
10
+ end
11
+ end
12
+
13
+ describe ".create_review", :vcr do
14
+ let(:attributes) {{
15
+ comment: "Awesome place",
16
+ rating: 5
17
+ }}
18
+
19
+ it "creates a new review" do
20
+ client.create_review(1, attributes)
21
+ assert_requested :post, bs_url("reviews"),
22
+ body: { booking_id: 1, reviews: [attributes] }.to_json
23
+ end
24
+
25
+ it "returns newly created review" do
26
+ VCR.use_cassette('BookingSync_API_Client_Reviews/_create_review/creates_a_new_review') do
27
+ review = client.create_review(1, attributes)
28
+ expect(review.comment).to eql(attributes[:comment])
29
+ expect(review.rating).to eql(attributes[:rating])
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe BookingSync::API::Client::Seasons do
4
+
5
+ let(:client) { BookingSync::API::Client.new(test_access_token) }
6
+
7
+ describe ".seasons", :vcr do
8
+ it "returns seasons" do
9
+ expect(client.seasons).not_to be_empty
10
+ assert_requested :get, bs_url("seasons")
11
+ end
12
+ end
13
+
14
+ describe ".create_season", :vcr do
15
+ let(:attributes) {{
16
+ name: "New season",
17
+ ratio: 0.2,
18
+ minimum_stay: 4
19
+ }}
20
+
21
+ it "creates a new season" do
22
+ client.create_season(13, attributes)
23
+ assert_requested :post, bs_url("seasons"),
24
+ body: { rates_table_id: 13, seasons: [attributes] }.to_json
25
+ end
26
+
27
+ it "returns newly created season" do
28
+ VCR.use_cassette('BookingSync_API_Client_Seasons/_create_season/creates_a_new_season') do
29
+ season = client.create_season(13, attributes)
30
+ expect(season.name).to eql(attributes[:name])
31
+ expect(season.minimum_stay).to eql(attributes[:minimum_stay])
32
+ end
33
+ end
34
+ end
35
+
36
+ describe ".edit_season", :vcr do
37
+ let(:attributes) {
38
+ { name: "Updated season" }
39
+ }
40
+ it "updates given season by ID" do
41
+ client.edit_season(6, attributes)
42
+ assert_requested :put, bs_url("seasons/6"),
43
+ body: { seasons: [attributes] }.to_json
44
+ end
45
+
46
+ it "returns updated season" do
47
+ VCR.use_cassette('BookingSync_API_Client_Seasons/_edit_season/updates_given_season_by_ID') do
48
+ season = client.edit_season(6, attributes)
49
+ expect(season).to be_kind_of(BookingSync::API::Resource)
50
+ expect(season.name).to eq(attributes[:name])
51
+ end
52
+ end
53
+ end
54
+
55
+ describe ".delete_season", :vcr do
56
+ it "deletes given season" do
57
+ client.delete_season(10)
58
+ assert_requested :delete, bs_url("seasons/10")
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe BookingSync::API::Client::SpecialOffers do
4
+ let(:client) { BookingSync::API::Client.new(test_access_token) }
5
+
6
+ describe ".special_offers", :vcr do
7
+ it "returns special_offers" do
8
+ expect(client.special_offers).not_to be_empty
9
+ assert_requested :get, bs_url("special_offers")
10
+ end
11
+ end
12
+
13
+ describe ".create_special_offer", :vcr do
14
+ let(:attributes) {
15
+ {
16
+ name: "New special offer",
17
+ start_at: '2014-04-28',
18
+ end_at: '2014-05-28',
19
+ discount: 5
20
+ }
21
+ }
22
+
23
+ it "creates a new special_offer" do
24
+ client.create_special_offer(12, attributes)
25
+ assert_requested :post, bs_url("special_offers"),
26
+ body: { rental_id: 12, special_offers: [attributes] }.to_json
27
+ end
28
+
29
+ it "returns newly created special_offer" do
30
+ VCR.use_cassette('BookingSync_API_Client_SpecialOffers/_create_special_offer/creates_a_new_special_offer') do
31
+ special_offer = client.create_special_offer(12, attributes)
32
+ expect(special_offer.name).to eql(attributes[:name])
33
+ end
34
+ end
35
+ end
36
+
37
+ describe ".edit_special_offer", :vcr do
38
+ let(:attributes) {
39
+ { name: 'Updated special offer' }
40
+ }
41
+
42
+ it "updates given special_offer by ID" do
43
+ client.edit_special_offer(3, attributes)
44
+ assert_requested :put, bs_url("special_offers/3"),
45
+ body: { special_offers: [attributes] }.to_json
46
+ end
47
+
48
+ it "returns updated special_offer" do
49
+ VCR.use_cassette('BookingSync_API_Client_SpecialOffers/_edit_special_offer/updates_given_special_offer_by_ID') do
50
+ special_offer = client.edit_special_offer(3, attributes)
51
+ expect(special_offer).to be_kind_of(BookingSync::API::Resource)
52
+ expect(special_offer.name).to eq(attributes[:name])
53
+ end
54
+ end
55
+ end
56
+
57
+ describe ".delete_special_offer", :vcr do
58
+ it "deletes given special_offer" do
59
+ client.delete_special_offer(4)
60
+ assert_requested :delete, bs_url("special_offers/4")
61
+ end
62
+ end
63
+ end
@@ -62,34 +62,55 @@ describe BookingSync::API::Client do
62
62
  end
63
63
 
64
64
  describe "#request" do
65
+ context "on response which responds to :resources" do
66
+ it "returns an array of resources" do
67
+ stub_get("resource", body: {resources: [{name: "Megan"}]}.to_json)
68
+ resources = client.request(:get, "resource")
69
+ expect(resources).to be_kind_of(Array)
70
+ expect(resources.first.name).to eq("Megan")
71
+ end
72
+ end
73
+
74
+ context "on response which doesn't respond to :resources" do
75
+ it "returns response" do
76
+ stub_get("resource", status: 204)
77
+ response = double(BookingSync::API::Response)
78
+ allow(client).to receive(:call) { response }
79
+ resources = client.request(:get, "resource")
80
+ expect(resources).to eql(response)
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#call" do
65
86
  before { VCR.turn_off! }
66
87
  it "authenticates the request with OAuth token" do
67
88
  ENV["ACCESS_TOKEN"] = nil
68
89
  stub_get("resource")
69
- client.get("resource")
90
+ client.call(:get, "resource")
70
91
  assert_requested :get, bs_url("resource"),
71
92
  headers: {"Authorization" => "Bearer fake-access-token"}
72
93
  end
73
94
 
74
95
  it "requests proper accept header for JSON API" do
75
96
  stub_get("resource")
76
- client.get("resource")
97
+ client.call(:get, "resource")
77
98
  assert_requested :get, bs_url("resource"),
78
99
  headers: {"Accept" => "application/vnd.api+json"}
79
100
  end
80
101
 
81
102
  it "requests sends data with JSON API content type" do
82
103
  stub_post("resource")
83
- client.post("resource")
104
+ client.call(:post, "resource")
84
105
  assert_requested :post, bs_url("resource"),
85
106
  headers: {"Content-Type" => "application/vnd.api+json"}
86
107
  end
87
108
 
88
- it "returns Array of resources" do
89
- stub_get("resource", body: {resources: [{name: "Megan"}]}.to_json)
90
- resources = client.get("resource")
91
- expect(resources.size).to eq(1)
92
- expect(resources.first.name).to eq("Megan")
109
+ it "returns an Response object of resources" do
110
+ attributes = {resources: [{name: "Megan"}]}
111
+ stub_post("resource", body: attributes.to_json)
112
+ response = client.call(:post, "resource", attributes)
113
+ expect(response).to be_kind_of(BookingSync::API::Response)
93
114
  end
94
115
 
95
116
  context "API returns 401" do
@@ -109,9 +130,9 @@ describe BookingSync::API::Client do
109
130
  end
110
131
 
111
132
  context "API returns 204 No Content" do
112
- it "returns an empty array" do
133
+ it "returns nil" do
113
134
  stub_get("resource", status: 204)
114
- expect(client.get("resource")).to eql([])
135
+ expect(client.get("resource")).to be_nil
115
136
  end
116
137
  end
117
138
 
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe BookingSync::API::Relation do
4
+ before { VCR.turn_off! }
5
+ let(:client) { BookingSync::API::Client.new(test_access_token,
6
+ base_url: "http://example.com") }
7
+ let(:links) { {"foo.photos" => "http://example.com/photos/{foo.photos}"} }
8
+ let(:relation) { BookingSync::API::Relation.new(client, :"foo.photos",
9
+ "http://example.com/photos/{foo.photos}") }
10
+
11
+ describe ".from_links" do
12
+ it "returns a hash of relations" do
13
+ relations = BookingSync::API::Relation.from_links(client, links)
14
+ expect(relations.size).to eql(1)
15
+ expect(relations).to be_kind_of(Hash)
16
+ expect(relations["foo.photos"]).to be_kind_of(BookingSync::API::Relation)
17
+ end
18
+ end
19
+
20
+ describe ".from_link" do
21
+ it "returns a single relation" do
22
+ relation = BookingSync::API::Relation.from_link(client, :example,
23
+ href: "https://example.com/photos")
24
+ expect(relation).to be_kind_of(BookingSync::API::Relation)
25
+ end
26
+ end
27
+
28
+ describe "#call" do
29
+ let(:client) { double(BookingSync::API::Client) }
30
+
31
+ it "makes HTTP request using API client" do
32
+ url_template = Addressable::Template.new("http://example.com/photos/{foo.photos}")
33
+ expect(client).to receive(:call).with(:get, url_template, nil, {})
34
+ relation.call
35
+ end
36
+ end
37
+
38
+ describe "#get" do
39
+ it "makes a HTTP GET using call on relation" do
40
+ url_template = Addressable::Template.new("http://example.com/photos/{foo.photos}")
41
+ expect(relation).to receive(:call).with({fields: [:name, :description], method: :get})
42
+ relation.get(fields: [:name, :description])
43
+ end
44
+ end
45
+
46
+
47
+ describe "#name" do
48
+ it "returns relation name" do
49
+ expect(relation.name).to eq(:"foo.photos")
50
+ end
51
+ end
52
+
53
+ describe "#href_template" do
54
+ it "returns relation's URL template" do
55
+ url_template = Addressable::Template.new("http://example.com/photos/{foo.photos}")
56
+ expect(relation.href_template).to eq(url_template)
57
+ end
58
+ end
59
+
60
+ describe "#href" do
61
+ it "returns relation's URL" do
62
+ expect(relation.href).to eq("http://example.com/photos/")
63
+ end
64
+
65
+ context "when options given" do
66
+ it "returns relation's URL expanded" do
67
+ url = relation.href("foo.photos" => [1, 2, 100])
68
+ expect(url).to eq("http://example.com/photos/1,2,100")
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe BookingSync::API::Resource do
4
+ let(:links) { {photos: [9, 10]} }
5
+ let(:resource) {
6
+ client = BookingSync::API::Client.new(test_access_token,
7
+ base_url: "http://foo.com")
8
+ BookingSync::API::Resource.new(client,
9
+ {
10
+ name: "foo", width: 700,
11
+ links: links,
12
+ details: {count: 1},
13
+ id: 10
14
+ },
15
+ {
16
+ :"foo.photos" => "http://foo.com/photos/{foo.photos}",
17
+ :"foo.bars" => "http://foo.com/bars/{foo.bars}"
18
+ },
19
+ "foo"
20
+ )
21
+ }
22
+
23
+ describe "processing values" do
24
+ it "makes data accessible" do
25
+ expect(resource.name).to eql("foo")
26
+ expect(resource.width).to eql(700)
27
+ end
28
+
29
+ it "makes nested data accessible" do
30
+ expect(resource.details.count).to eql(1)
31
+ end
32
+
33
+ describe "#_resources_key" do
34
+ it "returns resources_key" do
35
+ expect(resource._resources_key).to eql("foo")
36
+ end
37
+
38
+ context "for nested resource" do
39
+ it "returns nil" do
40
+ expect(resource.details._resources_key).to be_nil
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "associations" do
47
+ before { VCR.turn_off! }
48
+ it "fetches an association based on links" do
49
+ stub_request(:get, "http://foo.com/photos/9,10")
50
+ .to_return(body: {photos: [{file: 'a.jpg'}]}.to_json)
51
+ expect(resource.photos).to eql([{:file => "a.jpg"}])
52
+ end
53
+
54
+ context "when there are not associated ids" do
55
+ let(:links) { {photos: []} }
56
+ it "returns an empty array" do
57
+ expect(resource.photos).to eql([])
58
+ end
59
+ end
60
+
61
+ context "association doesn't exist" do
62
+ it "raises NoMethodError" do
63
+ expect { resource.pphotos }.to raise_error(NoMethodError)
64
+ end
65
+ end
66
+
67
+ it "passes query option to request" do
68
+ stub_request(:get, "http://foo.com/photos/9,10?fields=description")
69
+ .to_return(body: {photos: [{file: 'a.jpg'}]}.to_json)
70
+ resource.photos(fields: :description)
71
+ end
72
+ end
73
+
74
+ describe "#to_s" do
75
+ it "returns resource id" do
76
+ expect("#{resource}").to eq("10")
77
+ end
78
+ end
79
+ end