betfair-ng 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c48417f7d98fba1d9f5e7921baf1eb516f2bd694
4
+ data.tar.gz: 8ae3add7897ff82188598128aef98ce3b79b1937
5
+ SHA512:
6
+ metadata.gz: f20679c2a6718b30830bb94a666401f8ad7f2ef15943d5d2dd8a2a4f8711dc2a342763388a8d976a139c4c77516faab870fd6c8272a3c4125caffa42d42fcf59
7
+ data.tar.gz: 9695f1eada33d762ace02ae1e3060265b6b66806249dcab1462428f3bd56fefa4cb42570e3f5511e50650b25360b1e71847bfd138493a4309c9c595c18657893
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in betfair.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mike Campbell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,96 @@
1
+ # Betfair
2
+
3
+ A lightweight ruby wrapper for the Betfair Exchange API (API-NG).
4
+
5
+ Full API description, including API parameters etc, is available from [Betfair's dedicated API site](https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/API-NG+Overview).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'betfair', github: 'mikecmpbll/betfair'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # create a client with app code
23
+ client = Betfair::Client.new("X-Application" => "your_app_code")
24
+
25
+ # let's log in.
26
+ client.interactive_login("your_username", "your_password")
27
+
28
+ # you can do stuff like list event types:
29
+ event_types = client.list_event_types(filter: {})
30
+ # => [
31
+ # {
32
+ # "eventType"=>{
33
+ # "id"=>"7",
34
+ # "name"=>"Horse Racing"
35
+ # },
36
+ # "marketCount"=>215
37
+ # },
38
+ # ..etc..
39
+ # ]
40
+
41
+ # todays GB & Ireland horse racing win & place markets
42
+ racing_et_id = event_types.find{|et| et["eventType"]["name"] == "Horse Racing"}["eventType"]["id"]
43
+
44
+ racing_markets = client.list_market_catalogue({
45
+ filter: {
46
+ eventTypeIds: [racing_et_id],
47
+ marketTypeCodes: ["WIN", "PLACE"],
48
+ marketStartTime: {
49
+ from: Time.now.beginning_of_day.iso8601,
50
+ to: Time.now.end_of_day.iso8601
51
+ },
52
+ marketCountries: ["GB", "IRE"]
53
+ },
54
+ maxResults: 200,
55
+ marketProjection: [
56
+ "MARKET_START_TIME",
57
+ "RUNNER_METADATA",
58
+ "RUNNER_DESCRIPTION",
59
+ "EVENT_TYPE",
60
+ "EVENT",
61
+ "COMPETITION"
62
+ ]
63
+ })
64
+
65
+ # given an eventId from the market catalogue (the first for example),
66
+ # let's have a flutter shall we?
67
+ market = racing_markets.first
68
+ market_id = market["marketId"]
69
+ selection_id = market["runners"].find { |r| r["runnerName"] == "Imperial Commander" }["selectionId"]
70
+
71
+ # this places an Betfair SP bet with a price limit of 3.0 .
72
+ # see the API docs for the different types of orders.
73
+ client.place_orders({
74
+ marketId: market_id,
75
+ instructions: [{
76
+ orderType: "LIMIT_ON_CLOSE",
77
+ selectionId: selection_id,
78
+ side: "BACK",
79
+ limitOnCloseOrder: {
80
+ liability: liability,
81
+ price: 3.0
82
+ }
83
+ }]
84
+ })
85
+
86
+ # log back out.
87
+ client.logout
88
+ ```
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/mikecmpbll/betfair/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'betfair/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "betfair-ng"
8
+ spec.version = Betfair::VERSION
9
+ spec.authors = ["Mike Campbell"]
10
+ spec.email = ["mike@wordofmike.net"]
11
+ spec.summary = %q{A lightweight wrapper for the Betfair Exchange API-NG}
12
+ spec.homepage = "http://github.com/mikecmpbll/betfair"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency 'activesupport', '> 3.0.0'
24
+ spec.add_runtime_dependency 'httpi', '> 2.0.2'
25
+ end
@@ -0,0 +1,5 @@
1
+ require "betfair/version"
2
+ require "betfair/client"
3
+
4
+ module Betfair
5
+ end
@@ -0,0 +1,63 @@
1
+ require 'json'
2
+ require 'active_support/core_ext/string'
3
+
4
+ module Betfair
5
+ module API
6
+ module REST
7
+ def self.extended(obj)
8
+ obj.persistent_headers.merge!({
9
+ "Accept" => "application/json",
10
+ "Content-Type" => "application/json"
11
+ })
12
+
13
+ apis = {
14
+ betting: "https://api.betfair.com/exchange/betting/rest/v1.0",
15
+ account: "https://api.betfair.com/exchange/account/rest/v1.0"
16
+ }
17
+
18
+ obj.class::OPERATIONS.each do |api, operations|
19
+ operations.each do |operation|
20
+ define_method(operation) do |body = nil|
21
+ raise "Not signed in" unless ["X-Authentication", "X-Application"].all? { |k| persistent_headers.key?(k) }
22
+
23
+ post(url: "#{apis[api]}/#{operation.to_s.camelize(:lower)}/", body: body.to_json)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def interactive_login(username, password)
30
+ json = post({
31
+ url: "https://identitysso.betfair.com/api/login",
32
+ body: { username: username, password: password },
33
+ headers: { "Content-Type" => "application/x-www-form-urlencoded" }
34
+ })
35
+
36
+ session_token = json["token"]
37
+
38
+ persistent_headers.merge!({
39
+ "X-Authentication" => session_token
40
+ })
41
+ end
42
+
43
+ def logout
44
+ get(url: "https://identitysso.betfair.com/api/logout")
45
+ end
46
+
47
+ private
48
+ [:get, :post].each do |verb|
49
+ define_method(verb) do |*args|
50
+ response = super(*args)
51
+ parse_response(response)
52
+ end
53
+ end
54
+
55
+ def parse_response(response)
56
+ JSON.parse(response).tap { |r| handle_errors(r) }
57
+ end
58
+
59
+ def handle_errors(response)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,13 @@
1
+ module Betfair
2
+ module API
3
+ module RPC
4
+ def self.extended(obj)
5
+ obj.endpoint = "https://api.betfair.com/exchange/betting/json-rpc/v1"
6
+ obj.persistent_headers.merge!({
7
+ "Accept" => "application/json",
8
+ "Content-Type" => "application/json"
9
+ })
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ class Bet
2
+ end
@@ -0,0 +1,58 @@
1
+ require "betfair/api/rest"
2
+ require "betfair/api/rpc"
3
+ require "betfair/utils"
4
+ require "httpi"
5
+
6
+ module Betfair
7
+ class Client
8
+ include Utils
9
+
10
+ DEFAULT_SETTINGS = { retries: 1 }
11
+ OPERATIONS = {
12
+ betting: [:list_event_types, :list_events, :list_market_catalogue,
13
+ :list_market_book, :list_competitions, :list_market_profit_and_loss,
14
+ :list_countries, :list_current_orders, :list_cleared_orders,
15
+ :list_market_types, :list_time_ranges, :list_venues, :place_orders,
16
+ :cancel_orders, :replace_orders, :update_orders],
17
+ account: [:get_account_funds]
18
+ }
19
+
20
+ attr_accessor :settings, :persistent_headers
21
+
22
+ def initialize(headers = {}, api_type = :rest, settings = {})
23
+ @settings = DEFAULT_SETTINGS.merge(settings)
24
+ @persistent_headers = {}
25
+ @persistent_headers.merge!(headers)
26
+ extend_api(api_type)
27
+ end
28
+
29
+ private
30
+
31
+ [:get, :post].each do |verb|
32
+ define_method(verb) do |*args|
33
+ request = configure_request(*args)
34
+ response = attempt((settings[:retries] || 1).times) do
35
+ HTTPI.request(verb, request, settings[:adapter])
36
+ end
37
+ response.body
38
+ end
39
+ end
40
+
41
+ def configure_request(opts = {})
42
+ opts[:headers] = persistent_headers.merge(opts[:headers] || {})
43
+
44
+ HTTPI::Request.new(opts)
45
+ end
46
+
47
+ def extend_api(type)
48
+ case type
49
+ when :rest
50
+ extend API::REST
51
+ when :rpc
52
+ extend API::RPC
53
+ else
54
+ extend type
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,16 @@
1
+ module Betfair
2
+ module Utils
3
+ def attempt(enum)
4
+ exception = nil
5
+ enum.each do
6
+ begin
7
+ return yield
8
+ rescue Exception => e
9
+ exception = e
10
+ next
11
+ end
12
+ end
13
+ raise exception
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Betfair
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: betfair-ng
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: httpi
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.2
69
+ description:
70
+ email:
71
+ - mike@wordofmike.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - betfair.gemspec
82
+ - lib/betfair.rb
83
+ - lib/betfair/api/rest.rb
84
+ - lib/betfair/api/rpc.rb
85
+ - lib/betfair/bet.rb
86
+ - lib/betfair/client.rb
87
+ - lib/betfair/utils.rb
88
+ - lib/betfair/version.rb
89
+ homepage: http://github.com/mikecmpbll/betfair
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A lightweight wrapper for the Betfair Exchange API-NG
113
+ test_files: []