quovo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +12 -0
  5. data/.travis.yml +6 -0
  6. data/Gemfile +10 -0
  7. data/LICENSE +21 -0
  8. data/README.md +176 -0
  9. data/Rakefile +12 -0
  10. data/bin/console +53 -0
  11. data/lib/quovo.rb +81 -0
  12. data/lib/quovo/api.rb +31 -0
  13. data/lib/quovo/api/accounts.rb +65 -0
  14. data/lib/quovo/api/base.rb +27 -0
  15. data/lib/quovo/api/brokerages.rb +21 -0
  16. data/lib/quovo/api/challenges.rb +28 -0
  17. data/lib/quovo/api/history.rb +36 -0
  18. data/lib/quovo/api/portfolios.rb +47 -0
  19. data/lib/quovo/api/positions.rb +30 -0
  20. data/lib/quovo/api/users.rb +44 -0
  21. data/lib/quovo/config.rb +61 -0
  22. data/lib/quovo/errors.rb +7 -0
  23. data/lib/quovo/fake.rb +29 -0
  24. data/lib/quovo/hook.rb +23 -0
  25. data/lib/quovo/models/account.rb +34 -0
  26. data/lib/quovo/models/base.rb +38 -0
  27. data/lib/quovo/models/brokerage.rb +15 -0
  28. data/lib/quovo/models/challenge.rb +34 -0
  29. data/lib/quovo/models/choice.rb +10 -0
  30. data/lib/quovo/models/image.rb +10 -0
  31. data/lib/quovo/models/portfolio.rb +50 -0
  32. data/lib/quovo/models/position.rb +36 -0
  33. data/lib/quovo/models/progress.rb +11 -0
  34. data/lib/quovo/models/sync.rb +20 -0
  35. data/lib/quovo/models/transaction.rb +37 -0
  36. data/lib/quovo/models/user.rb +14 -0
  37. data/lib/quovo/refinements/cast.rb +22 -0
  38. data/lib/quovo/refinements/compact.rb +11 -0
  39. data/lib/quovo/refinements/permit.rb +11 -0
  40. data/lib/quovo/refinements/require.rb +20 -0
  41. data/lib/quovo/refinements/sensitive.rb +38 -0
  42. data/lib/quovo/refinements/to_time.rb +18 -0
  43. data/lib/quovo/request.rb +114 -0
  44. data/lib/quovo/scope.rb +19 -0
  45. data/lib/quovo/token.rb +70 -0
  46. data/lib/quovo/version.rb +3 -0
  47. data/quovo.gemspec +19 -0
  48. metadata +93 -0
@@ -0,0 +1,19 @@
1
+ module Quovo
2
+ module Scope
3
+ def scope(attributes)
4
+ parent = current_scope
5
+ current_scope(current_scope.merge(attributes))
6
+ result = yield if block_given?
7
+ current_scope(parent)
8
+ result
9
+ end
10
+
11
+ def current_scope(*args)
12
+ if args.any?
13
+ Thread.current[:__quovo_scope__] = args.first
14
+ else
15
+ Thread.current[:__quovo_scope__] ||= {}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,70 @@
1
+ module Quovo
2
+ class Token
3
+ using Quovo::Refinements::ToTime
4
+ include Quovo::Request
5
+
6
+ def initialize(storage: Quovo.config.token_storage, ttl: Quovo.config.token_ttl, prefix: Quovo.config.token_prefix, username: Quovo.config.username, password: Quovo.config.password)
7
+ @storage = storage
8
+ @ttl = ttl
9
+ @prefix = prefix
10
+ @username = username
11
+ @password = password
12
+ @token = nil
13
+ @expires = nil
14
+ end
15
+
16
+ def get
17
+ token, expires = read_cache
18
+ if token.nil? || expires < now
19
+ token, expires = generate
20
+ write_cache(token, expires)
21
+ end
22
+ token
23
+ end
24
+
25
+ def generate
26
+ params = {
27
+ name: [prefix, expires_from_now, salt].join('---'),
28
+ expires: expires_from_now
29
+ }
30
+ payload = request(:post, '/tokens', params, :json) do |req|
31
+ req.basic_auth(username, password)
32
+ end.fetch('access_token')
33
+ [payload.fetch('token'), payload.fetch('expires').to_time.utc]
34
+ end
35
+
36
+ attr_reader :storage, :ttl, :prefix, :username, :password
37
+
38
+ private
39
+
40
+ STORAGE_KEY = 'quovo-access-token'.freeze
41
+ SPLITTER = '|'.freeze
42
+
43
+ def salt
44
+ ('a'..'z').to_a.sample(10).join
45
+ end
46
+
47
+ def now
48
+ Time.now.utc
49
+ end
50
+
51
+ def expires_from_now
52
+ (now + ttl).iso8601
53
+ end
54
+
55
+ def read_cache
56
+ return @token, @expires if @token && @expires
57
+ token, expires = (storage.read(STORAGE_KEY) || '').split(SPLITTER)
58
+ return nil unless expires
59
+ @token = token
60
+ @expires = expires.to_time.utc
61
+ [@token, @expires]
62
+ end
63
+
64
+ def write_cache(token, expires)
65
+ storage.write(STORAGE_KEY, [token, expires.utc.iso8601].join(SPLITTER))
66
+ @token = token
67
+ @date = expires
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Quovo
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quovo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'quovo'
8
+ spec.version = Quovo::VERSION
9
+ spec.authors = ['Alex Gorkunov', 'Nick Sidorov']
10
+ spec.email = ['alexander.gorkunov@gmail.com', 'theindiefly@gmail.com']
11
+ spec.homepage = 'https://github.com/CanopyFA/quovo-ruby'
12
+
13
+ spec.summary = 'Quovo API client'
14
+ spec.description = 'Quovo RESTful API client, configurable, thread-safe and well-tested'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^tests/}) }
18
+ spec.require_paths = ['lib']
19
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quovo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Gorkunov
8
+ - Nick Sidorov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-06-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Quovo RESTful API client, configurable, thread-safe and well-tested
15
+ email:
16
+ - alexander.gorkunov@gmail.com
17
+ - theindiefly@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".coveralls.yml"
23
+ - ".gitignore"
24
+ - ".rubocop.yml"
25
+ - ".travis.yml"
26
+ - Gemfile
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - lib/quovo.rb
32
+ - lib/quovo/api.rb
33
+ - lib/quovo/api/accounts.rb
34
+ - lib/quovo/api/base.rb
35
+ - lib/quovo/api/brokerages.rb
36
+ - lib/quovo/api/challenges.rb
37
+ - lib/quovo/api/history.rb
38
+ - lib/quovo/api/portfolios.rb
39
+ - lib/quovo/api/positions.rb
40
+ - lib/quovo/api/users.rb
41
+ - lib/quovo/config.rb
42
+ - lib/quovo/errors.rb
43
+ - lib/quovo/fake.rb
44
+ - lib/quovo/hook.rb
45
+ - lib/quovo/models/account.rb
46
+ - lib/quovo/models/base.rb
47
+ - lib/quovo/models/brokerage.rb
48
+ - lib/quovo/models/challenge.rb
49
+ - lib/quovo/models/choice.rb
50
+ - lib/quovo/models/image.rb
51
+ - lib/quovo/models/portfolio.rb
52
+ - lib/quovo/models/position.rb
53
+ - lib/quovo/models/progress.rb
54
+ - lib/quovo/models/sync.rb
55
+ - lib/quovo/models/transaction.rb
56
+ - lib/quovo/models/user.rb
57
+ - lib/quovo/refinements/cast.rb
58
+ - lib/quovo/refinements/compact.rb
59
+ - lib/quovo/refinements/permit.rb
60
+ - lib/quovo/refinements/require.rb
61
+ - lib/quovo/refinements/sensitive.rb
62
+ - lib/quovo/refinements/to_time.rb
63
+ - lib/quovo/request.rb
64
+ - lib/quovo/scope.rb
65
+ - lib/quovo/token.rb
66
+ - lib/quovo/version.rb
67
+ - quovo.gemspec
68
+ homepage: https://github.com/CanopyFA/quovo-ruby
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.5
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Quovo API client
92
+ test_files: []
93
+ has_rdoc: