invertir-online 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e22d62d78144147a24bb67954db71fb52dfa10cc572e8524c848aef08424a69
4
+ data.tar.gz: b01bb60f6cd5d2fc792bd2315d6183448de6900792350e5f9b003037cb39a9e3
5
+ SHA512:
6
+ metadata.gz: 23f0951a7c896dace773fd893cebb2a695c3bb45808e36d1c304d44d2ddb59a68b30701956597d49e888dc84bcbd7e4574ce29bf0fdf7b9864725340c2f63485
7
+ data.tar.gz: 637474bc32baae5af84056d9cd40e0b5b08c4d4247860169eafe6be3f2afb2f342ab4d5d1bf58f44172907e4729cfc723387833a90d97e9cc380263a85f188e0
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'invertir-online'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/devinit ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env bash
2
+ bundle install
3
+
4
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require 'invertir-online/version'
2
+ require 'invertir-online/client/rest'
@@ -0,0 +1,54 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ require_relative 'rest/sign_request_middleware'
5
+ require_relative 'rest/timestamp_request_middleware'
6
+ require_relative 'rest/clients'
7
+ require_relative 'rest/endpoints'
8
+ require_relative 'rest/methods'
9
+
10
+ module InvertirOnline
11
+ module Client
12
+ class REST
13
+ BASE_URL = 'https://api.invertironline.com/'.freeze
14
+
15
+ def initialize(user: '', password: '',
16
+ adapter: Faraday.default_adapter)
17
+ @clients = {}
18
+ response = Faraday.post('https://api.invertironline.com/token', username: user, password: password, grant_type: 'password')
19
+ token = JSON.parse(response.body)["access_token"]
20
+ @clients[:signed] = verified_client token, adapter
21
+ end
22
+
23
+ METHODS.each do |method|
24
+ define_method(method[:name]) do |options = {}|
25
+ response = @clients[method[:client]].send(method[:action]) do |req|
26
+ endpoint = replace_path_variables(ENDPOINTS[method[:endpoint]], options)
27
+ req.url endpoint
28
+ req.params.merge! options.map { |k, v| [camelize(k.to_s), v] }.to_h
29
+ end
30
+ response.body
31
+ end
32
+ end
33
+
34
+ def self.add_query_param(query, key, value)
35
+ query = query.to_s
36
+ query << '&' unless query.empty?
37
+ query << "#{Faraday::Utils.escape key}=#{Faraday::Utils.escape value}"
38
+ end
39
+
40
+ def replace_path_variables(endpoint, options)
41
+ replaced_endpoint = endpoint
42
+ options.each do |k, v|
43
+ replaced_endpoint.gsub!(k.to_s, v)
44
+ end
45
+ replaced_endpoint
46
+ end
47
+
48
+ def camelize(str)
49
+ str.split('_')
50
+ .map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ require 'faraday_middleware'
2
+
3
+ module InvertirOnline
4
+ module Client
5
+ class REST
6
+ def verified_client(token, adapter)
7
+ Faraday.new(url: "#{BASE_URL}/api") do |conn|
8
+ conn.response :json, content_type: /\bjson$/
9
+ conn.headers['Authorization'] = "Bearer #{token}"
10
+ conn.adapter adapter
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module InvertirOnline
2
+ module Client
3
+ class REST
4
+ ENDPOINTS = {
5
+
6
+ # Account API Endpoints
7
+ buy_order: 'v2/operar/Comprar',
8
+ sell_order: 'v2/operar/Vender',
9
+ all_orders: 'v2/operaciones',
10
+ account_info: 'v2/estadocuenta',
11
+ price: 'v2/mercado/titulos/simbolo/cotizacion'
12
+ }.freeze
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module InvertirOnline
2
+ module Client
3
+ class REST
4
+ METHODS = [
5
+ # Account API Methods
6
+ # #create_buy_order!
7
+ { name: :create_buy_order!, client: :signed,
8
+ action: :post, endpoint: :buy_order },
9
+ # #create_sell_order!
10
+ { name: :create_sell_order!, client: :signed,
11
+ action: :post, endpoint: :sell_order },
12
+ # #all_orders
13
+ { name: :all_orders, client: :signed,
14
+ action: :get, endpoint: :all_orders },
15
+ # #account_info
16
+ { name: :account_info, client: :signed,
17
+ action: :get, endpoint: :account_info },
18
+ # #price
19
+ { name: :price, client: :signed,
20
+ action: :get, endpoint: :price },
21
+ ].freeze
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module InvertirOnline
2
+ module Client
3
+ class REST
4
+ # Sign the query string using HMAC(sha-256) and appends to query string
5
+ SignRequestMiddleware = Struct.new(:app, :secret_key) do
6
+ def call(env)
7
+ hash = OpenSSL::HMAC.hexdigest(
8
+ OpenSSL::Digest.new('sha256'), secret_key, env.url.query
9
+ )
10
+ env.url.query = REST.add_query_param(env.url.query, 'signature', hash)
11
+
12
+ app.call env
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'date'
2
+
3
+ module InvertirOnline
4
+ module Client
5
+ class REST
6
+ # Generate a timestamp in milliseconds and append to query string
7
+ TimestampRequestMiddleware = Struct.new(:app) do
8
+ def call(env)
9
+ env.url.query = REST.add_query_param(
10
+ env.url.query, 'timestamp', DateTime.now.strftime('%Q')
11
+ )
12
+
13
+ app.call env
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module InvertirOnline
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invertir-online
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Agustin Pina
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-08 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: '2.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faye-websocket
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description:
98
+ email:
99
+ - aguspina87@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - bin/console
105
+ - bin/devinit
106
+ - lib/invertir-online.rb
107
+ - lib/invertir-online/client/rest.rb
108
+ - lib/invertir-online/client/rest/clients.rb
109
+ - lib/invertir-online/client/rest/endpoints.rb
110
+ - lib/invertir-online/client/rest/methods.rb
111
+ - lib/invertir-online/client/rest/sign_request_middleware.rb
112
+ - lib/invertir-online/client/rest/timestamp_request_middleware.rb
113
+ - lib/invertir-online/version.rb
114
+ homepage: https://github.com/aguspina/invertir-online
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: API Wrapper for the Invertir Online stock exchange.
137
+ test_files: []