betfair_api_ng_rails 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -10
  3. data/lib/betfair_api_ng_rails/api/base_provider.rb +1 -11
  4. data/lib/betfair_api_ng_rails/api/bf/config.rb +1 -2
  5. data/lib/betfair_api_ng_rails/api/bf/constants.rb +14 -4
  6. data/lib/betfair_api_ng_rails/api/bf/data/attributes.rb +52 -0
  7. data/lib/betfair_api_ng_rails/api/bf/data/base.rb +17 -0
  8. data/lib/betfair_api_ng_rails/api/bf/data/competition.rb +26 -0
  9. data/lib/betfair_api_ng_rails/api/bf/data/competition_result.rb +23 -0
  10. data/lib/betfair_api_ng_rails/api/bf/data/concerns/hashable.rb +40 -0
  11. data/lib/betfair_api_ng_rails/api/bf/data/concerns/querable.rb +46 -0
  12. data/lib/betfair_api_ng_rails/api/bf/data/constants.rb +13 -0
  13. data/lib/betfair_api_ng_rails/api/bf/data/event.rb +26 -0
  14. data/lib/betfair_api_ng_rails/api/bf/data/event_result.rb +21 -0
  15. data/lib/betfair_api_ng_rails/api/bf/data/event_type.rb +26 -0
  16. data/lib/betfair_api_ng_rails/api/bf/data/event_type_result.rb +21 -0
  17. data/lib/betfair_api_ng_rails/api/bf/data/market_filter.rb +19 -0
  18. data/lib/betfair_api_ng_rails/api/bf/data/time_range.rb +18 -0
  19. data/lib/betfair_api_ng_rails/api/bf/data/time_range_result.rb +21 -0
  20. data/lib/betfair_api_ng_rails/api/bf/enums/bet_status.rb +17 -0
  21. data/lib/betfair_api_ng_rails/api/bf/enums/concerns/enumable.rb +17 -0
  22. data/lib/betfair_api_ng_rails/api/bf/enums/execution_report_error_code.rb +30 -0
  23. data/lib/betfair_api_ng_rails/api/bf/enums/execution_report_status.rb +17 -0
  24. data/lib/betfair_api_ng_rails/api/bf/enums/group_by.rb +19 -0
  25. data/lib/betfair_api_ng_rails/api/bf/enums/instruction_report_error_code.rb +33 -0
  26. data/lib/betfair_api_ng_rails/api/bf/enums/instruction_report_status.rb +16 -0
  27. data/lib/betfair_api_ng_rails/api/bf/enums/market_betting_type.rb +19 -0
  28. data/lib/betfair_api_ng_rails/api/bf/enums/market_projection.rb +20 -0
  29. data/lib/betfair_api_ng_rails/api/bf/enums/market_sort.rb +19 -0
  30. data/lib/betfair_api_ng_rails/api/bf/enums/market_status.rb +17 -0
  31. data/lib/betfair_api_ng_rails/api/bf/enums/match_projection.rb +16 -0
  32. data/lib/betfair_api_ng_rails/api/bf/enums/order_by.rb +16 -0
  33. data/lib/betfair_api_ng_rails/api/bf/enums/order_projection.rb +16 -0
  34. data/lib/betfair_api_ng_rails/api/bf/enums/order_status.rb +15 -0
  35. data/lib/betfair_api_ng_rails/api/bf/enums/order_type.rb +16 -0
  36. data/lib/betfair_api_ng_rails/api/bf/enums/persistence_type.rb +16 -0
  37. data/lib/betfair_api_ng_rails/api/bf/enums/price_data.rb +18 -0
  38. data/lib/betfair_api_ng_rails/api/bf/enums/rollup_model.rb +17 -0
  39. data/lib/betfair_api_ng_rails/api/bf/enums/runner_status.rb +19 -0
  40. data/lib/betfair_api_ng_rails/api/bf/enums/side.rb +15 -0
  41. data/lib/betfair_api_ng_rails/api/bf/enums/sort_dir.rb +15 -0
  42. data/lib/betfair_api_ng_rails/api/bf/enums/time_granularity.rb +16 -0
  43. data/lib/betfair_api_ng_rails/api/bf/formatters/js_tree_formatter.rb +7 -11
  44. data/lib/betfair_api_ng_rails/api/bf/helper.rb +33 -0
  45. data/lib/betfair_api_ng_rails/api/bf/http/factory.rb +37 -0
  46. data/lib/betfair_api_ng_rails/api/bf/http/requester.rb +69 -0
  47. data/lib/betfair_api_ng_rails/api/bf/http/responser.rb +58 -0
  48. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/base.rb +33 -0
  49. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_competitions.rb +19 -0
  50. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_event_types.rb +19 -0
  51. data/lib/betfair_api_ng_rails/api/bf/orm/parsers/list_events.rb +19 -0
  52. data/lib/betfair_api_ng_rails/api/bf/orm/requester.rb +52 -0
  53. data/lib/betfair_api_ng_rails/api/bf/provider.rb +11 -28
  54. data/lib/betfair_api_ng_rails/api/bf/session_manager.rb +26 -37
  55. data/lib/betfair_api_ng_rails/version.rb +1 -1
  56. data/lib/betfair_api_ng_rails.rb +80 -9
  57. data/lib/generators/betfair_api_ng_rails/install/templates/betfair_api_ng_rails.rb +2 -3
  58. metadata +48 -9
  59. data/lib/betfair_api_ng_rails/api/base_formatter.rb +0 -11
  60. data/lib/betfair_api_ng_rails/api/base_parser.rb +0 -25
  61. data/lib/betfair_api_ng_rails/api/base_requester.rb +0 -25
  62. data/lib/betfair_api_ng_rails/api/bf/http_requester.rb +0 -73
  63. data/lib/betfair_api_ng_rails/api/bf/http_responser.rb +0 -44
  64. data/lib/betfair_api_ng_rails/api/bf/parser.rb +0 -31
  65. data/lib/betfair_api_ng_rails/api/soccer/requester.rb +0 -25
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class MarketBettingType
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "ODDS",
9
+ "LINE",
10
+ "RANGE",
11
+ "ASIAN_HANDICAP_DOUBLE_LINE",
12
+ "ASIAN_HANDICAP_SINGLE_LINE",
13
+ "FIXED_ODDS"
14
+
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class MarketProjection
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "COMPETITION",
9
+ "EVENT",
10
+ "EVENT_TYPE",
11
+ "MARKET_START_TIME",
12
+ "MARKET_DESCRIPTION",
13
+ "RUNNER_DESCRIPTION",
14
+ "RUNNER_METADATA"
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class MarketSort
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "MINIMUM_TRADED",
9
+ "MAXIMUM_TRADED",
10
+ "MINIMUM_AVAILABLE",
11
+ "MAXIMUM_AVAILABLE",
12
+ "FIRST_TO_START",
13
+ "LAST_TO_START"
14
+
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class MarketStatus
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "INACTIVE",
9
+ "OPEN",
10
+ "SUSPENDED",
11
+ "CLOSED"
12
+
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class MatchProjection
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "NO_ROLLUP",
9
+ "ROLLED_UP_BY_PRICE",
10
+ "ROLLED_UP_BY_AVG_PRICE"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class OrderBy
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "BY_BET",
9
+ "BY_MARKET",
10
+ "BY_MATCH_TIME"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class OrderProjection
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "ALL",
9
+ "EXECUTABLE",
10
+ "EXECUTION_COMPLETE"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class OrderStatus
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "EXECUTION_COMPLETE",
9
+ "EXECUTABLE"
10
+
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class OrderType
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "LIMIT",
9
+ "LIMIT_ON_CLOSE",
10
+ "MARKET_ON_CLOSE"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class PersistenceType
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "LAPSE",
9
+ "PERSIST",
10
+ "MARKET_ON_CLOSE"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class PriceData
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "SP_AVAILABLE",
9
+ "SP_TRADED",
10
+ "EX_BEST_OFFERS",
11
+ "EX_ALL_OFFERS",
12
+ "EX_TRADED"
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class RollupModel
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "STAKE",
9
+ "PAYOUT",
10
+ "MANAGED_LIABILITY",
11
+ "NONE"
12
+
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class RunnerStatus
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "ACTIVE",
9
+ "WINNER",
10
+ "LOSER",
11
+ "REMOVED_VACANT",
12
+ "REMOVED",
13
+ "HIDDEN"
14
+
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class Side
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "BACK",
9
+ "LAY"
10
+
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class SortDir
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "EARLIEST_TO_LATEST",
9
+ "LATEST_TO_EARLIEST"
10
+
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Enums
5
+ class TimeGranularity
6
+ extend Api::BF::Enums::Concerns::Enumable
7
+
8
+ enumify "DAYS",
9
+ "HOURS",
10
+ "MINUTES"
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -5,24 +5,20 @@ module BetfairApiNgRails
5
5
  module Api
6
6
  module BF
7
7
  module Formatters
8
- class JsTreeFormatter < BetfairApiNgRails::Api::BaseFormatter
9
-
10
- attr_accessor :allow_children
8
+ class JsTreeFormatter
11
9
 
12
10
  ALLOW_CHILDREN = [:event_types, :competitions]
13
11
 
14
- def initialize
15
- @allow_children = false
16
- end
17
-
18
- def format(record: {}, resource: "")
12
+ def format(record: nil, resource: "")
13
+ return {} unless record
14
+ obj = record.send resource.to_s.singularize
19
15
  {
20
- id: record['id'],
21
- text: record['name'],
16
+ id: obj.id,
17
+ text: obj.name,
22
18
  children: ALLOW_CHILDREN.include?(resource.to_sym),
23
19
  data:{
24
20
  type: resource.to_s.singularize,
25
- fields: record
21
+ fields: obj.to_hash
26
22
  }
27
23
  }
28
24
  end
@@ -0,0 +1,33 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Helper
5
+
6
+ def read_certificate_file(path)
7
+ OpenSSL::X509::Certificate.new read_file(path)
8
+ end
9
+
10
+ def read_rsa_file(path)
11
+ OpenSSL::PKey::RSA.new read_file(path)
12
+ end
13
+
14
+ def read_file(path)
15
+ File.read path
16
+ rescue
17
+ ""
18
+ end
19
+
20
+ def prepare_api_req_json(method, filter, params)
21
+ {
22
+ "jsonrpc" => "2.0",
23
+ "method" => "SportsAPING/v1.0/#{method}",
24
+ "params" => {
25
+ "filter" => filter
26
+ }.merge(params)
27
+ }.to_json
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module Http
5
+ class Factory
6
+ class << self
7
+ include Api::BF::Constants
8
+
9
+ def provider_requester(with_method: "", params: {})
10
+ create_http_requester(Api::BF::Config.api_url).tap do |req|
11
+ req.set_request_headers API_REQUEST_HEADERS
12
+ req.set_auth_headers Api::BF::Config.application_key, BetfairApiNgRails::Api::BF::SessionManager.ssoid
13
+ req.set_api_req_body with_method, params
14
+ end
15
+ end
16
+
17
+ def session_requester
18
+ create_http_requester(Api::BF::Config.login_url).tap do |req|
19
+ req.set_ssl_files Api::BF::Config.ssl_crt_filepath, Api::BF::Config.ssl_key_filepath
20
+ req.set_request_headers SESSION_REQUEST_HEADERS
21
+ req.set_auth_headers Api::BF::Config.application_key
22
+ req.set_form_data "username" => Api::BF::Config.username, "password" => Api::BF::Config.password
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def create_http_requester(url = "")
29
+ Api::BF::Http::Requester.new url
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,69 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require 'json'
4
+ require 'betfair_api_ng_rails/api/bf/helper'
5
+
6
+ module BetfairApiNgRails
7
+ module Api
8
+ module BF
9
+ module Http
10
+ class Requester
11
+ include BetfairApiNgRails::Api::BF::Helper
12
+
13
+ attr_reader :http, :request, :uri
14
+
15
+ def initialize(url, use_ssl = true)
16
+ @uri = URI.parse url
17
+ @http = Net::HTTP.new uri.host, uri.port
18
+ http.use_ssl = use_ssl
19
+ @request = Net::HTTP::Post.new uri.request_uri
20
+ end
21
+
22
+ def do_request
23
+ Api::BF::Http::Responser.new http.request(request)
24
+ end
25
+
26
+ def set_crt_file(path)
27
+ http.cert = read_certificate_file(path)
28
+ end
29
+
30
+ def set_key_file(path)
31
+ http.key = read_rsa_file(path)
32
+ end
33
+
34
+ def set_verify_mode(value)
35
+ http.verify_mode = value
36
+ end
37
+
38
+ def set_ssl_files(crt_path, key_path, verify = OpenSSL::SSL::VERIFY_PEER)
39
+ set_crt_file crt_path
40
+ set_key_file key_path
41
+ set_verify_mode verify
42
+ end
43
+
44
+ def set_auth_headers(app_key, session_key = nil)
45
+ request["X-Application"] = app_key
46
+ request["X-Authentication"] = session_key if session_key
47
+ end
48
+
49
+ def set_request_headers(headers)
50
+ headers.each { |name, value| request[name] = value }
51
+ end
52
+
53
+ def set_form_data(values)
54
+ request.set_form_data values
55
+ end
56
+
57
+ def set_request_body(body)
58
+ request.body = body
59
+ end
60
+
61
+ def set_api_req_body(method, filter = {}, params = {})
62
+ set_request_body prepare_api_req_json(method, filter, params)
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+
3
+ module BetfairApiNgRails
4
+ module Api
5
+ module BF
6
+ module Http
7
+ class Responser
8
+ include Api::BF::Constants
9
+
10
+ attr_reader :response, :error_info
11
+
12
+ def initialize(response)
13
+ @response = response
14
+ end
15
+
16
+ def result
17
+ JSON.parse response.body
18
+ end
19
+
20
+ def has_error?
21
+ http_error? || api_req_error? || session_req_error?
22
+ end
23
+
24
+ private
25
+
26
+ def http_error?
27
+ set_error_info(:HTTP, response.code) if check_response_code
28
+ end
29
+
30
+ def session_req_error?
31
+ set_error_info(:SESSION, result['loginStatus']) if check_login_status
32
+ end
33
+
34
+ def api_req_error?
35
+ set_error_info(:API, result['error']) if check_error_key
36
+ end
37
+
38
+ def check_response_code
39
+ response.code != '200'
40
+ end
41
+
42
+ def check_login_status
43
+ result.fetch('loginStatus', SUCCESS_LOGIN) != SUCCESS_LOGIN
44
+ end
45
+
46
+ def check_error_key
47
+ result.has_key?('error')
48
+ end
49
+
50
+ def set_error_info(type, info)
51
+ @error_info = { type: type, info: info }
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module ORM
5
+ module Parsers
6
+ class Base
7
+
8
+ attr_reader :responser
9
+
10
+ def initialize(responser)
11
+ @responser = responser
12
+ end
13
+
14
+ def process
15
+ fetch_result_json.map { |json_row| process_row json_row }
16
+ end
17
+
18
+ private
19
+
20
+ def fetch_result_json
21
+ responser.has_error? ? [] : responser.result["result"]
22
+ end
23
+
24
+ def process_row(json_row)
25
+ raise 'Must be reimplemented!'
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module ORM
5
+ module Parsers
6
+ class ListCompetitions < Api::BF::ORM::Parsers::Base
7
+
8
+ private
9
+
10
+ def process_row(json_row)
11
+ Api::BF::Data::CompetitionResult.from_json json_row
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module ORM
5
+ module Parsers
6
+ class ListEventTypes < Api::BF::ORM::Parsers::Base
7
+
8
+ private
9
+
10
+ def process_row(json_row)
11
+ Api::BF::Data::EventTypeResult.from_json json_row
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module BF
4
+ module ORM
5
+ module Parsers
6
+ class ListEvents < Api::BF::ORM::Parsers::Base
7
+
8
+ private
9
+
10
+ def process_row(json_row)
11
+ Api::BF::Data::EventResult.from_json json_row
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_support/inflector'
2
+
3
+ module BetfairApiNgRails
4
+ module Api
5
+ module BF
6
+ module ORM
7
+ class Requester
8
+ class << self
9
+
10
+ def load(resource: "", filter: Api::BF::Data::MarketFilter.new, params: {})
11
+ res = current_provider.fetch method: build_function(resource), filter: filter, params: set_locale(params)
12
+ process_response resource, res
13
+ end
14
+
15
+ private
16
+
17
+ def process_response(resource, res)
18
+ r = parse_response(build_function(resource), res)
19
+ format_response(resource, r)
20
+ end
21
+
22
+ def parse_response(function, res)
23
+ build_parser(function, res).process
24
+ end
25
+
26
+ def format_response(resource, res)
27
+ return res unless BetfairApiNgRails.config.formatter
28
+ res.map { |obj| BetfairApiNgRails.config.formatter.format(record: obj, resource: resource) }
29
+ end
30
+
31
+ def set_locale(params)
32
+ params.merge locale: BetfairApiNgRails.config.locale
33
+ end
34
+
35
+ def build_function(method)
36
+ "list#{method.to_s.camelize}"
37
+ end
38
+
39
+ def current_provider
40
+ @_current_provider ||= BetfairApiNgRails::Api::BF::Provider.new
41
+ end
42
+
43
+ def build_parser(method, responser)
44
+ "BetfairApiNgRails::Api::BF::ORM::Parsers::#{method.to_s.camelize}".constantize.new responser
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end