betfair_api_ng_rails 1.1.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -1
  3. data/Rakefile +8 -8
  4. data/betfair_api_ng_rails.gemspec +12 -7
  5. data/lib/betfair_api_ng_rails.rb +162 -98
  6. data/lib/betfair_api_ng_rails/account.rb +4 -0
  7. data/lib/betfair_api_ng_rails/account_manager.rb +36 -0
  8. data/lib/betfair_api_ng_rails/account_session.rb +5 -0
  9. data/lib/betfair_api_ng_rails/account_session_manager.rb +29 -0
  10. data/lib/betfair_api_ng_rails/api/caching/helper.rb +34 -0
  11. data/lib/betfair_api_ng_rails/api/caching/response_cache.rb +57 -0
  12. data/lib/betfair_api_ng_rails/api/concerns/errorable.rb +1 -3
  13. data/lib/betfair_api_ng_rails/api/config.rb +20 -8
  14. data/lib/betfair_api_ng_rails/api/connection.rb +28 -10
  15. data/lib/betfair_api_ng_rails/api/connection_ext/caching.rb +60 -0
  16. data/lib/betfair_api_ng_rails/api/connection_ext/error_handling.rb +5 -1
  17. data/lib/betfair_api_ng_rails/api/connection_ext/logging.rb +34 -0
  18. data/lib/betfair_api_ng_rails/api/connection_ext/parsing.rb +2 -4
  19. data/lib/betfair_api_ng_rails/api/connection_ext/ssoid_refreshing.rb +40 -0
  20. data/lib/betfair_api_ng_rails/api/constants.rb +39 -18
  21. data/lib/betfair_api_ng_rails/api/data/account_details_response.rb +27 -0
  22. data/lib/betfair_api_ng_rails/api/data/account_funds_response.rb +26 -0
  23. data/lib/betfair_api_ng_rails/api/data/account_statement_report.rb +17 -0
  24. data/lib/betfair_api_ng_rails/api/data/competition.rb +3 -4
  25. data/lib/betfair_api_ng_rails/api/data/competition_result.rb +1 -3
  26. data/lib/betfair_api_ng_rails/api/data/concerns/attributable.rb +2 -6
  27. data/lib/betfair_api_ng_rails/api/data/concerns/hashable.rb +11 -4
  28. data/lib/betfair_api_ng_rails/api/data/constants.rb +2 -4
  29. data/lib/betfair_api_ng_rails/api/data/country_code_result.rb +1 -3
  30. data/lib/betfair_api_ng_rails/api/data/event.rb +1 -2
  31. data/lib/betfair_api_ng_rails/api/data/event_result.rb +1 -3
  32. data/lib/betfair_api_ng_rails/api/data/limit_on_close_order.rb +22 -0
  33. data/lib/betfair_api_ng_rails/api/data/limit_order.rb +23 -0
  34. data/lib/betfair_api_ng_rails/api/data/market_book.rb +2 -4
  35. data/lib/betfair_api_ng_rails/api/data/market_filter.rb +3 -2
  36. data/lib/betfair_api_ng_rails/api/data/market_on_close_order.rb +20 -0
  37. data/lib/betfair_api_ng_rails/api/data/order.rb +1 -3
  38. data/lib/betfair_api_ng_rails/api/data/place_execution_report.rb +24 -0
  39. data/lib/betfair_api_ng_rails/api/data/place_instruction.rb +25 -0
  40. data/lib/betfair_api_ng_rails/api/data/place_instruction_report.rb +27 -0
  41. data/lib/betfair_api_ng_rails/api/data/statement_item.rb +30 -0
  42. data/lib/betfair_api_ng_rails/api/data/statement_legacy_data.rb +40 -0
  43. data/lib/betfair_api_ng_rails/api/endpoint_factory.rb +21 -0
  44. data/lib/betfair_api_ng_rails/api/endpoints/australian.rb +17 -0
  45. data/lib/betfair_api_ng_rails/api/endpoints/base.rb +42 -0
  46. data/lib/betfair_api_ng_rails/api/endpoints/italian.rb +17 -0
  47. data/lib/betfair_api_ng_rails/api/endpoints/spanish.rb +17 -0
  48. data/lib/betfair_api_ng_rails/api/endpoints/uk.rb +17 -0
  49. data/lib/betfair_api_ng_rails/api/enums/execution_report_error_code.rb +22 -1
  50. data/lib/betfair_api_ng_rails/api/enums/execution_report_status.rb +12 -1
  51. data/lib/betfair_api_ng_rails/api/enums/include_item.rb +23 -0
  52. data/lib/betfair_api_ng_rails/api/enums/instruction_report_error_code.rb +25 -1
  53. data/lib/betfair_api_ng_rails/api/enums/instruction_report_status.rb +8 -1
  54. data/lib/betfair_api_ng_rails/api/enums/item_class.rb +19 -0
  55. data/lib/betfair_api_ng_rails/api/enums/order_status.rb +7 -1
  56. data/lib/betfair_api_ng_rails/api/enums/order_type.rb +11 -2
  57. data/lib/betfair_api_ng_rails/api/enums/persistence_type.rb +8 -1
  58. data/lib/betfair_api_ng_rails/api/enums/side.rb +7 -2
  59. data/lib/betfair_api_ng_rails/api/enums/wallet.rb +20 -0
  60. data/lib/betfair_api_ng_rails/api/formatter_factory.rb +2 -4
  61. data/lib/betfair_api_ng_rails/api/formatters/js_tree_formatter.rb +2 -4
  62. data/lib/betfair_api_ng_rails/api/hashalator.rb +6 -6
  63. data/lib/betfair_api_ng_rails/api/helper.rb +2 -4
  64. data/lib/betfair_api_ng_rails/api/http/factory.rb +19 -14
  65. data/lib/betfair_api_ng_rails/api/http/helpers/information_response.rb +2 -4
  66. data/lib/betfair_api_ng_rails/api/http/helpers/keep_alive_response.rb +3 -4
  67. data/lib/betfair_api_ng_rails/api/http/helpers/session_response.rb +3 -4
  68. data/lib/betfair_api_ng_rails/api/http/proxy_requester.rb +87 -0
  69. data/lib/betfair_api_ng_rails/api/http/requester.rb +3 -3
  70. data/lib/betfair_api_ng_rails/api/http/responser.rb +4 -3
  71. data/lib/betfair_api_ng_rails/api/invalid_endpoint_error.rb +6 -0
  72. data/lib/betfair_api_ng_rails/api/logs/console_logger.rb +13 -0
  73. data/lib/betfair_api_ng_rails/api/logs/file_logger.rb +23 -0
  74. data/lib/betfair_api_ng_rails/api/parsers/base.rb +14 -6
  75. data/lib/betfair_api_ng_rails/api/parsers/get_account_details.rb +13 -0
  76. data/lib/betfair_api_ng_rails/api/parsers/get_account_funds.rb +14 -0
  77. data/lib/betfair_api_ng_rails/api/parsers/get_account_statement.rb +15 -0
  78. data/lib/betfair_api_ng_rails/api/parsers/list_base.rb +18 -0
  79. data/lib/betfair_api_ng_rails/api/parsers/list_competitions.rb +5 -7
  80. data/lib/betfair_api_ng_rails/api/parsers/list_countries.rb +5 -7
  81. data/lib/betfair_api_ng_rails/api/parsers/list_event_types.rb +5 -7
  82. data/lib/betfair_api_ng_rails/api/parsers/list_events.rb +5 -7
  83. data/lib/betfair_api_ng_rails/api/parsers/list_market_book.rb +5 -7
  84. data/lib/betfair_api_ng_rails/api/parsers/list_market_catalogue.rb +5 -7
  85. data/lib/betfair_api_ng_rails/api/parsers/list_market_types.rb +5 -7
  86. data/lib/betfair_api_ng_rails/api/parsers/list_time_ranges.rb +5 -7
  87. data/lib/betfair_api_ng_rails/api/parsers/list_venues.rb +5 -7
  88. data/lib/betfair_api_ng_rails/api/parsers/place_orders.rb +14 -0
  89. data/lib/betfair_api_ng_rails/api/parsers/single_base.rb +19 -0
  90. data/lib/betfair_api_ng_rails/api/provider.rb +11 -14
  91. data/lib/betfair_api_ng_rails/api/request_method.rb +43 -0
  92. data/lib/betfair_api_ng_rails/api/request_methods/account.rb +51 -0
  93. data/lib/betfair_api_ng_rails/api/request_methods/all.rb +26 -0
  94. data/lib/betfair_api_ng_rails/api/request_methods/betting.rb +30 -0
  95. data/lib/betfair_api_ng_rails/api/session_manager.rb +34 -31
  96. data/lib/betfair_api_ng_rails/bad_response_error.rb +4 -0
  97. data/lib/betfair_api_ng_rails/errors.rb +3 -2
  98. data/lib/betfair_api_ng_rails/railtie.rb +7 -2
  99. data/lib/betfair_api_ng_rails/version.rb +1 -1
  100. data/lib/generators/betfair_api_ng_rails/install/templates/betfair_api_ng_rails.rb +20 -11
  101. data/lib/generators/betfair_api_ng_rails/install/templates/betfair_api_ng_rails.yml +5 -7
  102. data/spec/lib/betfair_api_ng_rails/account_manager_spec.rb +76 -0
  103. data/spec/lib/betfair_api_ng_rails/account_session_manager_spec.rb +59 -0
  104. data/spec/lib/betfair_api_ng_rails/account_session_spec.rb +13 -0
  105. data/spec/lib/betfair_api_ng_rails/account_spec.rb +18 -0
  106. data/spec/lib/betfair_api_ng_rails/api/caching/helper_spec.rb +89 -0
  107. data/spec/lib/betfair_api_ng_rails/api/caching/response_cache_spec.rb +131 -0
  108. data/spec/lib/betfair_api_ng_rails/api/config_spec.rb +37 -0
  109. data/spec/lib/betfair_api_ng_rails/api/connection_spec.rb +99 -0
  110. data/spec/lib/betfair_api_ng_rails/api/data/account_statement_report_spec.rb +11 -0
  111. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/competition_result_spec.rb +0 -0
  112. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/competition_spec.rb +0 -0
  113. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/concerns/attributable_spec.rb +0 -0
  114. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/concerns/hashable_spec.rb +16 -32
  115. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/concerns/jsonable_spec.rb +0 -0
  116. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/country_code_result_spec.rb +0 -0
  117. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/event_result_spec.rb +0 -0
  118. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/event_spec.rb +0 -0
  119. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/event_type_result_spec.rb +0 -0
  120. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/event_type_spec.rb +0 -0
  121. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/ex_best_offers_overrides_spec.rb +0 -0
  122. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/exchange_prices_spec.rb +0 -0
  123. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/market_book_spec.rb +1 -6
  124. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/market_catalogue_spec.rb +0 -0
  125. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/market_description_spec.rb +0 -0
  126. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/market_filter_spec.rb +0 -0
  127. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/market_type_result_spec.rb +0 -0
  128. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/match_spec.rb +0 -0
  129. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/order_spec.rb +0 -0
  130. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/price_projection_spec.rb +0 -0
  131. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/price_size_spec.rb +0 -0
  132. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/runner_catalog_spec.rb +0 -0
  133. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/runner_spec.rb +0 -0
  134. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/starting_prices_spec.rb +0 -0
  135. data/spec/lib/betfair_api_ng_rails/api/data/statement_item_spec.rb +9 -0
  136. data/spec/lib/betfair_api_ng_rails/api/data/statement_legacy_data_spec.rb +6 -0
  137. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/time_range_result_spec.rb +0 -0
  138. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/time_range_spec.rb +1 -3
  139. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/data/venue_result_spec.rb +0 -0
  140. data/spec/lib/betfair_api_ng_rails/api/endpoint_factory_spec.rb +27 -0
  141. data/spec/lib/betfair_api_ng_rails/api/endpoints/australian_spec.rb +6 -0
  142. data/spec/lib/betfair_api_ng_rails/api/endpoints/base_spec.rb +5 -0
  143. data/spec/lib/betfair_api_ng_rails/api/endpoints/italian_spec.rb +6 -0
  144. data/spec/lib/betfair_api_ng_rails/api/endpoints/spanish_spec.rb +7 -0
  145. data/spec/lib/betfair_api_ng_rails/api/endpoints/uk_spec.rb +6 -0
  146. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/formatters/js_tree_formatter_spec.rb +0 -0
  147. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/hashalator_spec.rb +27 -46
  148. data/spec/lib/betfair_api_ng_rails/api/http/factory_spec.rb +40 -0
  149. data/spec/lib/betfair_api_ng_rails/api/logs/console_logger_spec.rb +18 -0
  150. data/spec/lib/betfair_api_ng_rails/api/logs/file_logger_spec.rb +33 -0
  151. data/spec/lib/betfair_api_ng_rails/api/provider_spec.rb +53 -0
  152. data/spec/lib/betfair_api_ng_rails/api/request_method_spec.rb +38 -0
  153. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/api/request_methods_spec.rb +47 -32
  154. data/spec/lib/betfair_api_ng_rails/api/session_manager_spec.rb +77 -0
  155. data/spec/lib/betfair_api_ng_rails/integration/get_account_statement_spec.rb +57 -0
  156. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_competitions_spec.rb +7 -9
  157. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_countries_spec.rb +7 -9
  158. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_event_types_spec.rb +7 -9
  159. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_events_spec.rb +7 -9
  160. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_market_book_spec.rb +7 -9
  161. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_market_catalogue_spec.rb +7 -16
  162. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_market_types_spec.rb +7 -9
  163. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_time_ranges_spec.rb +7 -9
  164. data/spec/{betfair_api_ng_rails → lib/betfair_api_ng_rails}/integration/list_venues_spec.rb +7 -9
  165. data/spec/lib/betfair_api_ng_rails/integration/place_orders_spec.rb +55 -0
  166. data/spec/spec_helper.rb +9 -1
  167. data/spec/support/base_yaml_loader.rb +2 -4
  168. data/spec/support/data/models.yml +37 -1
  169. data/spec/support/data_models_yaml_loader.rb +1 -3
  170. data/spec/support/have_attr_accessor.rb +18 -0
  171. data/spec/support/helper.rb +1 -3
  172. data/spec/support/shared_examples/base_endpoint.rb +5 -0
  173. data/spec/support/shared_examples/concrete_endpoint.rb +28 -0
  174. data/spec/support/shared_examples/data_model.rb +2 -11
  175. data/spec/support/shared_examples/request_method.rb +5 -8
  176. data/spec/support/shared_examples/simple_list_filtering_request.rb +10 -19
  177. metadata +252 -117
  178. data/lib/betfair_api_ng_rails/api/request_methods.rb +0 -41
  179. data/spec/betfair_api_ng_rails/api/connection_spec.rb +0 -76
  180. data/spec/betfair_api_ng_rails/api/provider_spec.rb +0 -91
  181. data/spec/betfair_api_ng_rails/api/session_manager_spec.rb +0 -59
@@ -0,0 +1,5 @@
1
+ module BetfairApiNgRails
2
+ class AccountSession < Struct.new(:username, :ssoid)
3
+ end
4
+ end
5
+
@@ -0,0 +1,29 @@
1
+ require 'singleton'
2
+
3
+ module BetfairApiNgRails
4
+ class AccountSessionManager
5
+ include Singleton
6
+
7
+ attr_reader :sessions
8
+
9
+ def initialize
10
+ clear
11
+ end
12
+
13
+ def store(session)
14
+ sessions[session.username] = session
15
+ end
16
+
17
+ def get(username)
18
+ sessions.fetch(username, AccountSession.new).ssoid
19
+ end
20
+
21
+ def expire(username)
22
+ sessions.delete username
23
+ end
24
+
25
+ def clear
26
+ @sessions = {}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Caching
4
+ module Helper
5
+ def createsig(body)
6
+ Digest::MD5.hexdigest sigflat(body)
7
+ end
8
+
9
+ def sigflat(body)
10
+ res = nil
11
+ if body.class == Hash
12
+ arr = []
13
+ body.each do |key, value|
14
+ arr << "#{sigflat key}=>#{sigflat value}"
15
+ end
16
+ res = arr
17
+ end
18
+ if body.class == Array
19
+ str = ''
20
+ res = body.map do |value|
21
+ sigflat value
22
+ end.sort!.each do |value|
23
+ str << value
24
+ end
25
+ end
26
+ if body.class != String
27
+ res = body.to_s << body.class.to_s
28
+ end
29
+ res || body
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,57 @@
1
+ require 'ostruct'
2
+ require 'json'
3
+
4
+ module BetfairApiNgRails
5
+ module Api
6
+ module Caching
7
+ class ResponseCache
8
+ include Api::Caching::Helper
9
+ attr_reader :method, :sig_params
10
+
11
+ def initialize(method, params)
12
+ @method = method
13
+ @sig_params = createsig(params)
14
+ end
15
+
16
+ def self.redis_connection
17
+ @_redis_connection ||= Redis.new
18
+ end
19
+
20
+ def is_cached?
21
+ !cache_result.nil?
22
+ end
23
+
24
+ def responser
25
+ Api::Http::Responser.new prepare_response
26
+ end
27
+
28
+ def cache!(response)
29
+ cache_adapter.set cache_key, JSON.dump(response)
30
+ cache_adapter.expire cache_key, expire_time
31
+ end
32
+
33
+ private
34
+
35
+ def cache_result
36
+ @_cache_result ||= cache_adapter.get cache_key
37
+ end
38
+
39
+ def prepare_response
40
+ OpenStruct.new body: cache_result, code: '200'
41
+ end
42
+
43
+ def expire_time
44
+ BetfairApiNgRails.config.cache_expire
45
+ end
46
+
47
+ def cache_adapter
48
+ self.class.redis_connection
49
+ end
50
+
51
+ def cache_key
52
+ "betfair_api_ng_rails:#{method}:#{sig_params}"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -4,7 +4,6 @@ module BetfairApiNgRails
4
4
  module Api
5
5
  module Concerns
6
6
  module Errorable
7
-
8
7
  def has_errors?
9
8
  !error_info.nil?
10
9
  end
@@ -12,8 +11,7 @@ module BetfairApiNgRails
12
11
  def error_info
13
12
  @http_responser.try(:error_info?)
14
13
  end
15
-
16
14
  end
17
15
  end
18
16
  end
19
- end
17
+ end
@@ -1,27 +1,39 @@
1
1
  module BetfairApiNgRails
2
2
  module Api
3
3
  class Config
4
-
5
4
  class << self
6
-
7
5
  attr_accessor :application_key,
8
6
  :ssl_key_filepath,
9
7
  :ssl_crt_filepath,
10
- :login_url,
11
- :api_url,
12
8
  :username,
13
9
  :password,
14
10
  :locale,
15
11
  :formatter,
16
12
  :keep_alive_session,
17
- :keep_alive_url
13
+ :use_cache,
14
+ :cache_expire,
15
+ :proxy_url,
16
+ :proxy_enable,
17
+ :go_localuser,
18
+ :go_filename,
19
+ :endpoint
18
20
 
19
21
  def formatter=(value)
20
- @formatter = BetfairApiNgRails::Api::FormatterFactory.initialize_formatter value
22
+ @formatter = Api::FormatterFactory.initialize_formatter value
21
23
  end
22
24
 
23
- end
25
+ def endpoint=(value)
26
+ @endpoint = Api::EndpointFactory.call value
27
+ end
24
28
 
29
+ def load_for_environment(file_path, env)
30
+ require 'yaml'
31
+ load_config = YAML.load_file(file_path)[env]
32
+ Api::Constants::LOADABLE_CONFIG_OPTIONS.each do |opt|
33
+ send("#{opt}=", load_config[opt.to_s])
34
+ end
35
+ end
36
+ end
25
37
  end
26
38
  end
27
- end
39
+ end
@@ -1,37 +1,55 @@
1
+ require 'active_support/core_ext/module/delegation'
1
2
  require "betfair_api_ng_rails/errors"
2
3
 
3
4
  module BetfairApiNgRails
4
5
  module Api
5
6
  class Connection
6
-
7
- attr_reader :ssoid
7
+ attr_reader :account_name, :endpoint, :session_manager
8
8
 
9
- def initialize
10
- request_ssoid
9
+ delegate :app_key, :username, to: :account
10
+
11
+ def initialize(account_name)
12
+ BetfairApiNgRails.log.write("==> Initializing new connection #{self}")
13
+ @account_name = account_name
14
+ @endpoint = BetfairApiNgRails.config.endpoint
15
+ @session_manager = Api::SessionManager.new(endpoint)
11
16
  end
12
17
 
13
18
  def request(method, params = {})
14
- provider.fetch method: method, params: hashing(params)
19
+ provider.fetch method: Api::RequestMethod.new(method, endpoint), params: hashing(params)
15
20
  end
16
21
 
22
+ include Api::ConnectionExt::Caching
23
+ include Api::ConnectionExt::Logging
17
24
  include Api::ConnectionExt::ErrorHandling
25
+ include Api::ConnectionExt::SsoidRefreshing
18
26
  include Api::ConnectionExt::Parsing
19
27
  include Api::ConnectionExt::Formatting
20
28
 
21
- protected
29
+ protected
30
+
31
+ def expire_provider
32
+ session_manager.expire_ssoid account
33
+ end
22
34
 
23
35
  def provider
24
- @_provider ||= BetfairApiNgRails::Api::Provider.new ssoid
36
+ Api::Provider.new request_ssoid, app_key
25
37
  end
26
38
 
27
39
  def request_ssoid
28
- @ssoid = BetfairApiNgRails::Api::SessionManager.new_ssoid
40
+ session_manager.get_ssoid account
29
41
  end
30
42
 
31
43
  def hashing(params)
32
- BetfairApiNgRails::Api::Hashalator.new(params).to_hash
44
+ Api::Hashalator.new(params).to_hash
33
45
  end
34
46
 
47
+ def account
48
+ acc = BetfairApiNgRails.account_manager.get account_name
49
+ raise BetfairApiNgRails::NoAccountProvided unless acc
50
+ BetfairApiNgRails.log.write("==> Using account #{acc.username}")
51
+ acc
52
+ end
35
53
  end
36
54
  end
37
- end
55
+ end
@@ -0,0 +1,60 @@
1
+ require 'active_support/inflector'
2
+
3
+ module BetfairApiNgRails
4
+ module Api
5
+ module ConnectionExt
6
+ module Caching
7
+ include BetfairApiNgRails::Api::Constants
8
+
9
+ def self.included(base)
10
+ base.send :class_eval, <<-CODE
11
+ def request_with_caching(method, params = {})
12
+ if is_caching_on?
13
+ BetfairApiNgRails.log.write("Cache is on! Trying to process with cache...")
14
+ process_with_caching method, params
15
+ else
16
+ BetfairApiNgRails.log.write("Cache is off!")
17
+ return request_without_caching(method, params)
18
+ end
19
+ end
20
+ alias_method :request_without_caching, :request
21
+ alias_method :request, :request_with_caching
22
+ CODE
23
+ end
24
+
25
+ private
26
+
27
+ def is_caching_on?
28
+ BetfairApiNgRails.config.use_cache
29
+ end
30
+
31
+ def process_with_caching(method, params)
32
+ cache_service = Api::Caching::ResponseCache.new(method, hashing(params))
33
+ if cache_service.is_cached?
34
+ BetfairApiNgRails.log.write(" Finded cached record!")
35
+ return cache_service.responser
36
+ else
37
+ BetfairApiNgRails.log.write(" Cached record not found!")
38
+ return make_real_request(method, params, cache_service)
39
+ end
40
+ end
41
+
42
+ def make_real_request(method, params, cache_service)
43
+ res = request_without_caching(method, params)
44
+ if need_caching?(res)
45
+ cache_service.cache!(res.result)
46
+ BetfairApiNgRails.log.write(" Result was cached!")
47
+ else
48
+ BetfairApiNgRails.log.write(" There are some problems 0_o... No any response cached...")
49
+ end
50
+ res
51
+ end
52
+
53
+ def need_caching?(res)
54
+ !(res.nil? || res.has_error?)
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
@@ -11,7 +11,7 @@ module BetfairApiNgRails
11
11
  base.send :class_eval, <<-CODE
12
12
  def request_with_error_handling(method, params = {})
13
13
  res = request_without_error_handling(method, params)
14
- raise_exception(res.error_info) if res.has_error?
14
+ raise_exception(res.try(:error_info) || default_error_info) if res.nil? || res.has_error?
15
15
  res
16
16
  end
17
17
  alias_method :request_without_error_handling, :request
@@ -25,6 +25,10 @@ module BetfairApiNgRails
25
25
  raise BetfairApiNgRails::APINGException.new(error), "Error in #{error[:type]} section"
26
26
  end
27
27
 
28
+ def default_error_info
29
+ {type: :SESSION, info: { 'code' => -32099 }}
30
+ end
31
+
28
32
  end
29
33
  end
30
34
  end
@@ -0,0 +1,34 @@
1
+ require 'active_support/inflector'
2
+
3
+ module BetfairApiNgRails
4
+ module Api
5
+ module ConnectionExt
6
+ module Logging
7
+ include BetfairApiNgRails::Api::Constants
8
+
9
+ def self.included(base)
10
+ base.send :class_eval, <<-CODE
11
+ def request_with_logging(method, params = {})
12
+ write_income_request method, params
13
+ res = request_without_logging(method, params)
14
+ write_result(res)
15
+ res
16
+ end
17
+ alias_method :request_without_logging, :request
18
+ alias_method :request, :request_with_logging
19
+ CODE
20
+ end
21
+
22
+ private
23
+
24
+ def write_income_request(method, params)
25
+ BetfairApiNgRails.log.write("--->Income request for #{method} with parameters #{hashing(params)}")
26
+ end
27
+
28
+ def write_result(res)
29
+ BetfairApiNgRails.log.write(" Result of request: #{res.result}")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,7 +4,6 @@ module BetfairApiNgRails
4
4
  module Api
5
5
  module ConnectionExt
6
6
  module Parsing
7
-
8
7
  def self.included(base)
9
8
  base.send :class_eval, <<-CODE
10
9
  def request_with_parsing(method, params = {})
@@ -16,13 +15,12 @@ module BetfairApiNgRails
16
15
  CODE
17
16
  end
18
17
 
19
- private
18
+ private
20
19
 
21
20
  def init_parser(method, responser)
22
21
  "BetfairApiNgRails::Api::Parsers::#{method.camelize}".constantize.new responser
23
22
  end
24
-
25
23
  end
26
24
  end
27
25
  end
28
- end
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/inflector'
2
+ require "betfair_api_ng_rails/errors"
3
+
4
+ module BetfairApiNgRails
5
+ module Api
6
+ module ConnectionExt
7
+ module SsoidRefreshing
8
+ include BetfairApiNgRails::Api::Constants
9
+
10
+ def self.included(base)
11
+ base.send :class_eval, <<-CODE
12
+ def request_with_ssoid_refreshing(method, params = {})
13
+ res = nil
14
+ begin
15
+ res = request_without_ssoid_refreshing(method, params)
16
+ rescue BetfairApiNgRails::APINGException => e
17
+ raise e unless is_ssoid_problem?(e)
18
+ try_request method, params
19
+ end
20
+ res
21
+ end
22
+ alias_method :request_without_ssoid_refreshing, :request
23
+ alias_method :request, :request_with_ssoid_refreshing
24
+ CODE
25
+ end
26
+
27
+ private
28
+
29
+ def is_ssoid_problem?(e)
30
+ e.info[:info]["code"].to_i == -32099
31
+ end
32
+
33
+ def try_request(method, params)
34
+ expire_provider
35
+ request_without_ssoid_refreshing(method, params)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,11 +1,8 @@
1
1
  module BetfairApiNgRails
2
2
  module Api
3
3
  module Constants
4
-
5
4
  SUCCESS_LOGIN = "SUCCESS"
6
5
 
7
- PROVIDER_NAME = "BF"
8
-
9
6
  SESSION_REQUEST_HEADERS = {
10
7
  "Content-Type" => "application/x-www-form-urlencoded"
11
8
  }
@@ -15,20 +12,28 @@ module BetfairApiNgRails
15
12
  "Accept" => "application/json"
16
13
  }
17
14
 
18
- ALLOWED_RESOURCES = [
19
- "listEventTypes",
20
- "listCompetitions",
21
- "listTimeRanges",
22
- "listEvents",
23
- "listMarketTypes",
24
- "listCountries",
25
- "listVenues",
26
- "listMarketCatalogue",
27
- "listMarketBook",
28
- "listCurrentOrders",
29
- "listClearedOrders",
30
- "listMarketProfitAndLoss"
31
- ]
15
+ ALLOWED_RESOURCES = {
16
+ betting:[
17
+ "listEventTypes",
18
+ "listCompetitions",
19
+ "listTimeRanges",
20
+ "listEvents",
21
+ "listMarketTypes",
22
+ "listCountries",
23
+ "listVenues",
24
+ "listMarketCatalogue",
25
+ "listMarketBook",
26
+ "listCurrentOrders",
27
+ "listClearedOrders",
28
+ "listMarketProfitAndLoss",
29
+ "placeOrders"
30
+ ],
31
+ account:[
32
+ "getAccountDetails",
33
+ "getAccountFunds",
34
+ "getAccountStatement"
35
+ ]
36
+ }
32
37
 
33
38
  ALLOWED_FORMATTING = [
34
39
  "listEventTypes",
@@ -46,6 +51,22 @@ module BetfairApiNgRails
46
51
  "listVenues"
47
52
  ]
48
53
 
54
+ LOADABLE_CONFIG_OPTIONS = [
55
+ :application_key,
56
+ :username,
57
+ :password,
58
+ :go_localuser,
59
+ :go_filename
60
+ ]
61
+
62
+ JSON_METHOD = {
63
+ betting: 'SportsAPING',
64
+ account: 'AccountAPING'
65
+ }
66
+
67
+ # URLs
68
+
69
+ KEEP_ALIVE_URL = "https://identitysso.betfair.com/api/keepAlive"
49
70
  end
50
71
  end
51
- end
72
+ end