7digital 0.1.2 → 0.1.5
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.
- checksums.yaml +7 -0
- data/README.rdoc +143 -143
- data/lib/sevendigital.rb +55 -55
- data/lib/sevendigital/api_operator.rb +127 -127
- data/lib/sevendigital/api_operator_cached.rb +91 -62
- data/lib/sevendigital/api_request.rb +72 -62
- data/lib/sevendigital/client.rb +122 -122
- data/lib/sevendigital/client_configuration.rb +85 -83
- data/lib/sevendigital/client_digestors.rb +122 -122
- data/lib/sevendigital/client_managers.rb +57 -57
- data/lib/sevendigital/digestion_tract/api_response_digestor.rb +48 -48
- data/lib/sevendigital/digestion_tract/artist_digestor.rb +35 -35
- data/lib/sevendigital/digestion_tract/basket_digestor.rb +19 -19
- data/lib/sevendigital/digestion_tract/basket_item_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/chart_item_digestor.rb +34 -34
- data/lib/sevendigital/digestion_tract/country_digestor.rb +15 -15
- data/lib/sevendigital/digestion_tract/digestor.rb +122 -122
- data/lib/sevendigital/digestion_tract/download_url_digestor.rb +23 -23
- data/lib/sevendigital/digestion_tract/format_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/label_digestor.rb +21 -21
- data/lib/sevendigital/digestion_tract/list_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/list_item_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/locker_digestor.rb +22 -22
- data/lib/sevendigital/digestion_tract/locker_release_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/locker_track_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/oauth_access_token_digestor.rb +18 -18
- data/lib/sevendigital/digestion_tract/oauth_request_token_digestor.rb +18 -18
- data/lib/sevendigital/digestion_tract/pager_digestor.rb +28 -28
- data/lib/sevendigital/digestion_tract/price_digestor.rb +28 -28
- data/lib/sevendigital/digestion_tract/release_digestor.rb +45 -45
- data/lib/sevendigital/digestion_tract/tag_digestor.rb +23 -23
- data/lib/sevendigital/digestion_tract/track_digestor.rb +38 -37
- data/lib/sevendigital/digestion_tract/user_card_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/user_digestor.rb +21 -21
- data/lib/sevendigital/management/artist_manager.rb +97 -97
- data/lib/sevendigital/management/basket_manager.rb +29 -29
- data/lib/sevendigital/management/country_manager.rb +10 -10
- data/lib/sevendigital/management/list_manager.rb +18 -18
- data/lib/sevendigital/management/manager.rb +12 -12
- data/lib/sevendigital/management/oauth_manager.rb +23 -23
- data/lib/sevendigital/management/release_manager.rb +54 -54
- data/lib/sevendigital/management/tag_manager.rb +10 -10
- data/lib/sevendigital/management/track_manager.rb +29 -27
- data/lib/sevendigital/management/user_card_manager.rb +26 -26
- data/lib/sevendigital/management/user_manager.rb +76 -76
- data/lib/sevendigital/model/api_response.rb +32 -32
- data/lib/sevendigital/model/artist.rb +104 -104
- data/lib/sevendigital/model/basket.rb +15 -15
- data/lib/sevendigital/model/basket_item.rb +14 -14
- data/lib/sevendigital/model/card.rb +11 -11
- data/lib/sevendigital/model/chart_item.rb +9 -9
- data/lib/sevendigital/model/download_url.rb +7 -7
- data/lib/sevendigital/model/format.rb +7 -7
- data/lib/sevendigital/model/label.rb +7 -7
- data/lib/sevendigital/model/list.rb +4 -4
- data/lib/sevendigital/model/list_item.rb +8 -8
- data/lib/sevendigital/model/locker.rb +4 -4
- data/lib/sevendigital/model/locker_release.rb +9 -9
- data/lib/sevendigital/model/locker_track.rb +9 -9
- data/lib/sevendigital/model/price.rb +9 -9
- data/lib/sevendigital/model/release.rb +57 -57
- data/lib/sevendigital/model/sevendigital_error.rb +11 -11
- data/lib/sevendigital/model/sevendigital_object.rb +57 -57
- data/lib/sevendigital/model/tag.rb +7 -7
- data/lib/sevendigital/model/track.rb +87 -83
- data/lib/sevendigital/model/user.rb +74 -74
- data/lib/sevendigital/pager.rb +20 -20
- data/lib/sevendigital/version.rb +5 -5
- data/spec/api_operator_cached_spec.rb +233 -178
- data/spec/api_operator_spec.rb +320 -320
- data/spec/api_request_spec.rb +46 -31
- data/spec/client_configuration_spec.rb +78 -78
- data/spec/client_spec.rb +161 -161
- data/spec/data/config/sevendigital.yml +5 -5
- data/spec/data/configuration_env_override.yml +5 -5
- data/spec/data/configuration_override.yml +1 -1
- data/spec/digestion_tract/api_response_digestor_spec.rb +93 -93
- data/spec/digestion_tract/artist_digestor_spec.rb +81 -81
- data/spec/digestion_tract/basket_digestor_spec.rb +31 -31
- data/spec/digestion_tract/basket_item_digestor_spec.rb +58 -58
- data/spec/digestion_tract/chart_digestor_spec.rb +52 -52
- data/spec/digestion_tract/country_digestor_spec.rb +33 -33
- data/spec/digestion_tract/digestor_spec.rb +33 -33
- data/spec/digestion_tract/download_url_digestor_spec.rb +40 -40
- data/spec/digestion_tract/format_digestor_spec.rb +38 -38
- data/spec/digestion_tract/label_digestor_spec.rb +33 -33
- data/spec/digestion_tract/list_digestor_spec.rb +31 -31
- data/spec/digestion_tract/list_item_digestor_spec.rb +80 -80
- data/spec/digestion_tract/locker_digestor_spec.rb +30 -30
- data/spec/digestion_tract/locker_release_digestor_spec.rb +70 -70
- data/spec/digestion_tract/locker_track_digestor_spec.rb +56 -56
- data/spec/digestion_tract/oauth_access_token_digestor_spec.rb +34 -34
- data/spec/digestion_tract/oauth_request_token_digestor_spec.rb +34 -34
- data/spec/digestion_tract/pager_digestor_spec.rb +50 -50
- data/spec/digestion_tract/price_digestor_spec.rb +73 -73
- data/spec/digestion_tract/release_digestor_spec.rb +115 -115
- data/spec/digestion_tract/tag_digestor_spec.rb +46 -46
- data/spec/digestion_tract/track_digestor_spec.rb +105 -104
- data/spec/digestion_tract/user_card_digestor_spec.rb +58 -58
- data/spec/digestion_tract/user_digestor_spec.rb +42 -42
- data/spec/integration/artist_spec.rb +99 -86
- data/spec/integration/country_spec.rb +18 -18
- data/spec/integration/list_spec.rb +22 -22
- data/spec/integration/purchasing_spec.rb +24 -24
- data/spec/integration/release_spec.rb +34 -34
- data/spec/integration/sevendigital_spec.yml +4 -4
- data/spec/integration/track_spec.rb +38 -23
- data/spec/integration/user_spec.rb +22 -22
- data/spec/management/artist_manager_spec.rb +172 -172
- data/spec/management/basket_manager_spec.rb +77 -77
- data/spec/management/country_manager_spec.rb +34 -34
- data/spec/management/list_manager_spec.rb +26 -26
- data/spec/management/oauth_manager_spec.rb +72 -72
- data/spec/management/release_manager_spec.rb +200 -200
- data/spec/management/tag_manager_spec.rb +34 -34
- data/spec/management/track_manager_spec.rb +111 -107
- data/spec/management/user_card_manager_spec.rb +78 -78
- data/spec/management/user_manager_spec.rb +218 -218
- data/spec/model/api_response_spec.rb +48 -48
- data/spec/model/artist_spec.rb +162 -162
- data/spec/model/basket_spec.rb +62 -62
- data/spec/model/release_spec.rb +133 -133
- data/spec/model/sevendigital_object_spec.rb +73 -73
- data/spec/model/track_spec.rb +146 -144
- data/spec/model/user_spec.rb +181 -181
- data/spec/spec_helper.rb +86 -84
- data/spec/test-xml/methods/artist/browse.xml +21 -21
- data/spec/test-xml/methods/artist/byTag/top.xml +50 -50
- data/spec/test-xml/methods/artist/chart.xml +30 -30
- data/spec/test-xml/methods/artist/details.xml +9 -9
- data/spec/test-xml/methods/artist/releases.xml +544 -544
- data/spec/test-xml/methods/artist/search.xml +18 -18
- data/spec/test-xml/methods/artist/similar.xml +39 -39
- data/spec/test-xml/methods/artist/tags.xml +11 -11
- data/spec/test-xml/methods/artist/toptracks.xml +279 -279
- data/spec/test-xml/methods/basket/additem.xml +23 -23
- data/spec/test-xml/methods/basket/create.xml +10 -10
- data/spec/test-xml/methods/basket/index.xml +23 -23
- data/spec/test-xml/methods/basket/removeitem.xml +10 -10
- data/spec/test-xml/methods/country/resolve.xml +6 -6
- data/spec/test-xml/methods/editorial/list.xml +170 -170
- data/spec/test-xml/methods/oauth/accesstoken.xml +5 -5
- data/spec/test-xml/methods/oauth/requesttoken.xml +5 -5
- data/spec/test-xml/methods/release/bydate.xml +173 -173
- data/spec/test-xml/methods/release/bytag/top.xml +150 -150
- data/spec/test-xml/methods/release/chart.xml +181 -181
- data/spec/test-xml/methods/release/details.xml +48 -48
- data/spec/test-xml/methods/release/recommend.xml +89 -89
- data/spec/test-xml/methods/release/search.xml +194 -194
- data/spec/test-xml/methods/release/tags.xml +27 -27
- data/spec/test-xml/methods/release/tracks.xml +28 -28
- data/spec/test-xml/methods/tag/index.xml +16 -16
- data/spec/test-xml/methods/track/chart.xml +149 -149
- data/spec/test-xml/methods/track/details.xml +30 -30
- data/spec/test-xml/methods/track/search.xml +159 -159
- data/spec/test-xml/methods/user/details.xml +4 -4
- data/spec/test-xml/methods/user/locker.xml +49 -49
- data/spec/test-xml/methods/user/payment/card/add.xml +7 -7
- data/spec/test-xml/methods/user/payment/card/index.xml +7 -7
- data/spec/test-xml/methods/user/purchase/basket.xml +48 -48
- data/spec/test-xml/methods/user/purchase/item.xml +48 -48
- data/spec/test-xml/methods/user/signUp.xml +5 -5
- data/spec/test-xml/objects/artist.xml +7 -7
- data/spec/test-xml/objects/artist_chart_item.xml +7 -7
- data/spec/test-xml/objects/artist_list.xml +22 -22
- data/spec/test-xml/objects/artist_list_empty.xml +4 -4
- data/spec/test-xml/objects/basket.xml +21 -21
- data/spec/test-xml/objects/basket_item.xml +11 -11
- data/spec/test-xml/objects/basket_item_list.xml +25 -25
- data/spec/test-xml/objects/basket_item_list_empty.xml +1 -1
- data/spec/test-xml/objects/download_url.xml +7 -7
- data/spec/test-xml/objects/list.xml +89 -89
- data/spec/test-xml/objects/list_item_list.xml +85 -85
- data/spec/test-xml/objects/list_item_release.xml +13 -13
- data/spec/test-xml/objects/locker.xml +47 -47
- data/spec/test-xml/objects/locker_release.xml +40 -40
- data/spec/test-xml/objects/locker_release_list.xml +87 -87
- data/spec/test-xml/objects/locker_release_list_empty.xml +4 -4
- data/spec/test-xml/objects/locker_release_one_item_list.xml +43 -43
- data/spec/test-xml/objects/locker_track.xml +25 -25
- data/spec/test-xml/objects/locker_track_list.xml +26 -26
- data/spec/test-xml/objects/locker_track_list_empty.xml +2 -2
- data/spec/test-xml/objects/nested_release_list.xml +22 -22
- data/spec/test-xml/objects/oauth_access_token.xml +3 -3
- data/spec/test-xml/objects/oauth_request_token.xml +3 -3
- data/spec/test-xml/objects/price.xml +7 -7
- data/spec/test-xml/objects/release.xml +40 -40
- data/spec/test-xml/objects/release_chart_item.xml +39 -39
- data/spec/test-xml/objects/release_list.xml +18 -18
- data/spec/test-xml/objects/release_list_empty.xml +4 -4
- data/spec/test-xml/objects/response.xml +38 -38
- data/spec/test-xml/objects/tag.xml +4 -4
- data/spec/test-xml/objects/track.xml +34 -33
- data/spec/test-xml/objects/track_chart_item.xml +27 -27
- data/spec/test-xml/objects/user.xml +4 -4
- data/spec/test-xml/objects/user_payment_card.xml +5 -5
- metadata +15 -25
@@ -1,75 +1,75 @@
|
|
1
|
-
module Sevendigital
|
2
|
-
|
3
|
-
class User < SevendigitalObject
|
4
|
-
|
5
|
-
attr_accessor :oauth_access_token, :id, :type
|
6
|
-
|
7
|
-
#Artist image URL (optional lazy-loaded property)
|
8
|
-
#@return [String]
|
9
|
-
sevendigital_basic_property :email_address
|
10
|
-
|
11
|
-
sevendigital_extended_property :locker
|
12
|
-
|
13
|
-
sevendigital_extended_property :cards
|
14
|
-
|
15
|
-
def authenticated?
|
16
|
-
return !@oauth_access_token.nil?
|
17
|
-
end
|
18
|
-
|
19
|
-
def get_details(options={})
|
20
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
21
|
-
user_with_details = @api_client.user.get_details(@oauth_access_token, options)
|
22
|
-
copy_basic_properties_from(user_with_details)
|
23
|
-
user_with_details
|
24
|
-
end
|
25
|
-
|
26
|
-
def get_locker(options={})
|
27
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
28
|
-
@api_client.user.get_locker(@oauth_access_token, options)
|
29
|
-
end
|
30
|
-
|
31
|
-
def get_cards(options={})
|
32
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
33
|
-
@api_client.user_payment_card.get_card_list(@oauth_access_token, options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def add_card(card_number, card_type, card_holder_name, card_start_date, card_expiry_date, card_issue_number,
|
37
|
-
card_verification_code, card_post_code, card_country, options={})
|
38
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
39
|
-
@api_client.user_payment_card.add_card(card_number, card_type, card_holder_name, card_start_date, card_expiry_date, card_issue_number,
|
40
|
-
card_verification_code, card_post_code, card_country, @oauth_access_token, options)
|
41
|
-
end
|
42
|
-
|
43
|
-
def select_card(card_id, options={})
|
44
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
45
|
-
@api_client.user_payment_card.select_card(card_id, @oauth_access_token, options)
|
46
|
-
end
|
47
|
-
|
48
|
-
# <b>DEPRECATED:</b> Please use <tt>purchase_item!</tt> instead.
|
49
|
-
def purchase!(release_id, track_id, price, token, options={})
|
50
|
-
warn "[DEPRECATION] `purchase!` is deprecated. Please use 'purchase_item!' instead."
|
51
|
-
purchase_item!(release_id, track_id, price, options={})
|
52
|
-
end
|
53
|
-
|
54
|
-
def purchase_item!(release_id, track_id, price, options={})
|
55
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
56
|
-
@api_client.user.purchase_item(release_id, track_id, price, @oauth_access_token, options)
|
57
|
-
end
|
58
|
-
|
59
|
-
def purchase_basket!(basket_id, options={})
|
60
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
61
|
-
@api_client.user.purchase_basket(basket_id, @oauth_access_token, options)
|
62
|
-
end
|
63
|
-
|
64
|
-
def stream_track_url(release_id, track_id, options={})
|
65
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
66
|
-
@api_client.user.get_stream_track_url(release_id, track_id, @oauth_access_token, options)
|
67
|
-
end
|
68
|
-
|
69
|
-
def add_card_url(return_url, options={})
|
70
|
-
raise Sevendigital::SevendigitalError if !authenticated?
|
71
|
-
@api_client.user.get_add_card_url(return_url, @oauth_access_token, options)
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
1
|
+
module Sevendigital
|
2
|
+
|
3
|
+
class User < SevendigitalObject
|
4
|
+
|
5
|
+
attr_accessor :oauth_access_token, :id, :type
|
6
|
+
|
7
|
+
#Artist image URL (optional lazy-loaded property)
|
8
|
+
#@return [String]
|
9
|
+
sevendigital_basic_property :email_address
|
10
|
+
|
11
|
+
sevendigital_extended_property :locker
|
12
|
+
|
13
|
+
sevendigital_extended_property :cards
|
14
|
+
|
15
|
+
def authenticated?
|
16
|
+
return !@oauth_access_token.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_details(options={})
|
20
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
21
|
+
user_with_details = @api_client.user.get_details(@oauth_access_token, options)
|
22
|
+
copy_basic_properties_from(user_with_details)
|
23
|
+
user_with_details
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_locker(options={})
|
27
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
28
|
+
@api_client.user.get_locker(@oauth_access_token, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_cards(options={})
|
32
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
33
|
+
@api_client.user_payment_card.get_card_list(@oauth_access_token, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_card(card_number, card_type, card_holder_name, card_start_date, card_expiry_date, card_issue_number,
|
37
|
+
card_verification_code, card_post_code, card_country, options={})
|
38
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
39
|
+
@api_client.user_payment_card.add_card(card_number, card_type, card_holder_name, card_start_date, card_expiry_date, card_issue_number,
|
40
|
+
card_verification_code, card_post_code, card_country, @oauth_access_token, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
def select_card(card_id, options={})
|
44
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
45
|
+
@api_client.user_payment_card.select_card(card_id, @oauth_access_token, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
# <b>DEPRECATED:</b> Please use <tt>purchase_item!</tt> instead.
|
49
|
+
def purchase!(release_id, track_id, price, token, options={})
|
50
|
+
warn "[DEPRECATION] `purchase!` is deprecated. Please use 'purchase_item!' instead."
|
51
|
+
purchase_item!(release_id, track_id, price, options={})
|
52
|
+
end
|
53
|
+
|
54
|
+
def purchase_item!(release_id, track_id, price, options={})
|
55
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
56
|
+
@api_client.user.purchase_item(release_id, track_id, price, @oauth_access_token, options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def purchase_basket!(basket_id, options={})
|
60
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
61
|
+
@api_client.user.purchase_basket(basket_id, @oauth_access_token, options)
|
62
|
+
end
|
63
|
+
|
64
|
+
def stream_track_url(release_id, track_id, options={})
|
65
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
66
|
+
@api_client.user.get_stream_track_url(release_id, track_id, @oauth_access_token, options)
|
67
|
+
end
|
68
|
+
|
69
|
+
def add_card_url(return_url, options={})
|
70
|
+
raise Sevendigital::SevendigitalError if !authenticated?
|
71
|
+
@api_client.user.get_add_card_url(return_url, @oauth_access_token, options)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
75
|
end
|
data/lib/sevendigital/pager.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
begin
|
2
|
-
require "will_paginate/collection"
|
3
|
-
rescue LoadError
|
4
|
-
end
|
5
|
-
|
6
|
-
module Sevendigital
|
7
|
-
#@private
|
8
|
-
#internal class used to page array results if will_paginate is available
|
9
|
-
class Pager # :nodoc:
|
10
|
-
attr_accessor :page, :page_size, :total_items
|
11
|
-
|
12
|
-
def paginate_list(list)
|
13
|
-
return list unless defined?(WillPaginate)
|
14
|
-
paged_list = WillPaginate::Collection.create(@page, @page_size, @total_items) do |pager|
|
15
|
-
pager.replace(list)
|
16
|
-
end
|
17
|
-
paged_list
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
1
|
+
begin
|
2
|
+
require "will_paginate/collection"
|
3
|
+
rescue LoadError
|
4
|
+
end
|
5
|
+
|
6
|
+
module Sevendigital
|
7
|
+
#@private
|
8
|
+
#internal class used to page array results if will_paginate is available
|
9
|
+
class Pager # :nodoc:
|
10
|
+
attr_accessor :page, :page_size, :total_items
|
11
|
+
|
12
|
+
def paginate_list(list)
|
13
|
+
return list unless defined?(WillPaginate)
|
14
|
+
paged_list = WillPaginate::Collection.create(@page, @page_size, @total_items) do |pager|
|
15
|
+
pager.replace(list)
|
16
|
+
end
|
17
|
+
paged_list
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/sevendigital/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
module Sevendigital
|
2
|
-
# The version of the gem
|
3
|
-
NAME = "7digital"
|
4
|
-
VERSION = '0.1.
|
5
|
-
end
|
1
|
+
module Sevendigital
|
2
|
+
# The version of the gem
|
3
|
+
NAME = "7digital"
|
4
|
+
VERSION = '0.1.5'
|
5
|
+
end
|
@@ -1,179 +1,234 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require 'ostruct'
|
3
|
-
require 'time'
|
4
|
-
|
5
|
-
describe "ApiOperatorCached" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
conf = test_configuration
|
9
|
-
@client = stub_api_client(conf)
|
10
|
-
@cache = stub(Hash)
|
11
|
-
@cached_operator = Sevendigital::ApiOperatorCached.new(@client, @cache)
|
12
|
-
@stub_api_request = stub_api_request()
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should not look into cache if request requires signature" do
|
16
|
-
http_response = stub_http_response
|
17
|
-
@stub_api_request.stub!(:requires_signature?).and_return(true)
|
18
|
-
@cached_operator.stub(:create_request_uri).and_return("key")
|
19
|
-
@cache.should_not_receive(:get)
|
20
|
-
@cached_operator.should_receive(:make_http_request).with(@stub_api_request).and_return(http_response)
|
21
|
-
@cached_operator.should_receive(:digest_http_response).with(http_response)
|
22
|
-
@cached_operator.call_api(@stub_api_request)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should not cache api response if request was signed" do
|
26
|
-
|
27
|
-
http_response = stub_http_response
|
28
|
-
@stub_api_request.stub!(:requires_signature?).and_return(true)
|
29
|
-
@cached_operator.stub(:create_request_uri).and_return("key")
|
30
|
-
|
31
|
-
@cached_operator.stub!(:make_http_request).and_return(http_response)
|
32
|
-
@cached_operator.stub!(:digest_http_response)
|
33
|
-
@cache.should_not_receive(:set)
|
34
|
-
@cached_operator.call_api(@stub_api_request)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should not make an http request if response already in cache and not out of date " do
|
38
|
-
http_response = stub_http_response
|
39
|
-
@cached_operator.stub(:create_request_uri).and_return("key")
|
40
|
-
@cache.stub!(:get).with("key").and_return(http_response)
|
41
|
-
@cached_operator.stub!(:response_out_of_date?).with(http_response).and_return(false)
|
42
|
-
@cached_operator.should_not_receive(:make_http_request)
|
43
|
-
@cached_operator.should_not_receive(:digest_http_response).with(http_response)
|
44
|
-
@cached_operator.call_api(@stub_api_request)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should make an http request if cached response is out of date " do
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
@
|
52
|
-
@
|
53
|
-
|
54
|
-
|
55
|
-
@
|
56
|
-
@cached_operator.should_receive(:
|
57
|
-
@cached_operator.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
@
|
66
|
-
@cache.stub!(:
|
67
|
-
@
|
68
|
-
@cached_operator.
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
@cached_operator.stub(:create_request_uri).and_return("key")
|
80
|
-
@cache.stub!(:get).with("key").and_return(nil)
|
81
|
-
@
|
82
|
-
@cached_operator.
|
83
|
-
|
84
|
-
|
85
|
-
@cached_operator.call_api(@stub_api_request)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
@cached_operator.
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
"Date" =>
|
127
|
-
})
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
end
|
178
|
-
|
1
|
+
require "spec_helper"
|
2
|
+
require 'ostruct'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
describe "ApiOperatorCached" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
conf = test_configuration
|
9
|
+
@client = stub_api_client(conf)
|
10
|
+
@cache = stub(Hash)
|
11
|
+
@cached_operator = Sevendigital::ApiOperatorCached.new(@client, @cache)
|
12
|
+
@stub_api_request = stub_api_request()
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not look into cache if request requires signature" do
|
16
|
+
http_response = stub_http_response
|
17
|
+
@stub_api_request.stub!(:requires_signature?).and_return(true)
|
18
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
19
|
+
@cache.should_not_receive(:get)
|
20
|
+
@cached_operator.should_receive(:make_http_request).with(@stub_api_request).and_return(http_response)
|
21
|
+
@cached_operator.should_receive(:digest_http_response).with(http_response)
|
22
|
+
@cached_operator.call_api(@stub_api_request)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not cache api response if request was signed" do
|
26
|
+
|
27
|
+
http_response = stub_http_response
|
28
|
+
@stub_api_request.stub!(:requires_signature?).and_return(true)
|
29
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
30
|
+
|
31
|
+
@cached_operator.stub!(:make_http_request).and_return(http_response)
|
32
|
+
@cached_operator.stub!(:digest_http_response)
|
33
|
+
@cache.should_not_receive(:set)
|
34
|
+
@cached_operator.call_api(@stub_api_request)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not make an http request if response already in cache and not out of date " do
|
38
|
+
http_response = stub_http_response
|
39
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
40
|
+
@cache.stub!(:get).with("key").and_return(http_response)
|
41
|
+
@cached_operator.stub!(:response_out_of_date?).with(http_response, nil).and_return(false)
|
42
|
+
@cached_operator.should_not_receive(:make_http_request)
|
43
|
+
@cached_operator.should_not_receive(:digest_http_response).with(http_response)
|
44
|
+
@cached_operator.call_api(@stub_api_request)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should make an http request if cached response is out of date " do
|
48
|
+
expired_http_response = stub_http_response
|
49
|
+
fresh_http_response = stub_http_response
|
50
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
51
|
+
@cache.stub!(:get).with("key").and_return(expired_http_response)
|
52
|
+
@cached_operator.stub!(:response_out_of_date?).with(expired_http_response, nil).and_return(true)
|
53
|
+
|
54
|
+
@cache.stub!(:set).and_return(nil)
|
55
|
+
@cached_operator.should_receive(:make_http_request).and_return(fresh_http_response)
|
56
|
+
@cached_operator.should_receive(:digest_http_response).with(fresh_http_response)
|
57
|
+
@cached_operator.call_api(@stub_api_request)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should use overridden cache_max_age when checking if response out of date" do
|
61
|
+
@stub_api_request.stub!(:options).and_return({:cache_max_age => 123})
|
62
|
+
expired_http_response = stub_http_response
|
63
|
+
fresh_http_response = stub_http_response
|
64
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
65
|
+
@cache.stub!(:get).with("key").and_return(expired_http_response)
|
66
|
+
@cache.stub!(:set).and_return(nil)
|
67
|
+
@cached_operator.stub(:make_http_request).and_return(fresh_http_response)
|
68
|
+
@cached_operator.stub(:digest_http_response).with(fresh_http_response)
|
69
|
+
|
70
|
+
@cached_operator.should_receive(:response_out_of_date?).with(expired_http_response, 123).and_return(true)
|
71
|
+
|
72
|
+
@cached_operator.call_api(@stub_api_request)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should make an http request if response not yet in cache, digest it and return the result " do
|
76
|
+
api_response = stub_api_response()
|
77
|
+
http_response = fake_http_response()
|
78
|
+
|
79
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
80
|
+
@cache.stub!(:get).with("key").and_return(nil)
|
81
|
+
@cache.stub!(:set).and_return(nil)
|
82
|
+
@cached_operator.should_receive(:make_http_request).with(@stub_api_request).and_return(http_response)
|
83
|
+
@cached_operator.should_receive(:digest_http_response).with(http_response).and_return(api_response)
|
84
|
+
|
85
|
+
response = @cached_operator.call_api(@stub_api_request)
|
86
|
+
response.should == api_response
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should cache uncached api response if request was not signed" do
|
90
|
+
http_response = stub_http_response()
|
91
|
+
api_response = stub_api_response()
|
92
|
+
|
93
|
+
@cached_operator.stub(:create_request_uri).and_return("key")
|
94
|
+
@cache.stub!(:get).with("key").and_return(nil)
|
95
|
+
@cached_operator.stub!(:make_http_request).and_return(http_response)
|
96
|
+
@cached_operator.stub!(:digest_http_response).with(http_response).and_return(api_response)
|
97
|
+
|
98
|
+
@cache.should_receive(:set).with("key", api_response).and_return(nil)
|
99
|
+
@cached_operator.call_api(@stub_api_request)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "response should be out of date if it's past its cache max age" do
|
103
|
+
now = Time.now.utc
|
104
|
+
yesterday = now - 24*60*60
|
105
|
+
max_age = 12*60*60
|
106
|
+
|
107
|
+
http_response = stub_http_response
|
108
|
+
http_response.stub!(:headers).and_return({
|
109
|
+
"cache-control" => "private, max-age=#{max_age}",
|
110
|
+
"Date" => yesterday.httpdate
|
111
|
+
})
|
112
|
+
|
113
|
+
@cached_operator.response_out_of_date?(http_response, nil, now).should == true
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
it "response should not be out of date if it's within its cache max age" do
|
119
|
+
now = Time.now.utc
|
120
|
+
yesterday = now - 24*60*60
|
121
|
+
max_age = 48*60*60
|
122
|
+
|
123
|
+
http_response = stub_http_response
|
124
|
+
http_response.stub!(:headers).and_return({
|
125
|
+
"cache-control" => "private, max-age=#{max_age}",
|
126
|
+
"Date" => yesterday.httpdate
|
127
|
+
})
|
128
|
+
|
129
|
+
@cached_operator.response_out_of_date?(http_response, nil, now).should == false
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
it "response should not be out of date if it's within overridden cache max age" do
|
134
|
+
now = Time.now.utc
|
135
|
+
yesterday = now - 24*60*60
|
136
|
+
max_age = 12*60*60
|
137
|
+
overridden_max_age = 48*60*60
|
138
|
+
|
139
|
+
http_response = stub_http_response
|
140
|
+
http_response.stub!(:headers).and_return({
|
141
|
+
"cache-control" => "private, max-age=#{max_age}",
|
142
|
+
"Date" => yesterday.httpdate
|
143
|
+
})
|
144
|
+
|
145
|
+
@cached_operator.response_out_of_date?(http_response, overridden_max_age, now).should == false
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
it "response should be out of date if it's not within overridden cache max age" do
|
150
|
+
now = Time.now.utc
|
151
|
+
yesterday = now - 24*60*60
|
152
|
+
max_age = 48*60*60
|
153
|
+
overridden_max_age = 12*60*60
|
154
|
+
|
155
|
+
http_response = stub_http_response
|
156
|
+
http_response.stub!(:headers).and_return({
|
157
|
+
"cache-control" => "private, max-age=#{max_age}",
|
158
|
+
"Date" => yesterday.httpdate
|
159
|
+
})
|
160
|
+
|
161
|
+
@cached_operator.response_out_of_date?(http_response, overridden_max_age, now).should == true
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
it "response should be out of date if it is missing caching headers" do
|
167
|
+
|
168
|
+
http_response = stub_http_response
|
169
|
+
http_response.stub!(:headers).and_return({})
|
170
|
+
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
|
171
|
+
|
172
|
+
http_response.stub!(:headers).and_return({
|
173
|
+
"Date" => Time.now.utc.httpdate
|
174
|
+
})
|
175
|
+
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
it "response should be out of date if no max-age has been specified" do
|
180
|
+
|
181
|
+
http_response = stub_http_response
|
182
|
+
http_response.stub!(:headers).and_return({"cache-control" => "no-cache", "Date" => Time.now.utc.httpdate})
|
183
|
+
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
it "response should be out of date if it is missing Date header" do
|
188
|
+
|
189
|
+
http_response = stub_http_response
|
190
|
+
http_response.stub!(:headers).and_return({"cache-control" => "private, max-age=#{2**30}"})
|
191
|
+
@cached_operator.response_out_of_date?(http_response, Time.now.utc).should == true
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should read default cache max age value from configuration" do
|
196
|
+
conf = test_configuration
|
197
|
+
conf.cache_max_age = {:"m" => 1000}
|
198
|
+
@client = stub_api_client(conf)
|
199
|
+
@cached_operator = Sevendigital::ApiOperatorCached.new(@client, @cache)
|
200
|
+
@cached_operator.default_cache_max_age(@stub_api_request).should == 1000
|
201
|
+
end
|
202
|
+
|
203
|
+
def fake_http_response
|
204
|
+
return Net::HTTP.new("1.1", 200, "response_body")
|
205
|
+
end
|
206
|
+
|
207
|
+
def stub_http_response
|
208
|
+
stub(Net::HTTP)
|
209
|
+
end
|
210
|
+
|
211
|
+
def stub_time(time)
|
212
|
+
Time.stub!(:now).and_return(time)
|
213
|
+
end
|
214
|
+
|
215
|
+
def stub_api_response
|
216
|
+
api_response = stub(Sevendigital::ApiResponse)
|
217
|
+
api_response.stub!(:out_of_date?).and_return(false)
|
218
|
+
api_response
|
219
|
+
end
|
220
|
+
|
221
|
+
def stub_api_request
|
222
|
+
api_request = stub(Sevendigital::ApiRequest)
|
223
|
+
|
224
|
+
api_request.stub!(:parameters).and_return({})
|
225
|
+
api_request.stub!(:options).and_return({})
|
226
|
+
api_request.stub!(:api_service).and_return(nil)
|
227
|
+
api_request.stub!(:api_method).and_return("m")
|
228
|
+
api_request.stub!(:requires_signature?).and_return(false)
|
229
|
+
api_request.stub!(:requires_secure_connection?).and_return(false)
|
230
|
+
api_request.stub!(:ensure_country_is_set)
|
231
|
+
return api_request
|
232
|
+
end
|
233
|
+
|
179
234
|
end
|