ig_markets 0.15 → 0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4205e8e7a404222272416344c5f21e912de43256
4
- data.tar.gz: 55595208ec8a08d8941d68387c6c61e19c436d5d
3
+ metadata.gz: 26ec69035134c7762495091e87dd4794c9aaebdf
4
+ data.tar.gz: 01373bb017a39473a812c60fa599ee0b4f082ec4
5
5
  SHA512:
6
- metadata.gz: 230f8eb850b639d3a6fa479cd42101ecd89c901382951adaa5a69d064029623a30c042cbe9bc44ee16d11b0e1cf50c034dc99e0aa43f3081063b27cda4a797ec
7
- data.tar.gz: 20991fd83b740be9a1bcc36ce2c112a7a63b2070adc9cf736617a130350b9810abe33663214b1b45889b3e53bda3741cc37f633068f1284cab30a09088d36d9b
6
+ metadata.gz: 6bad0ef7b8a20a769177bfbf8bb59454aa11f31c836efb62a8d9b18ce929a5aff7d59b5a2f51691d5f3862daa40bf00110136bc7a9d331336125c73335c96bd8
7
+ data.tar.gz: ac90eab61abcfa36d8c022f9d89514eee691e49feafd7aecbf579e710ba1880fea613b0d069a1cb5c2a0bd041a1d0d8a01a31186160ebb7f8c29436a0f446e00
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # IG Markets Changelog
2
2
 
3
+ ### 0.16 — July 20, 2016
4
+
5
+ - Switched to YAML for the `.ig_markets` config files and added support for storing multiple authentication profiles in
6
+ the config files that can then be selected on the command-line using the new `--profile NAME` argument
7
+ - Added `IGMarkets::DealingPlatform#client_account_summary`
8
+ - Report the HTTP code when a request fails
9
+
3
10
  ### 0.15 — July 11, 2016
4
11
 
5
12
  - Added `ig_markets self-test` command that can be run under a demo account in order to test the library against the
data/README.md CHANGED
@@ -46,21 +46,31 @@ $ ig_markets COMMAND [SUBCOMMAND] --username USERNAME --password PASSWORD --api-
46
46
 
47
47
  #### Config File
48
48
 
49
- On startup `ig_markets` searches for config files named `"./.ig_markets"` and then `"~/.ig_markets"`, and if they are
50
- present interprets their contents as command-line arguments. This can be used to avoid having to specify authentication
51
- details with every invocation.
52
-
53
- To do this create a config file at `"./.ig_markets"` or `"~/.ig_markets"` with the following contents:
54
-
55
- ```shell
56
- --username USERNAME
57
- --password PASSWORD
58
- --api-key API-KEY
59
- --demo # Include only if this is a demo account
49
+ On startup `ig_markets` looks for config files at `./.ig_markets.yml` and `~/.ig_markets.yml`. These are YAML files that
50
+ can hold named sets of predefined authentication profiles in order to avoid having to specify these repeatedly on the
51
+ command-line for every invocation.
52
+
53
+ The desired authentication profile is specified using the `--profile` command-line argument, and if the `--profile`
54
+ argument is omitted then the `default` profile will be used.
55
+
56
+ Here is an example of a config file with a `default` profile and a `demo` profile:
57
+
58
+ ```yaml
59
+ profiles:
60
+ default:
61
+ username: USERNAME
62
+ password: PASSWORD
63
+ api-key: API-KEY
64
+
65
+ demo:
66
+ username: DEMO-USERNAME
67
+ password: DEMO-PASSWORD
68
+ api-key: DEMO-API-KEY
69
+ demo: true
60
70
  ```
61
71
 
62
- The following examples assume the presence of a config file that contains valid authentication details.
63
-
72
+ The following examples assume the presence of a config file that contains a valid default authentication profile.
73
+
64
74
  #### Commands
65
75
 
66
76
  Use `ig_markets help` to get details on the options accepted by the commands and subcommands. The list of available
@@ -22,14 +22,14 @@ module IGMarkets
22
22
  attribute :direction, Symbol, allowed_values: [:buy, :sell]
23
23
  attribute :good_till_date
24
24
  attribute :guaranteed_stop, Boolean
25
- attribute :level, Float, nil_if: 0
25
+ attribute :level, Float
26
26
  attribute :limit_distance, Fixnum
27
- attribute :limit_level, Float, nil_if: 0
27
+ attribute :limit_level, Float
28
28
  attribute :market_name
29
29
  attribute :size
30
30
  attribute :stop_distance, Fixnum
31
- attribute :stop_level, Float, nil_if: 0
32
- attribute :trailing_step, Float, nil_if: 0
31
+ attribute :stop_level, Float
32
+ attribute :trailing_step, Float
33
33
  attribute :trailing_stop_distance, Fixnum
34
34
  end
35
35
 
@@ -1,33 +1,36 @@
1
1
  module IGMarkets
2
2
  module CLI
3
- # Helper class for working with the config files supported by the command-line client.
3
+ # Helper class for working with the YAML config files supported by the command-line client. A config file has a
4
+ # `profiles` root key and every subkey of this root key specifies the name of a profile. The values underneath each
5
+ # subkey are then interpreted as command-line arguments. The profile to use is chosen using the `--profiles`
6
+ # command-line argument.
7
+ #
8
+ # See `README.md` for further details.
4
9
  class ConfigFile
5
- # Initializes this config file with the passed lines.
10
+ # Initializes this config file with the passed content.
6
11
  #
7
- # @param [Array<String>] lines
8
- def initialize(lines = [])
9
- @lines = lines
10
- end
12
+ # @param [Hash] content
13
+ def initialize(content = {})
14
+ @content = content || {}
11
15
 
12
- # Returns the arguments contained in this config file.
13
- #
14
- # @return [Array<String>]
15
- def arguments
16
- @lines.map { |line| line.gsub(/#.*/, '') }
17
- .map(&:strip)
18
- .join(' ')
19
- .split(' ')
16
+ @profiles = (@content['profiles'] || {}).each_with_object({}) do |(profile_name, profile_arguments), result|
17
+ result[profile_name] = profile_arguments.map do |argument, value|
18
+ "--#{argument}=#{value}"
19
+ end
20
+ end
20
21
  end
21
22
 
22
- # Inserts the arguments from this config file into the passed arguments array.
23
+ # Inserts the arguments specified in this config file into the passed arguments array.
23
24
  #
24
25
  # @param [Array<String>] argv The array of command-line arguments to alter.
25
- def prepend_arguments_to_argv(argv)
26
+ def prepend_profile_arguments_to_argv(argv)
27
+ profile = selected_profile argv
28
+
26
29
  insert_index = argv.index do |argument|
27
30
  argument[0] == '-'
28
31
  end || -1
29
32
 
30
- argv.insert insert_index, *arguments
33
+ argv.insert insert_index, *@profiles.fetch(profile, [])
31
34
  end
32
35
 
33
36
  # Takes a list of potential config files and returns a {ConfigFile} instance for the first one that exists.
@@ -38,7 +41,21 @@ module IGMarkets
38
41
  File.exist? filename
39
42
  end
40
43
 
41
- new(config_file ? File.readlines(config_file) : [])
44
+ new(config_file && YAML.load_file(config_file))
45
+ end
46
+
47
+ private
48
+
49
+ # Searches the passed arguments list for the name of the profile to use (specified by the `--profile` argument).
50
+ def selected_profile(argv)
51
+ profile_index = argv.index '--profile'
52
+
53
+ if profile_index
54
+ argv.delete_at profile_index
55
+ argv.delete_at profile_index
56
+ else
57
+ 'default'
58
+ end
42
59
  end
43
60
  end
44
61
  end
@@ -8,6 +8,7 @@ module IGMarkets
8
8
  class_option :api_key, required: true, desc: 'The API key for the session'
9
9
  class_option :demo, type: :boolean, desc: 'Use the demo platform (default is the live platform)'
10
10
  class_option :verbose, type: :boolean, desc: 'Whether to print the raw REST API requests and responses'
11
+ class_option :profile, desc: 'The name of the authentication profile to use (will be read from the config file)'
11
12
 
12
13
  desc 'orders [SUBCOMAND=list ...]', 'Command for working with orders'
13
14
  subcommand 'orders', Orders
@@ -44,7 +45,7 @@ module IGMarkets
44
45
  #
45
46
  # @param [Array<String>] argv The array of command-line arguments.
46
47
  def bootstrap(argv)
47
- config_file.prepend_arguments_to_argv argv
48
+ config_file.prepend_profile_arguments_to_argv argv
48
49
 
49
50
  if argv.index('--version') || argv.index('-v')
50
51
  puts VERSION
@@ -68,7 +69,7 @@ module IGMarkets
68
69
 
69
70
  yield @dealing_platform
70
71
  rescue IGMarkets::RequestFailedError => request_failed_error
71
- error "Request error: #{request_failed_error.error}"
72
+ error "Request error (HTTP #{request_failed_error.http_code}): #{request_failed_error.error}"
72
73
  rescue ArgumentError => argument_error
73
74
  error "Argument error: #{argument_error}"
74
75
  end
@@ -147,7 +148,7 @@ module IGMarkets
147
148
  #
148
149
  # @return [ConfigFile]
149
150
  def config_file
150
- ConfigFile.find "#{Dir.pwd}/.ig_markets", "#{Dir.home}/.ig_markets"
151
+ ConfigFile.find "#{Dir.pwd}/.ig_markets.yml", "#{Dir.home}/.ig_markets.yml"
151
152
  end
152
153
 
153
154
  # Prints out details of the passed deal confirmation.
@@ -1,5 +1,5 @@
1
1
  module IGMarkets
2
- # Contains details on an IG Markets client account summary. Returned by {DealingPlatform#sign_in}.
2
+ # Contains details on an IG Markets client account summary. Returned by {DealingPlatform#client_account_summary}.
3
3
  class ClientAccountSummary < Model
4
4
  # Contains details on a form, used by {#form_details}.
5
5
  class FormDetails < Model
@@ -17,6 +17,9 @@ module IGMarkets
17
17
  # @return [Session] The session used by this dealing platform.
18
18
  attr_reader :session
19
19
 
20
+ # @return [ClientAccountSummary] The summary of the client account that is returned as part of a successful sign in.
21
+ attr_reader :client_account_summary
22
+
20
23
  # @return [AccountMethods] Methods for working with the logged in account.
21
24
  attr_reader :account
22
25
 
@@ -57,7 +60,8 @@ module IGMarkets
57
60
  # @param [String] api_key The account API key.
58
61
  # @param [:live, :demo] platform The platform to use.
59
62
  #
60
- # @return [ClientAccountSummary] The client account summary returned by the sign in request.
63
+ # @return [ClientAccountSummary] The client account summary returned by the sign in request. This result can also
64
+ # be accessed through the {#client_account_summary} accessor.
61
65
  def sign_in(username, password, api_key, platform)
62
66
  session.username = username
63
67
  session.password = password
@@ -66,7 +70,7 @@ module IGMarkets
66
70
 
67
71
  result = session.sign_in
68
72
 
69
- instantiate_models ClientAccountSummary, result
73
+ @client_account_summary = instantiate_models ClientAccountSummary, result
70
74
  end
71
75
 
72
76
  # Signs out of the IG Markets Dealing Platform, ending any current session.
@@ -25,7 +25,7 @@ module IGMarkets
25
25
  def parse(response)
26
26
  if response.is_a? Hash
27
27
  response.each_with_object({}) do |(key, value), new_hash|
28
- new_hash[camel_case_to_snake_case(key).downcase.to_sym] = parse(value)
28
+ new_hash[camel_case_to_snake_case(key).to_sym] = parse(value)
29
29
  end
30
30
  elsif response.is_a? Array
31
31
  response.map { |item| parse item }
@@ -40,7 +40,7 @@ module IGMarkets
40
40
  #
41
41
  # @return [String]
42
42
  def camel_case_to_snake_case(camel_case)
43
- camel_case.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z])([a-z])/, '\1_\2\3')
43
+ camel_case.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z])([a-z])/, '\1_\2\3').downcase
44
44
  end
45
45
  end
46
46
  end
@@ -1,4 +1,4 @@
1
1
  module IGMarkets
2
2
  # The version of this gem.
3
- VERSION = '0.15'.freeze
3
+ VERSION = '0.16'.freeze
4
4
  end
data/lib/ig_markets.rb CHANGED
@@ -7,6 +7,7 @@ require 'rest-client'
7
7
  require 'securerandom'
8
8
  require 'terminal-table'
9
9
  require 'thor'
10
+ require 'yaml'
10
11
 
11
12
  require 'ig_markets/boolean'
12
13
  require 'ig_markets/model'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ig_markets
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.15'
4
+ version: '0.16'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Viney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.10.3
33
+ version: 0.10.4
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.10.3
40
+ version: 0.10.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rest-client
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -198,14 +198,14 @@ dependencies:
198
198
  requirements:
199
199
  - - "~>"
200
200
  - !ruby/object:Gem::Version
201
- version: '0.8'
201
+ version: '0.9'
202
202
  type: :development
203
203
  prerelease: false
204
204
  version_requirements: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - "~>"
207
207
  - !ruby/object:Gem::Version
208
- version: '0.8'
208
+ version: '0.9'
209
209
  description:
210
210
  email: richard.viney@gmail.com
211
211
  executables:
@@ -310,4 +310,3 @@ specification_version: 4
310
310
  summary: Ruby library and command-line client for accessing the IG Markets dealing
311
311
  platform.
312
312
  test_files: []
313
- has_rdoc: