betfair_api_ng_rails 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -28
  3. data/Rakefile +10 -1
  4. data/betfair_api_ng_rails.gemspec +3 -0
  5. data/lib/betfair_api_ng_rails.rb +92 -73
  6. data/lib/betfair_api_ng_rails/api/concerns/errorable.rb +19 -0
  7. data/lib/betfair_api_ng_rails/api/config.rb +27 -0
  8. data/lib/betfair_api_ng_rails/api/connection.rb +34 -0
  9. data/lib/betfair_api_ng_rails/api/connection_ext/formatting.rb +34 -0
  10. data/lib/betfair_api_ng_rails/api/connection_ext/parsing.rb +29 -0
  11. data/lib/betfair_api_ng_rails/api/constants.rb +51 -0
  12. data/lib/betfair_api_ng_rails/api/data/attributes.rb +50 -0
  13. data/lib/betfair_api_ng_rails/api/data/base.rb +16 -0
  14. data/lib/betfair_api_ng_rails/api/data/competition.rb +20 -0
  15. data/lib/betfair_api_ng_rails/api/data/competition_result.rb +21 -0
  16. data/lib/betfair_api_ng_rails/api/data/concerns/hashable.rb +29 -0
  17. data/lib/betfair_api_ng_rails/api/data/constants.rb +11 -0
  18. data/lib/betfair_api_ng_rails/api/data/country_code_result.rb +19 -0
  19. data/lib/betfair_api_ng_rails/api/data/event.rb +20 -0
  20. data/lib/betfair_api_ng_rails/api/data/event_result.rb +19 -0
  21. data/lib/betfair_api_ng_rails/api/data/event_type.rb +20 -0
  22. data/lib/betfair_api_ng_rails/api/data/event_type_result.rb +19 -0
  23. data/lib/betfair_api_ng_rails/api/data/market_filter.rb +16 -0
  24. data/lib/betfair_api_ng_rails/api/data/market_type_result.rb +19 -0
  25. data/lib/betfair_api_ng_rails/api/data/time_range.rb +20 -0
  26. data/lib/betfair_api_ng_rails/api/data/time_range_result.rb +19 -0
  27. data/lib/betfair_api_ng_rails/api/data/venue_result.rb +19 -0
  28. data/lib/betfair_api_ng_rails/api/enums/bet_status.rb +15 -0
  29. data/lib/betfair_api_ng_rails/api/enums/concerns/enumable.rb +15 -0
  30. data/lib/betfair_api_ng_rails/api/enums/execution_report_error_code.rb +28 -0
  31. data/lib/betfair_api_ng_rails/api/enums/execution_report_status.rb +15 -0
  32. data/lib/betfair_api_ng_rails/api/enums/group_by.rb +17 -0
  33. data/lib/betfair_api_ng_rails/api/enums/instruction_report_error_code.rb +31 -0
  34. data/lib/betfair_api_ng_rails/api/enums/instruction_report_status.rb +14 -0
  35. data/lib/betfair_api_ng_rails/api/enums/market_betting_type.rb +17 -0
  36. data/lib/betfair_api_ng_rails/api/enums/market_projection.rb +18 -0
  37. data/lib/betfair_api_ng_rails/api/enums/market_sort.rb +17 -0
  38. data/lib/betfair_api_ng_rails/api/enums/market_status.rb +15 -0
  39. data/lib/betfair_api_ng_rails/api/enums/match_projection.rb +14 -0
  40. data/lib/betfair_api_ng_rails/api/enums/order_by.rb +14 -0
  41. data/lib/betfair_api_ng_rails/api/enums/order_projection.rb +14 -0
  42. data/lib/betfair_api_ng_rails/api/enums/order_status.rb +13 -0
  43. data/lib/betfair_api_ng_rails/api/enums/order_type.rb +14 -0
  44. data/lib/betfair_api_ng_rails/api/enums/persistence_type.rb +14 -0
  45. data/lib/betfair_api_ng_rails/api/enums/price_data.rb +16 -0
  46. data/lib/betfair_api_ng_rails/api/enums/rollup_model.rb +15 -0
  47. data/lib/betfair_api_ng_rails/api/enums/runner_status.rb +17 -0
  48. data/lib/betfair_api_ng_rails/api/enums/side.rb +13 -0
  49. data/lib/betfair_api_ng_rails/api/enums/sort_dir.rb +13 -0
  50. data/lib/betfair_api_ng_rails/api/enums/time_granularity.rb +14 -0
  51. data/lib/betfair_api_ng_rails/api/formatter_factory.rb +1 -1
  52. data/lib/betfair_api_ng_rails/api/formatters/js_tree_formatter.rb +38 -0
  53. data/lib/betfair_api_ng_rails/api/hashalator.rb +39 -0
  54. data/lib/betfair_api_ng_rails/api/helper.rb +29 -0
  55. data/lib/betfair_api_ng_rails/api/http/factory.rb +41 -0
  56. data/lib/betfair_api_ng_rails/api/http/helpers/information_response.rb +35 -0
  57. data/lib/betfair_api_ng_rails/api/http/helpers/keep_alive_response.rb +32 -0
  58. data/lib/betfair_api_ng_rails/api/http/helpers/session_response.rb +36 -0
  59. data/lib/betfair_api_ng_rails/api/http/requester.rb +75 -0
  60. data/lib/betfair_api_ng_rails/api/http/responser.rb +47 -0
  61. data/lib/betfair_api_ng_rails/api/parsers/base.rb +31 -0
  62. data/lib/betfair_api_ng_rails/api/parsers/list_competitions.rb +15 -0
  63. data/lib/betfair_api_ng_rails/api/parsers/list_countries.rb +15 -0
  64. data/lib/betfair_api_ng_rails/api/parsers/list_event_types.rb +15 -0
  65. data/lib/betfair_api_ng_rails/api/parsers/list_events.rb +15 -0
  66. data/lib/betfair_api_ng_rails/api/parsers/list_market_types.rb +15 -0
  67. data/lib/betfair_api_ng_rails/api/parsers/list_time_ranges.rb +15 -0
  68. data/lib/betfair_api_ng_rails/api/parsers/list_venues.rb +15 -0
  69. data/lib/betfair_api_ng_rails/api/provider.rb +38 -0
  70. data/lib/betfair_api_ng_rails/api/request_methods.rb +33 -0
  71. data/lib/betfair_api_ng_rails/api/session_manager.rb +54 -0
  72. data/lib/betfair_api_ng_rails/errors.rb +6 -0
  73. data/lib/betfair_api_ng_rails/railtie.rb +7 -0
  74. data/lib/betfair_api_ng_rails/version.rb +1 -1
  75. data/lib/generators/betfair_api_ng_rails/install/templates/betfair_api_ng_rails.rb +11 -9
  76. data/lib/generators/betfair_api_ng_rails/install/templates/betfair_api_ng_rails.yml +2 -0
  77. data/spec/betfair_api_ng_rails/api/connection_spec.rb +74 -0
  78. data/spec/betfair_api_ng_rails/api/data/concerns/hashable_spec.rb +81 -0
  79. data/spec/betfair_api_ng_rails/api/formatters/js_tree_formatter_spec.rb +70 -0
  80. data/spec/betfair_api_ng_rails/api/hashalator_spec.rb +146 -0
  81. data/spec/betfair_api_ng_rails/api/provider_spec.rb +91 -0
  82. data/spec/betfair_api_ng_rails/api/request_methods_spec.rb +60 -0
  83. data/spec/betfair_api_ng_rails/api/session_manager_spec.rb +59 -0
  84. data/spec/betfair_api_ng_rails/integration/list_competitions_spec.rb +18 -0
  85. data/spec/betfair_api_ng_rails/integration/list_countries_spec.rb +18 -0
  86. data/spec/betfair_api_ng_rails/integration/list_event_types_spec.rb +18 -0
  87. data/spec/betfair_api_ng_rails/integration/list_events_spec.rb +18 -0
  88. data/spec/betfair_api_ng_rails/integration/list_market_types_spec.rb +18 -0
  89. data/spec/betfair_api_ng_rails/integration/list_time_ranges_spec.rb +18 -0
  90. data/spec/betfair_api_ng_rails/integration/list_venues_spec.rb +18 -0
  91. data/spec/spec_helper.rb +16 -0
  92. data/spec/support/shared_examples/request_method.rb +18 -0
  93. data/spec/support/shared_examples/simple_list_filtering_request.rb +51 -0
  94. metadata +156 -66
  95. data/lib/betfair_api_ng_rails/api/base_provider.rb +0 -15
  96. data/lib/betfair_api_ng_rails/api/bf/concerns/errorable.rb +0 -21
  97. data/lib/betfair_api_ng_rails/api/bf/config.rb +0 -27
  98. data/lib/betfair_api_ng_rails/api/bf/constants.rb +0 -37
  99. data/lib/betfair_api_ng_rails/api/bf/data/attributes.rb +0 -52
  100. data/lib/betfair_api_ng_rails/api/bf/data/base.rb +0 -17
  101. data/lib/betfair_api_ng_rails/api/bf/data/competition.rb +0 -26
  102. data/lib/betfair_api_ng_rails/api/bf/data/competition_result.rb +0 -23
  103. data/lib/betfair_api_ng_rails/api/bf/data/concerns/hashable.rb +0 -40
  104. data/lib/betfair_api_ng_rails/api/bf/data/concerns/querable.rb +0 -46
  105. data/lib/betfair_api_ng_rails/api/bf/data/constants.rb +0 -13
  106. data/lib/betfair_api_ng_rails/api/bf/data/event.rb +0 -26
  107. data/lib/betfair_api_ng_rails/api/bf/data/event_result.rb +0 -21
  108. data/lib/betfair_api_ng_rails/api/bf/data/event_type.rb +0 -26
  109. data/lib/betfair_api_ng_rails/api/bf/data/event_type_result.rb +0 -21
  110. data/lib/betfair_api_ng_rails/api/bf/data/market_filter.rb +0 -19
  111. data/lib/betfair_api_ng_rails/api/bf/data/time_range.rb +0 -18
  112. data/lib/betfair_api_ng_rails/api/bf/data/time_range_result.rb +0 -21
  113. data/lib/betfair_api_ng_rails/api/bf/enums/bet_status.rb +0 -17
  114. data/lib/betfair_api_ng_rails/api/bf/enums/concerns/enumable.rb +0 -17
  115. data/lib/betfair_api_ng_rails/api/bf/enums/execution_report_error_code.rb +0 -30
  116. data/lib/betfair_api_ng_rails/api/bf/enums/execution_report_status.rb +0 -17
  117. data/lib/betfair_api_ng_rails/api/bf/enums/group_by.rb +0 -19
  118. data/lib/betfair_api_ng_rails/api/bf/enums/instruction_report_error_code.rb +0 -33
  119. data/lib/betfair_api_ng_rails/api/bf/enums/instruction_report_status.rb +0 -16
  120. data/lib/betfair_api_ng_rails/api/bf/enums/market_betting_type.rb +0 -19
  121. data/lib/betfair_api_ng_rails/api/bf/enums/market_projection.rb +0 -20
  122. data/lib/betfair_api_ng_rails/api/bf/enums/market_sort.rb +0 -19
  123. data/lib/betfair_api_ng_rails/api/bf/enums/market_status.rb +0 -17
  124. data/lib/betfair_api_ng_rails/api/bf/enums/match_projection.rb +0 -16
  125. data/lib/betfair_api_ng_rails/api/bf/enums/order_by.rb +0 -16
  126. data/lib/betfair_api_ng_rails/api/bf/enums/order_projection.rb +0 -16
  127. data/lib/betfair_api_ng_rails/api/bf/enums/order_status.rb +0 -15
  128. data/lib/betfair_api_ng_rails/api/bf/enums/order_type.rb +0 -16
  129. data/lib/betfair_api_ng_rails/api/bf/enums/persistence_type.rb +0 -16
  130. data/lib/betfair_api_ng_rails/api/bf/enums/price_data.rb +0 -18
  131. data/lib/betfair_api_ng_rails/api/bf/enums/rollup_model.rb +0 -17
  132. data/lib/betfair_api_ng_rails/api/bf/enums/runner_status.rb +0 -19
  133. data/lib/betfair_api_ng_rails/api/bf/enums/side.rb +0 -15
  134. data/lib/betfair_api_ng_rails/api/bf/enums/sort_dir.rb +0 -15
  135. data/lib/betfair_api_ng_rails/api/bf/enums/time_granularity.rb +0 -16
  136. data/lib/betfair_api_ng_rails/api/bf/formatters/js_tree_formatter.rb +0 -30
  137. data/lib/betfair_api_ng_rails/api/bf/helper.rb +0 -33
  138. data/lib/betfair_api_ng_rails/api/bf/http/factory.rb +0 -37
  139. data/lib/betfair_api_ng_rails/api/bf/http/requester.rb +0 -69
  140. data/lib/betfair_api_ng_rails/api/bf/http/responser.rb +0 -58
  141. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/base.rb +0 -33
  142. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_competitions.rb +0 -19
  143. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_event_types.rb +0 -19
  144. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_events.rb +0 -19
  145. data/lib/betfair_api_ng_rails/api/bf/orm/requester.rb +0 -52
  146. data/lib/betfair_api_ng_rails/api/bf/provider.rb +0 -38
  147. data/lib/betfair_api_ng_rails/api/bf/session_manager.rb +0 -46
@@ -0,0 +1,54 @@
1
+ require 'betfair_api_ng_rails/api/concerns/errorable'
2
+
3
+ module BetfairApiNgRails
4
+ module Api
5
+ class SessionManager
6
+ class << self
7
+ include Api::Concerns::Errorable
8
+ include Api::Constants
9
+
10
+ def ssoid
11
+ @ssoid ||= fetch_ssoid
12
+ end
13
+
14
+ def request_ssoid
15
+ ssoid
16
+ !has_errors?
17
+ end
18
+
19
+ def expire_ssoid
20
+ @ssoid = nil
21
+ end
22
+
23
+ def new_ssoid
24
+ expire_ssoid
25
+ ssoid
26
+ end
27
+
28
+ private
29
+
30
+ def fetch_ssoid
31
+ get_login_response.session_token.tap { |sid| send_keep_alive(sid) }
32
+ end
33
+
34
+ def get_login_response
35
+ http_requester.do_request
36
+ end
37
+
38
+ def send_keep_alive(sid)
39
+ return unless Api::Config.keep_alive_session
40
+ keep_alive_requester(sid).do_request
41
+ end
42
+
43
+ def http_requester
44
+ @_http_requester ||= Api::Http::Factory.session_requester
45
+ end
46
+
47
+ def keep_alive_requester(sid)
48
+ Api::Http::Factory.keep_alive_requester sid
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,6 @@
1
+ module BetfairApiNgRails
2
+
3
+ class NoConnectionError < ::StandardError
4
+ end
5
+
6
+ end
@@ -0,0 +1,7 @@
1
+ module BetfairApiNgRails
2
+ class Railtie < Rails::Railtie
3
+ config.after_initialize do
4
+ BetfairApiNgRails.connection = BetfairApiNgRails::Api::Connection.new
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module BetfairApiNgRails
2
- VERSION = "0.0.8"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -3,14 +3,16 @@ BetfairApiNgRails.config do |config|
3
3
 
4
4
  load_config = YAML.load_file("#{Rails.root}/config/betfair_api_ng_rails.yml")[Rails.env]
5
5
 
6
- config.application_key = load_config['application_key']
7
- config.ssl_key_filepath = "#{Rails.root}/public/client-2048.key"
8
- config.ssl_crt_filepath = "#{Rails.root}/public/client-2048.crt"
9
- config.login_url = load_config['login_url']
10
- config.api_url = load_config['api_url']
11
- config.username = load_config['username']
12
- config.password = load_config['password']
13
- config.locale = :ru
14
- config.formatter = nil #out of box available formatter for JsTree
6
+ config.application_key = load_config['application_key']
7
+ config.ssl_key_filepath = "#{Rails.root}/public/client-2048.key"
8
+ config.ssl_crt_filepath = "#{Rails.root}/public/client-2048.crt"
9
+ config.login_url = load_config['login_url']
10
+ config.api_url = load_config['api_url']
11
+ config.keep_alive_url = load_config['keep_alive_url']
12
+ config.username = load_config['username']
13
+ config.password = load_config['password']
14
+ config.locale = :ru
15
+ config.formatter = nil #out of box available formatter for JsTree
16
+ config.keep_alive_session = true
15
17
 
16
18
  end
@@ -2,6 +2,7 @@ development:
2
2
  application_key: YOUR_APP_KEY
3
3
  login_url: https://identitysso.betfair.com/api/certlogin
4
4
  api_url: https://api-ng.betstores.com/betting/betfair/services/api.betfair.com/exchange/betting/json-rpc/v1
5
+ keep_alive_url: https://identitysso.betfair.com/api/keepAlive
5
6
  username: YOUR_ACCOUNT_USERNAME
6
7
  password: YOUR_ACCOUNT_PASSWORD
7
8
 
@@ -9,5 +10,6 @@ production:
9
10
  application_key: YOUR_APP_KEY
10
11
  login_url: https://identitysso.betfair.com/api/certlogin
11
12
  api_url: https://api-ng.betstores.com/betting/betfair/services/api.betfair.com/exchange/betting/json-rpc/v1
13
+ keep_alive_url: https://identitysso.betfair.com/api/keepAlive
12
14
  username: YOUR_ACCOUNT_USERNAME
13
15
  password: YOUR_ACCOUNT_PASSWORD
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe BetfairApiNgRails::Api::Connection do
4
+
5
+ subject { described_class.new }
6
+
7
+ describe "#initialize" do
8
+
9
+ it "requests session identifier" do
10
+ expect_any_instance_of(described_class).to receive(:request_ssoid)
11
+ described_class.new
12
+ end
13
+
14
+ end
15
+
16
+ describe "#request" do
17
+
18
+ let(:provider) { double(:provider) }
19
+ let(:parser) { double(:parser, process: true) }
20
+
21
+ before(:each) do
22
+ expect_any_instance_of(described_class).to receive(:request_ssoid)
23
+ expect(subject).to receive(:init_parser).and_return parser
24
+ end
25
+
26
+ it "calls provider's fetch method" do
27
+ expect(subject).to receive(:provider).and_return provider
28
+ expect(provider).to receive(:fetch).with(method: 'method', params: {})
29
+ expect(subject).to receive(:hashing).with({}).and_return({})
30
+ subject.request 'method'
31
+ end
32
+
33
+ end
34
+
35
+ describe "private method" do
36
+
37
+ describe "#provider" do
38
+
39
+ before(:each) { expect_any_instance_of(described_class).to receive(:request_ssoid) }
40
+
41
+ it "inits new provider" do
42
+ expect(BetfairApiNgRails::Api::Provider).to receive(:new)
43
+ subject.send(:provider)
44
+ end
45
+
46
+ end
47
+
48
+ describe "#request_ssoid" do
49
+
50
+ it "requests new ssoid for new connection" do
51
+ expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).at_least(:once)
52
+ subject.send(:request_ssoid)
53
+ end
54
+
55
+ end
56
+
57
+ describe "#hashing" do
58
+
59
+ let(:hashalator) { double(:hashalator) }
60
+ let(:params) { double(:params) }
61
+
62
+ before(:each) { expect_any_instance_of(described_class).to receive(:request_ssoid) }
63
+
64
+ it "hashalators params" do
65
+ expect(BetfairApiNgRails::Api::Hashalator).to receive(:new).with(params).and_return hashalator
66
+ expect(hashalator).to receive(:to_hash)
67
+ subject.send :hashing, params
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ class TestClass
4
+ include BetfairApiNgRails::Api::Data::Concerns::Hashable
5
+
6
+ attr_accessor :name
7
+
8
+ def initialize()
9
+ @name = 'Walli'
10
+ end
11
+
12
+ def self.to_hash_attrs
13
+ [:name]
14
+ end
15
+
16
+ end
17
+
18
+ describe BetfairApiNgRails::Api::Data::Concerns::Hashable do
19
+
20
+ subject { TestClass.new }
21
+
22
+ describe "#to_hash" do
23
+
24
+ let(:attributes) { double(:attributes) }
25
+ let(:hashalator) { double(:hashalator) }
26
+
27
+ it "returns Hashalator's to_hash result" do
28
+ expect(subject).to receive(:attributes_hash).and_return attributes
29
+ expect(BetfairApiNgRails::Api::Hashalator).to receive(:new).with(attributes).and_return hashalator
30
+ expect(hashalator).to receive(:to_hash)
31
+ subject.to_hash
32
+ end
33
+
34
+ end
35
+
36
+ describe "private method" do
37
+
38
+ describe "#attributes_hash" do
39
+
40
+ context "returns hash represantation of attributes and its values" do
41
+
42
+ its(:attributes_hash) { is_expected.to eq({:name => 'Walli'}) }
43
+
44
+ end
45
+
46
+ end
47
+
48
+ describe "#get_attr_value" do
49
+
50
+ it "returns attribute value by its name" do
51
+ expect(subject.send(:get_attr_value, :name)).to eq 'Walli'
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ describe "integration tests" do
59
+
60
+ context "returns hash represention of TestClass instance" do
61
+
62
+ its(:to_hash) { is_expected.to eq({'name' => 'Walli'}) }
63
+
64
+ end
65
+
66
+ context 'dont include nil attributes in resulting hash' do
67
+
68
+ before(:each) do
69
+ subject.class.send :instance_eval, <<-CODE
70
+ attr_accessor :second_name
71
+ def self.to_hash_attrs; [:name, :second_name]; end
72
+ CODE
73
+ end
74
+
75
+ its(:to_hash) { is_expected.to eq({'name' => 'Walli'}) }
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe BetfairApiNgRails::Api::Formatters::JsTreeFormatter do
4
+
5
+ subject { described_class.new }
6
+
7
+ describe "#process" do
8
+
9
+ let(:record) { double(:record) }
10
+
11
+ it "formats each resource record" do
12
+ expect(subject).to receive(:format_record).with(record, :some_request)
13
+ subject.process([record], as: 'listSomeRequest')
14
+ end
15
+
16
+ end
17
+
18
+ describe "private method" do
19
+
20
+ describe "#resource_from_method" do
21
+
22
+ it "returns proper resource name" do
23
+ expect(subject.send(:resource_from_method, 'listEventTypes')).to eq :event_types
24
+ end
25
+
26
+ end
27
+
28
+ describe "#format_record" do
29
+
30
+ context 'when passed nil record' do
31
+
32
+ it "returns empty hash" do
33
+ expect(subject.send(:format_record, nil, 'resource')).to eq({})
34
+ end
35
+
36
+ end
37
+
38
+ context 'when passed valid params for formatting' do
39
+
40
+ let(:resource) { double(:resource, id: 1, name: 'name', to_hash: true) }
41
+ let(:record) { double(:record, some_resource: resource) }
42
+
43
+ subject { described_class.new.send(:format_record, record, :some_resources) }
44
+
45
+ its([:id]) { is_expected.to eq(1) }
46
+ its([:text]) { is_expected.to eq('name') }
47
+
48
+ context 'and resource allow children' do
49
+
50
+ before { stub_const("BetfairApiNgRails::Api::Formatters::JsTreeFormatter::ALLOW_CHILDREN", [:some_resources]) }
51
+
52
+ its([:children]) { is_expected.to eq(true) }
53
+
54
+ end
55
+
56
+ context 'and resource not allow children' do
57
+
58
+ before { stub_const("BetfairApiNgRails::Api::Formatters::JsTreeFormatter::ALLOW_CHILDREN", []) }
59
+
60
+ its([:children]) { is_expected.to eq(false) }
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+
3
+ describe BetfairApiNgRails::Api::Hashalator do
4
+
5
+ let(:hashable) { { some_key: :some_value } }
6
+
7
+ subject { described_class.new(hashable) }
8
+
9
+ its(:hashable) { is_expected.to eq hashable }
10
+
11
+ describe "#to_hash" do
12
+
13
+ it "iterates though elements and process they" do
14
+ expect(subject).to receive(:hash_key).at_least(:once)
15
+ expect(subject).to receive(:hash_value).at_least(:once)
16
+ subject.to_hash
17
+ end
18
+
19
+ end
20
+
21
+ describe "private method" do
22
+
23
+ describe "#hashing_val" do
24
+
25
+ let(:el) { double(:el) }
26
+
27
+ context 'when array passed' do
28
+
29
+ let(:array) { [el] }
30
+
31
+ it "calls hash_value on each element" do
32
+ expect(subject).to receive(:hash_value).with(el)
33
+ subject.send :hashing_val, array
34
+ end
35
+
36
+ end
37
+
38
+ context 'when single value passed' do
39
+
40
+ it "calls to_hash on it" do
41
+ expect(el).to receive(:to_hash)
42
+ subject.send :hashing_val, el
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ describe "#hash_key" do
50
+
51
+ it "returns key for api request hash" do
52
+ expect(subject.send(:hash_key, :some_key)).to eq 'someKey'
53
+ end
54
+
55
+ end
56
+
57
+ describe "#need_to_hash?" do
58
+
59
+ before { stub_const("BetfairApiNgRails::Api::Data::Constants::ATTRS_AS_IS", classes) }
60
+
61
+ context 'when ATTRS_AS_IS include value class' do
62
+
63
+ let(:classes) { [String] }
64
+
65
+ it "returns false" do
66
+ expect(subject.send(:need_to_hash?, 'value')).to eq false
67
+ end
68
+
69
+ end
70
+
71
+ context 'when ATTRS_AS_IS not include value class' do
72
+
73
+ let(:classes) { [String] }
74
+
75
+ it "returns true" do
76
+ expect(subject.send(:need_to_hash?, 1)).to eq true
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ describe "#hash_value" do
84
+
85
+ let(:value) { double(:value) }
86
+
87
+ context 'when need to hashing value' do
88
+
89
+ before { expect(subject).to receive(:need_to_hash?).with(value).and_return(true) }
90
+
91
+ it "calls to_hash on value" do
92
+ expect(subject).to receive(:hashing_val).with(value)
93
+ subject.send :hash_value, value
94
+ end
95
+
96
+ end
97
+
98
+ context 'when dont need to hashing value' do
99
+
100
+ before { expect(subject).to receive(:need_to_hash?).with(value).and_return(false) }
101
+
102
+ it "returns value" do
103
+ expect(subject).to receive(:hashing_val).with(value).exactly(0).times
104
+ subject.send :hash_value, value
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+
113
+ context 'integration tests' do
114
+
115
+ subject { described_class.new(hashable).to_hash }
116
+
117
+ context 'when only simple values passed' do
118
+
119
+ let(:hashable) { { some_key: :some_value } }
120
+
121
+ it { is_expected.to eq({'someKey' => :some_value}) }
122
+
123
+ end
124
+
125
+ context 'when array with simple values passes' do
126
+
127
+ let(:hashable) { {some_key: [1, 2, 3]} }
128
+
129
+ it { is_expected.to eq({'someKey' => [1, 2, 3]}) }
130
+
131
+ end
132
+
133
+ context 'when array with at least one complex value passes' do
134
+
135
+ let(:el) { double(:el) }
136
+ let(:hashable) { {some_key: [el]} }
137
+
138
+ before { expect(el).to receive(:to_hash).and_return(el) }
139
+
140
+ it { is_expected.to eq({'someKey' => [el]}) }
141
+
142
+ end
143
+
144
+ end
145
+
146
+ end