avantage 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +60 -0
  4. data/lib/avantage.rb +103 -2
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29165b7eb62a73fefa8c1007475b6dcec0a92467ca589f2398bd2ed881d83824
4
- data.tar.gz: '027311472815753cb99601d1fe4dc815141264f03861176b87247e865fffe636'
3
+ metadata.gz: b636721c2bffd6f0c193f31a9a92a3ece3dd818cde4e4d095fba8a1c28f8c543
4
+ data.tar.gz: bddfec80486d6f303de470a28f4fd27579999d324b9051e5fe06e6e632abf070
5
5
  SHA512:
6
- metadata.gz: 39b6ea975fcaf2825c4425d971450623c2a7cadc3d363c486896e4f5b01a3959dc5e9111f3ed1f9e21f4870c62e9f26a509b91abafcb9cdc454dad1b3565acba
7
- data.tar.gz: 9bb51cc603e4116a7ba98e57b3b941c480d29b3af72951508fe96cde8c89b2aba68e267d55cc0df84c74d7dae112993ca4dbf7028d3ab534e2e8ae35af077552
6
+ metadata.gz: b55eb347c6ae28226358d1606e59bb1e1f6bfe0c75e0a136bbb68d08bd2e86770b6d9e665e19673d60fd1bac1e2e847760a88dd52af322ee7cef2f3d8c150cd7
7
+ data.tar.gz: 7722bab312b6c954160691007aaa2b5c5a3657923c60fbd19525230f2cd5c8ccef49e2bfce4084ae9de61903f492fc404d76bafefa3b019acfe5ade597947088
@@ -2,6 +2,11 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## avantage 0.1.0 released 2019-06-19
6
+
7
+ * Initial working release
8
+
9
+
5
10
  ## avantage 0.0.1 released 2019-06-14
6
11
 
7
12
  * Initial release
data/README.md CHANGED
@@ -8,6 +8,66 @@ A Ruby library to query the [Alpha Vantage API](https://www.alphavantage.co/docu
8
8
 
9
9
  ## usage
10
10
 
11
+ The first step is to instantiate a client:
12
+ ```ruby
13
+ require 'avantage'
14
+
15
+ client = Avantage::Client.new('6SAMPLEWO0KLUG55')
16
+ # or more something like
17
+ client = Avantage::Client.new(File.read('.api.key').strip)
18
+ ```
19
+
20
+ Functions may be called directly:
21
+ ```ruby
22
+ require 'pp'
23
+
24
+ pp client.get(:global_quote, symbol: 'GOOG')
25
+ #
26
+ # ==>
27
+ #
28
+ # {"Global Quote"=>
29
+ # {"01. symbol"=>"GOOG",
30
+ # "02. open"=>"1109.6900",
31
+ # "03. high"=>"1116.3900",
32
+ # "04. low"=>"1098.9995",
33
+ # "05. price"=>"1103.6000",
34
+ # "06. volume"=>"1386684",
35
+ # "07. latest trading day"=>"2019-06-18",
36
+ # "08. previous close"=>"1092.5000",
37
+ # "09. change"=>"11.1000",
38
+ # "10. change percent"=>"1.0160%"}}
39
+ ```
40
+
41
+ GLOBAL_QUOTE, SYMBOL_SEARCH, CURRENCY_EXCHANGE_RATE, and SECTOR can be called directly:
42
+ ```ruby
43
+ client.global_quote('GOOG')
44
+ client.global_quote(symbol: 'GOOG')
45
+ client.global_quote(symbol: 'GOOG', datatype: 'csv')
46
+ client.quote('GOOG')
47
+ # ...
48
+
49
+ client.symbol_search(keywords: 'IBM')
50
+ client.symbol_search('IBM')
51
+ client.search(keywords: 'IBM')
52
+ client.search('IBM')
53
+ client.search(keywords: 'IBM', datatype: 'csv')
54
+ # ...
55
+
56
+ client.currency_exchange_rate('USD', 'JPY')
57
+ client.exchange_rate('USD', 'JPY')
58
+ client.exchange_rate(from: 'USD', to: 'JPY')
59
+ client.exchange_rate(from_currency: 'USD', to_currency: 'JPY')
60
+ client.forex(from: 'USD', to: 'JPY')
61
+ client.forex('USD', 'JPY')
62
+ client.forex(from: 'USD', to: 'JPY', datatype: 'csv')
63
+ # ...
64
+
65
+ client.sectors
66
+ client.sector
67
+ client.sectors(datatype: 'csv')
68
+ # ...
69
+ ```
70
+
11
71
 
12
72
  ## license
13
73
 
@@ -1,8 +1,109 @@
1
1
 
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+
2
6
  module Avantage
3
7
 
4
- VERSION = '0.0.1'
8
+ VERSION = '0.1.0'
9
+
10
+ USER_AGENT = "avantage #{VERSION} - https://github.com/jmettraux/avantage"
11
+ API_ROOT_URI = 'https://www.alphavantage.co/query?'
12
+
13
+ class Client
14
+
15
+ def initialize(api_key)
16
+
17
+ @api_key = api_key
18
+ end
19
+
20
+ def global_quote(symbol)
21
+
22
+ os = symbol.is_a?(Hash) ? symbol : { symbol: symbol }
23
+
24
+ get('GLOBAL_QUOTE', os)
25
+ end
26
+ alias quote global_quote
27
+
28
+ def symbol_search(keywords)
29
+
30
+ os = keywords.is_a?(Hash) ? keywords : { keywords: keywords }
31
+
32
+ get('SYMBOL_SEARCH', os)
33
+ end
34
+ alias search symbol_search
35
+
36
+ def sector(os={})
37
+
38
+ get('SECTOR', os)
39
+ end
40
+ alias sectors sector
41
+
42
+ def currency_exchange_rate(from, to=nil)
43
+
44
+ os =
45
+ if from.is_a?(Hash)
46
+ from.inject({}) { |k, v|
47
+ if k == :from
48
+ h[:from_currency] = v
49
+ elsif k == :to
50
+ h[:to_currency] = v
51
+ else
52
+ h[k] = v
53
+ end
54
+ h }
55
+ else
56
+ { from_currency: from, to_currency: to }
57
+ end
58
+
59
+ get('CURRENCY_EXCHANGE_RATE', os)
60
+ end
61
+ alias exchange_rate currency_exchange_rate
62
+ alias forex currency_exchange_rate
63
+
64
+ def get(function, parameters)
65
+
66
+ func = function.to_s.upcase
67
+
68
+ params = parameters.merge(function: func, apikey: @api_key)
69
+ params[:datatype] = 'json' if params.delete(:json)
70
+ params[:datatype] = 'csv' if params.delete(:csv)
71
+
72
+ uri = API_ROOT_URI + URI.encode_www_form(params)
73
+
74
+ req = Net::HTTP::Get.new(uri)
75
+ req.instance_eval { @header.clear }
76
+ def req.set_header(k, v); @header[k] = [ v ]; end
77
+
78
+ req.set_header('User-Agent', USER_AGENT)
79
+
80
+ u = URI(uri)
81
+
82
+ t0 = monow
83
+
84
+ t = Net::HTTP.new(u.host, u.port)
85
+ t.use_ssl = (u.scheme == 'https')
86
+ #t.set_debug_output($stdout)
87
+
88
+ res = t.request(req)
89
+
90
+ r = res.body
91
+ if res.content_type.match(/\Aapplication\/json\b/)
92
+ r = JSON.parse(r)
93
+ end
94
+
95
+ class << r; attr_accessor :_response, :_client, :_elapsed; end
96
+ #
97
+ r._response = res
98
+ r._client = self
99
+ r._elapsed = monow - t0
100
+
101
+ r
102
+ end
103
+
104
+ protected
5
105
 
6
- USER_AGENT = "avantage - #{VERSION} - https://github.com/jmettraux/avantage"
106
+ def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end
107
+ end
7
108
  end
8
109
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avantage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-14 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec