betfair_api_ng_rails 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -1
  3. data/Rakefile +8 -8
  4. data/lib/betfair_api_ng_rails/api/connection.rb +3 -0
  5. data/lib/betfair_api_ng_rails/api/connection_ext/error_handling.rb +31 -0
  6. data/lib/betfair_api_ng_rails/api/connection_ext/formatting.rb +0 -1
  7. data/lib/betfair_api_ng_rails/api/connection_ext/parsing.rb +0 -1
  8. data/lib/betfair_api_ng_rails/api/data/base.rb +2 -1
  9. data/lib/betfair_api_ng_rails/api/data/competition.rb +7 -6
  10. data/lib/betfair_api_ng_rails/api/data/competition_result.rb +6 -10
  11. data/lib/betfair_api_ng_rails/api/data/concerns/attributable.rb +51 -0
  12. data/lib/betfair_api_ng_rails/api/data/concerns/hashable.rb +7 -2
  13. data/lib/betfair_api_ng_rails/api/data/concerns/jsonable.rb +46 -0
  14. data/lib/betfair_api_ng_rails/api/data/constants.rb +1 -1
  15. data/lib/betfair_api_ng_rails/api/data/country_code_result.rb +5 -8
  16. data/lib/betfair_api_ng_rails/api/data/event.rb +9 -4
  17. data/lib/betfair_api_ng_rails/api/data/event_result.rb +5 -8
  18. data/lib/betfair_api_ng_rails/api/data/event_type.rb +5 -4
  19. data/lib/betfair_api_ng_rails/api/data/event_type_result.rb +5 -8
  20. data/lib/betfair_api_ng_rails/api/data/ex_best_offers_overrides.rb +24 -0
  21. data/lib/betfair_api_ng_rails/api/data/exchange_prices.rb +16 -0
  22. data/lib/betfair_api_ng_rails/api/data/market_book.rb +31 -0
  23. data/lib/betfair_api_ng_rails/api/data/market_catalogue.rb +23 -0
  24. data/lib/betfair_api_ng_rails/api/data/market_description.rb +29 -0
  25. data/lib/betfair_api_ng_rails/api/data/market_filter.rb +18 -1
  26. data/lib/betfair_api_ng_rails/api/data/market_type_result.rb +5 -8
  27. data/lib/betfair_api_ng_rails/api/data/match.rb +19 -0
  28. data/lib/betfair_api_ng_rails/api/data/order.rb +29 -0
  29. data/lib/betfair_api_ng_rails/api/data/price_projection.rb +23 -0
  30. data/lib/betfair_api_ng_rails/api/data/price_size.rb +15 -0
  31. data/lib/betfair_api_ng_rails/api/data/runner.rb +24 -0
  32. data/lib/betfair_api_ng_rails/api/data/runner_catalog.rb +19 -0
  33. data/lib/betfair_api_ng_rails/api/data/starting_prices.rb +18 -0
  34. data/lib/betfair_api_ng_rails/api/data/time_range.rb +5 -4
  35. data/lib/betfair_api_ng_rails/api/data/time_range_result.rb +5 -8
  36. data/lib/betfair_api_ng_rails/api/data/venue_result.rb +5 -8
  37. data/lib/betfair_api_ng_rails/api/hashalator.rb +2 -1
  38. data/lib/betfair_api_ng_rails/api/parsers/base.rb +0 -2
  39. data/lib/betfair_api_ng_rails/api/parsers/list_market_book.rb +15 -0
  40. data/lib/betfair_api_ng_rails/api/parsers/list_market_catalogue.rb +15 -0
  41. data/lib/betfair_api_ng_rails/api/request_methods.rb +9 -1
  42. data/lib/betfair_api_ng_rails/errors.rb +7 -0
  43. data/lib/betfair_api_ng_rails/version.rb +1 -1
  44. data/lib/betfair_api_ng_rails.rb +41 -25
  45. data/spec/betfair_api_ng_rails/api/connection_spec.rb +3 -1
  46. data/spec/betfair_api_ng_rails/api/data/competition_result_spec.rb +15 -0
  47. data/spec/betfair_api_ng_rails/api/data/competition_spec.rb +7 -0
  48. data/spec/betfair_api_ng_rails/api/data/concerns/attributable_spec.rb +156 -0
  49. data/spec/betfair_api_ng_rails/api/data/concerns/hashable_spec.rb +44 -0
  50. data/spec/betfair_api_ng_rails/api/data/concerns/jsonable_spec.rb +170 -0
  51. data/spec/betfair_api_ng_rails/api/data/country_code_result_spec.rb +7 -0
  52. data/spec/betfair_api_ng_rails/api/data/event_result_spec.rb +16 -0
  53. data/spec/betfair_api_ng_rails/api/data/event_spec.rb +7 -0
  54. data/spec/betfair_api_ng_rails/api/data/event_type_result_spec.rb +16 -0
  55. data/spec/betfair_api_ng_rails/api/data/event_type_spec.rb +7 -0
  56. data/spec/betfair_api_ng_rails/api/data/ex_best_offers_overrides_spec.rb +7 -0
  57. data/spec/betfair_api_ng_rails/api/data/exchange_prices_spec.rb +15 -0
  58. data/spec/betfair_api_ng_rails/api/data/market_book_spec.rb +15 -0
  59. data/spec/betfair_api_ng_rails/api/data/market_catalogue_spec.rb +39 -0
  60. data/spec/betfair_api_ng_rails/api/data/market_description_spec.rb +7 -0
  61. data/spec/betfair_api_ng_rails/api/data/market_filter_spec.rb +7 -0
  62. data/spec/betfair_api_ng_rails/api/data/market_type_result_spec.rb +7 -0
  63. data/spec/betfair_api_ng_rails/api/data/match_spec.rb +7 -0
  64. data/spec/betfair_api_ng_rails/api/data/order_spec.rb +7 -0
  65. data/spec/betfair_api_ng_rails/api/data/price_projection_spec.rb +15 -0
  66. data/spec/betfair_api_ng_rails/api/data/price_size_spec.rb +7 -0
  67. data/spec/betfair_api_ng_rails/api/data/runner_catalog_spec.rb +7 -0
  68. data/spec/betfair_api_ng_rails/api/data/runner_spec.rb +33 -0
  69. data/spec/betfair_api_ng_rails/api/data/starting_prices_spec.rb +7 -0
  70. data/spec/betfair_api_ng_rails/api/data/time_range_result_spec.rb +17 -0
  71. data/spec/betfair_api_ng_rails/api/data/time_range_spec.rb +7 -0
  72. data/spec/betfair_api_ng_rails/api/data/venue_result_spec.rb +7 -0
  73. data/spec/betfair_api_ng_rails/api/hashalator_spec.rb +34 -4
  74. data/spec/betfair_api_ng_rails/api/request_methods_spec.rb +20 -0
  75. data/spec/betfair_api_ng_rails/integration/list_market_book_spec.rb +19 -0
  76. data/spec/betfair_api_ng_rails/integration/list_market_catalogue_spec.rb +36 -0
  77. data/spec/support/base_yaml_loader.rb +21 -0
  78. data/spec/support/data/models.yml +205 -0
  79. data/spec/support/data_models_yaml_loader.rb +7 -0
  80. data/spec/support/helper.rb +9 -0
  81. data/spec/support/shared_examples/data_model.rb +35 -0
  82. data/spec/support/shared_examples/simple_list_filtering_request.rb +3 -1
  83. metadata +100 -18
  84. data/lib/betfair_api_ng_rails/api/data/attributes.rb +0 -50
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Data
4
+ class PriceSize < Api::Data::Base
5
+
6
+ PRICE_SIZE_ATTRS = [
7
+ :price,
8
+ :size
9
+ ]
10
+
11
+ attributes PRICE_SIZE_ATTRS
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Data
4
+ class Runner < Api::Data::Base
5
+
6
+ RUNNER_ATTRS = [
7
+ :selection_id,
8
+ :handicap,
9
+ :status,
10
+ :adjustment_factor,
11
+ :last_price_traded,
12
+ :total_matched,
13
+ :removal_date,
14
+ {sp: { type: BetfairApiNgRails::StartingPrices }},
15
+ {ex: { type: BetfairApiNgRails::ExchangePrices }},
16
+ {orders: { type: BetfairApiNgRails::Order, array: true }},
17
+ {matches: { type: BetfairApiNgRails::Match, array: true }}
18
+ ]
19
+
20
+ attributes RUNNER_ATTRS
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Data
4
+ class RunnerCatalog < Api::Data::Base
5
+
6
+ RUNNER_CATALOG_ATTRS = [
7
+ :selection_id,
8
+ :runner_name,
9
+ :handicap,
10
+ :sort_priority,
11
+ :metadata
12
+ ]
13
+
14
+ attributes RUNNER_CATALOG_ATTRS
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Data
4
+ class StartingPrices < Api::Data::Base
5
+
6
+ STARTING_PRICES_ATTRS = [
7
+ :near_price,
8
+ :far_price,
9
+ {back_stake_taken: { type: BetfairApiNgRails::PriceSize, array: true }},
10
+ {lay_liability_taken: { type: BetfairApiNgRails::PriceSize, array: true }},
11
+ :actual_s_p
12
+ ]
13
+
14
+ attributes STARTING_PRICES_ATTRS
15
+ end
16
+ end
17
+ end
18
+ end
@@ -4,11 +4,12 @@ module BetfairApiNgRails
4
4
  class TimeRange < Api::Data::Base
5
5
  include Api::Data::Concerns::Hashable
6
6
 
7
- attr_accessor *TIME_RANGE_ATTRS
7
+ TIME_RANGE_ATTRS = [
8
+ :from,
9
+ :to
10
+ ]
8
11
 
9
- def self.from_json(json_row)
10
- new TIME_RANGE_ATTRS.inject({}) { |h, a| h[a] = json_row[a.to_s.camelize(:lower)]; h }
11
- end
12
+ attributes TIME_RANGE_ATTRS
12
13
 
13
14
  def self.to_hash_attrs
14
15
  TIME_RANGE_ATTRS
@@ -3,15 +3,12 @@ module BetfairApiNgRails
3
3
  module Data
4
4
  class TimeRangeResult < Api::Data::Base
5
5
 
6
- attr_accessor :time_range,
7
- :market_count
6
+ TIME_RANGE_RESULT_ATTRS = [
7
+ {time_range: {type: BetfairApiNgRails::TimeRange}},
8
+ :market_count
9
+ ]
8
10
 
9
- def self.from_json(json_row)
10
- new(
11
- time_range: Api::Data::TimeRange.from_json(json_row['timeRange']),
12
- market_count: json_row['marketCount'],
13
- )
14
- end
11
+ attributes TIME_RANGE_RESULT_ATTRS
15
12
 
16
13
  end
17
14
  end
@@ -3,15 +3,12 @@ module BetfairApiNgRails
3
3
  module Data
4
4
  class VenueResult < Api::Data::Base
5
5
 
6
- attr_accessor :market_count,
7
- :venue
6
+ VENUE_RESULT_ATTRS = [
7
+ :market_count,
8
+ :venue
9
+ ]
8
10
 
9
- def self.from_json(json_row)
10
- new(
11
- venue: json_row['venue'],
12
- market_count: json_row['marketCount']
13
- )
14
- end
11
+ attributes VENUE_RESULT_ATTRS
15
12
 
16
13
  end
17
14
  end
@@ -11,7 +11,8 @@ module BetfairApiNgRails
11
11
 
12
12
  def to_hash
13
13
  hashable.inject({}) do |h, (k, v)|
14
- h[hash_key(k)] = hash_value(v)
14
+ value = hash_value(v)
15
+ h[hash_key(k)] = value if !value.nil? && value != ""
15
16
  h
16
17
  end
17
18
  end
@@ -1,5 +1,3 @@
1
- require 'pry-nav'
2
-
3
1
  module BetfairApiNgRails
4
2
  module Api
5
3
  module Parsers
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Parsers
4
+ class ListMarketBook < Api::Parsers::Base
5
+
6
+ private
7
+
8
+ def process_row(json_row)
9
+ BetfairApiNgRails::MarketBook.from_json json_row
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BetfairApiNgRails
2
+ module Api
3
+ module Parsers
4
+ class ListMarketCatalogue < Api::Parsers::Base
5
+
6
+ private
7
+
8
+ def process_row(json_row)
9
+ BetfairApiNgRails::MarketCatalogue.from_json json_row
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -8,12 +8,20 @@ module BetfairApiNgRails
8
8
 
9
9
  SIMPLE_LISTING_FILTERED.each do |method|
10
10
  eval <<-CODE
11
- def #{method.underscore}(filter: Api::Data::MarketFilter.new, locale: :en)
11
+ def #{method.underscore}(filter: Api::Data::MarketFilter.new, locale: BetfairApiNgRails.config.locale)
12
12
  run_request __method__, { filter: filter }, { locale: locale }
13
13
  end
14
14
  CODE
15
15
  end
16
16
 
17
+ def list_market_catalogue(filter: MarketFilter.new, market_projection: [], sort: "", max_results: '1', locale: BetfairApiNgRails.config.locale)
18
+ run_request __method__, { filter: filter, market_projection: market_projection, sort: sort, max_results: max_results }, { locale: locale }
19
+ end
20
+
21
+ def list_market_book(market_ids: [], price_projection: BetfairApiNgRails::PriceProjection.new, order_projection: '', match_projection: '', currency_code: 'USD', locale: BetfairApiNgRails.config.locale)
22
+ run_request __method__, { market_ids: market_ids, price_projection: price_projection, order_projection: order_projection, match_projection: match_projection }, { currency_code: currency_code, locale: locale }
23
+ end
24
+
17
25
  def place_orders(market_id: "", instructions: [], customer_ref: "")
18
26
  run_request __method__, { market_id: market_id, instructions: instructions, customer_ref: customer_ref }
19
27
  end
@@ -3,4 +3,11 @@ module BetfairApiNgRails
3
3
  class NoConnectionError < ::StandardError
4
4
  end
5
5
 
6
+ class APINGException < ::StandardError
7
+ attr_reader :info
8
+ def initialize(info)
9
+ @info = info
10
+ end
11
+ end
12
+
6
13
  end
@@ -1,3 +1,3 @@
1
1
  module BetfairApiNgRails
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -27,25 +27,38 @@ module BetfairApiNgRails
27
27
 
28
28
  module Data
29
29
 
30
- autoload :Base, 'betfair_api_ng_rails/api/data/base'
31
- autoload :Attributes, 'betfair_api_ng_rails/api/data/attributes'
32
- autoload :Constants, 'betfair_api_ng_rails/api/data/constants'
33
- autoload :Competition, 'betfair_api_ng_rails/api/data/competition'
34
- autoload :CompetitionResult, 'betfair_api_ng_rails/api/data/competition_result'
35
- autoload :MarketFilter, 'betfair_api_ng_rails/api/data/market_filter'
36
- autoload :TimeRange, 'betfair_api_ng_rails/api/data/time_range'
37
- autoload :TimeRangeResult, 'betfair_api_ng_rails/api/data/time_range_result'
38
- autoload :EventType, 'betfair_api_ng_rails/api/data/event_type'
39
- autoload :EventTypeResult, 'betfair_api_ng_rails/api/data/event_type_result'
40
- autoload :Event, 'betfair_api_ng_rails/api/data/event'
41
- autoload :EventResult, 'betfair_api_ng_rails/api/data/event_result'
42
- autoload :CountryCodeResult, 'betfair_api_ng_rails/api/data/country_code_result'
43
- autoload :VenueResult, 'betfair_api_ng_rails/api/data/venue_result'
44
- autoload :MarketTypeResult, 'betfair_api_ng_rails/api/data/market_type_result'
30
+ autoload :Base, 'betfair_api_ng_rails/api/data/base'
31
+ autoload :Constants, 'betfair_api_ng_rails/api/data/constants'
32
+ autoload :Competition, 'betfair_api_ng_rails/api/data/competition'
33
+ autoload :CompetitionResult, 'betfair_api_ng_rails/api/data/competition_result'
34
+ autoload :MarketFilter, 'betfair_api_ng_rails/api/data/market_filter'
35
+ autoload :TimeRange, 'betfair_api_ng_rails/api/data/time_range'
36
+ autoload :TimeRangeResult, 'betfair_api_ng_rails/api/data/time_range_result'
37
+ autoload :EventType, 'betfair_api_ng_rails/api/data/event_type'
38
+ autoload :EventTypeResult, 'betfair_api_ng_rails/api/data/event_type_result'
39
+ autoload :Event, 'betfair_api_ng_rails/api/data/event'
40
+ autoload :EventResult, 'betfair_api_ng_rails/api/data/event_result'
41
+ autoload :CountryCodeResult, 'betfair_api_ng_rails/api/data/country_code_result'
42
+ autoload :VenueResult, 'betfair_api_ng_rails/api/data/venue_result'
43
+ autoload :MarketTypeResult, 'betfair_api_ng_rails/api/data/market_type_result'
44
+ autoload :MarketCatalogue, 'betfair_api_ng_rails/api/data/market_catalogue'
45
+ autoload :MarketDescription, 'betfair_api_ng_rails/api/data/market_description'
46
+ autoload :RunnerCatalog, 'betfair_api_ng_rails/api/data/runner_catalog'
47
+ autoload :MarketBook, 'betfair_api_ng_rails/api/data/market_book'
48
+ autoload :Runner, 'betfair_api_ng_rails/api/data/runner'
49
+ autoload :Order, 'betfair_api_ng_rails/api/data/order'
50
+ autoload :Match, 'betfair_api_ng_rails/api/data/match'
51
+ autoload :StartingPrices, 'betfair_api_ng_rails/api/data/starting_prices'
52
+ autoload :ExchangePrices, 'betfair_api_ng_rails/api/data/exchange_prices'
53
+ autoload :PriceSize, 'betfair_api_ng_rails/api/data/price_size'
54
+ autoload :PriceProjection, 'betfair_api_ng_rails/api/data/price_projection'
55
+ autoload :ExBestOffersOverrides, 'betfair_api_ng_rails/api/data/ex_best_offers_overrides'
45
56
 
46
57
  module Concerns
47
58
 
48
59
  autoload :Hashable, 'betfair_api_ng_rails/api/data/concerns/hashable'
60
+ autoload :Attributable, 'betfair_api_ng_rails/api/data/concerns/attributable'
61
+ autoload :Jsonable, 'betfair_api_ng_rails/api/data/concerns/jsonable'
49
62
 
50
63
  end
51
64
 
@@ -94,21 +107,24 @@ module BetfairApiNgRails
94
107
 
95
108
  module Parsers
96
109
 
97
- autoload :Base, 'betfair_api_ng_rails/api/parsers/base'
98
- autoload :ListCompetitions, 'betfair_api_ng_rails/api/parsers/list_competitions'
99
- autoload :ListEventTypes, 'betfair_api_ng_rails/api/parsers/list_event_types'
100
- autoload :ListEvents, 'betfair_api_ng_rails/api/parsers/list_events'
101
- autoload :ListTimeRanges, 'betfair_api_ng_rails/api/parsers/list_time_ranges'
102
- autoload :ListCountries, 'betfair_api_ng_rails/api/parsers/list_countries'
103
- autoload :ListVenues, 'betfair_api_ng_rails/api/parsers/list_venues'
104
- autoload :ListMarketTypes, 'betfair_api_ng_rails/api/parsers/list_market_types'
110
+ autoload :Base, 'betfair_api_ng_rails/api/parsers/base'
111
+ autoload :ListCompetitions, 'betfair_api_ng_rails/api/parsers/list_competitions'
112
+ autoload :ListEventTypes, 'betfair_api_ng_rails/api/parsers/list_event_types'
113
+ autoload :ListEvents, 'betfair_api_ng_rails/api/parsers/list_events'
114
+ autoload :ListTimeRanges, 'betfair_api_ng_rails/api/parsers/list_time_ranges'
115
+ autoload :ListCountries, 'betfair_api_ng_rails/api/parsers/list_countries'
116
+ autoload :ListVenues, 'betfair_api_ng_rails/api/parsers/list_venues'
117
+ autoload :ListMarketTypes, 'betfair_api_ng_rails/api/parsers/list_market_types'
118
+ autoload :ListMarketCatalogue, 'betfair_api_ng_rails/api/parsers/list_market_catalogue'
119
+ autoload :ListMarketBook, 'betfair_api_ng_rails/api/parsers/list_market_book'
105
120
 
106
121
  end
107
122
 
108
123
  module ConnectionExt
109
124
 
110
- autoload :Parsing, 'betfair_api_ng_rails/api/connection_ext/parsing'
111
- autoload :Formatting, 'betfair_api_ng_rails/api/connection_ext/formatting'
125
+ autoload :ErrorHandling, 'betfair_api_ng_rails/api/connection_ext/error_handling'
126
+ autoload :Parsing, 'betfair_api_ng_rails/api/connection_ext/parsing'
127
+ autoload :Formatting, 'betfair_api_ng_rails/api/connection_ext/formatting'
112
128
 
113
129
  end
114
130
 
@@ -17,6 +17,7 @@ describe BetfairApiNgRails::Api::Connection do
17
17
 
18
18
  let(:provider) { double(:provider) }
19
19
  let(:parser) { double(:parser, process: true) }
20
+ let(:response) { double(:response) }
20
21
 
21
22
  before(:each) do
22
23
  expect_any_instance_of(described_class).to receive(:request_ssoid)
@@ -25,7 +26,8 @@ describe BetfairApiNgRails::Api::Connection do
25
26
 
26
27
  it "calls provider's fetch method" do
27
28
  expect(subject).to receive(:provider).and_return provider
28
- expect(provider).to receive(:fetch).with(method: 'method', params: {})
29
+ expect(provider).to receive(:fetch).with(method: 'method', params: {}).and_return response
30
+ expect(response).to receive(:has_error?).and_return false
29
31
  expect(subject).to receive(:hashing).with({}).and_return({})
30
32
  subject.request 'method'
31
33
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe BetfairApiNgRails::CompetitionResult do
4
+
5
+ include_examples 'data model', DataModelsYamlLoader.new.fetch('competitionResult') do
6
+
7
+ describe "competition association" do
8
+
9
+ its('competition.id') { is_expected.to eq '1' }
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe BetfairApiNgRails::Competition do
4
+
5
+ include_examples 'data model', DataModelsYamlLoader.new.fetch('competition')
6
+
7
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ class TestClass
4
+ include BetfairApiNgRails::Api::Data::Concerns::Attributable
5
+ end
6
+
7
+ describe BetfairApiNgRails::Api::Data::Concerns::Attributable do
8
+
9
+ describe "class method" do
10
+
11
+ subject { TestClass }
12
+
13
+ describe ".attributes" do
14
+
15
+ let(:attrib) { [:some_attr] }
16
+
17
+ it "defines all needed attr accessors" do
18
+ expect(TestClass).to receive(:define_class_attrs).with(attrib)
19
+ expect(TestClass).to receive(:define_attr_accessors)
20
+ TestClass.attributes attrib
21
+ end
22
+
23
+ end
24
+
25
+ describe ".define_class_attrs" do
26
+
27
+ before do
28
+ expect(TestClass).to receive(:process_attributes).and_return [:some_attr]
29
+ TestClass.send :define_class_attrs, [:some_attr]
30
+ end
31
+
32
+ its(:class_attrs) { is_expected.to include(:some_attr) }
33
+
34
+ end
35
+
36
+ describe ".fetch_attr_name" do
37
+
38
+ subject { TestClass.send :fetch_attr_name, attrib }
39
+
40
+ context 'when passed hash' do
41
+
42
+ let(:attrib) { {some_attr: {type: String}} }
43
+
44
+ context "returns first key name" do
45
+ it { is_expected.to eq :some_attr }
46
+ end
47
+
48
+ end
49
+
50
+ context 'when passed simple string' do
51
+
52
+ let(:attrib) { :some_attr1 }
53
+
54
+ context 'returns passed value' do
55
+ it { is_expected.to eq :some_attr1 }
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ describe ".fetch_attr_options" do
63
+
64
+ subject { TestClass.send :fetch_attr_options, attrib }
65
+
66
+ context 'when passed hash' do
67
+
68
+ let(:attrib) { {some_attr: {type: String}} }
69
+
70
+ context "returns default options merged with setted one" do
71
+ it { is_expected.to eq({type: String, array: false}) }
72
+ end
73
+
74
+ end
75
+
76
+ context 'when passed simple string' do
77
+
78
+ let(:attrib) { :some_attr1 }
79
+
80
+ context 'returns default options' do
81
+ it { is_expected.to eq({type: String, array: false}) }
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+
88
+ describe ".define_attr_accessors" do
89
+
90
+ before { expect(TestClass).to receive(:class_attrs).and_return({some_attr: {type: String, array: false}}) }
91
+
92
+ it "defines attr accessors for instance" do
93
+ expect(TestClass).to receive(:class_eval).with("attr_accessor :some_attr")
94
+ TestClass.send :define_attr_accessors
95
+ end
96
+
97
+ end
98
+
99
+ describe ".process_attributes" do
100
+
101
+ subject { TestClass.send :process_attributes, attributes }
102
+
103
+ context 'when simple value passed' do
104
+
105
+ let(:attributes) { [:some_attr] }
106
+
107
+ context "returns attributes with default settings" do
108
+
109
+ it { is_expected.to eq({some_attr: BetfairApiNgRails::Api::Data::Concerns::Attributable::DEFAULT_OPTIONS}) }
110
+
111
+ end
112
+
113
+ end
114
+
115
+ context 'when attribute with params passed' do
116
+
117
+ let(:attributes) { [{some_attr: {type: Integer}}] }
118
+
119
+ context "returns attributes with merged setting" do
120
+
121
+ it { is_expected.to eq({some_attr: {type: Integer, array: false}}) }
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
131
+ describe "integration tests" do
132
+
133
+ let(:attrib) { [:some_attr, {some_attr1: {type: Integer}}] }
134
+
135
+ before { TestClass.attributes attrib }
136
+
137
+ context 'for instance' do
138
+
139
+ subject { TestClass.new }
140
+
141
+ its(:some_attr) { is_expected.to be_nil }
142
+ its(:some_attr1) { is_expected.to be_nil }
143
+
144
+ end
145
+
146
+ context 'for class' do
147
+
148
+ subject { TestClass }
149
+
150
+ its('class_attrs.keys') { is_expected.to include(:some_attr) }
151
+
152
+ end
153
+
154
+ end
155
+
156
+ end
@@ -53,6 +53,36 @@ describe BetfairApiNgRails::Api::Data::Concerns::Hashable do
53
53
 
54
54
  end
55
55
 
56
+ describe "#get_attr_key" do
57
+
58
+ subject { TestClass.new.send :get_attr_key, attrib }
59
+
60
+ context 'when passed Hash' do
61
+
62
+ let(:attrib) { {some_attr: { type: String }} }
63
+
64
+ context 'returns first key' do
65
+
66
+ it { is_expected.to eq :some_attr }
67
+
68
+ end
69
+
70
+ end
71
+
72
+ context 'when passed single value' do
73
+
74
+ let(:attrib) { :some_attr }
75
+
76
+ context 'returns it' do
77
+
78
+ it { is_expected.to eq :some_attr }
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
56
86
  end
57
87
 
58
88
  describe "integration tests" do
@@ -76,6 +106,20 @@ describe BetfairApiNgRails::Api::Data::Concerns::Hashable do
76
106
 
77
107
  end
78
108
 
109
+ context 'when exists attr with additional options' do
110
+
111
+ before(:each) do
112
+ subject.class.send :instance_eval, <<-CODE
113
+ attr_accessor :second_name
114
+ def self.to_hash_attrs; [:name, {second_name: {type: String, array: false}}]; end
115
+ CODE
116
+ subject.second_name = "Black"
117
+ end
118
+
119
+ its(:to_hash) { is_expected.to eq({'name' => 'Walli', 'secondName' => 'Black'}) }
120
+
121
+ end
122
+
79
123
  end
80
124
 
81
125
  end