hg-finance 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8c3c29ec2ee03c3d9fe913e153681e16b44dae0
4
+ data.tar.gz: 53eac99f9645eef8b4ada3fdd5237de6412be636
5
+ SHA512:
6
+ metadata.gz: 0966553e351939fa13826ab5ac29ce2e4cb50e6d334fe9d27c167058af15fab9e7e46ca65a13ee8c005bc7a88248f12be490d569e50609914a18edebb8891e24
7
+ data.tar.gz: 8c7005b4e5b3ad0ea79fa510541adcdbbb2e76797aef3bc26cfb4bc7989fe959362076f7fea12f96767d6b1044466e5e64a91b36f945a5151fcb7031f1bd2a6a
@@ -0,0 +1,54 @@
1
+ require 'hg/finance/locale'
2
+
3
+ module HG
4
+ module Finance
5
+
6
+ class Currency
7
+ # Public: Name
8
+ attr_accessor :name
9
+
10
+ # Public: ISO code
11
+ attr_accessor :iso_code
12
+
13
+ # Public: Source currency
14
+ attr_accessor :source
15
+
16
+ # Public: Price to buy
17
+ attr_accessor :buy
18
+
19
+ # Public: Price to seel
20
+ attr_accessor :sell
21
+
22
+ # Public: Last day variation
23
+ attr_accessor :variation
24
+
25
+ def initialize(options = {})
26
+ if options.count != 0
27
+ @name = options[:name] if options[:name]
28
+ @iso_code = options[:iso_code] if options[:iso_code]
29
+ @source = options[:source] if options[:source]
30
+ @buy = options[:buy].to_f if options[:buy]
31
+ @sell = options[:sell].to_f if options[:sell]
32
+ @variation = options[:variation].to_f if options[:variation]
33
+ end
34
+ end
35
+
36
+ def to_s separator = ' - '
37
+ to_return = []
38
+
39
+ to_return << self.name.to_s + ' (' + self.iso_code.to_s + ')'
40
+
41
+ to_return << "#{Locale.get_format(:buy).to_s.capitalize}: " + "#{self.source} #{self.buy}" if self.buy
42
+ to_return << "#{Locale.get_format(:sell).to_s.capitalize}: " + "#{self.source} #{self.sell}" if self.sell
43
+ to_return << "#{Locale.get_format(:variation).to_s.capitalize}: " + self.variation.to_s if self.variation
44
+
45
+ return to_return.join(separator)
46
+ end
47
+
48
+ def inspect
49
+ self.to_s
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,73 @@
1
+ require 'hg/finance/currency'
2
+ require 'hg/finance/taxes'
3
+ require 'hg/finance/stock'
4
+
5
+ module HG
6
+ module Finance
7
+
8
+ class Data
9
+
10
+ attr_accessor :request, :requested_at, :key_status
11
+ attr_accessor :taxes, :currencies, :stocks
12
+
13
+ def initialize params, host_name, use_ssl = true
14
+ query_params = params.map{|k,v| "#{k.to_s}=#{v.to_s}"}.join('&')
15
+ @request = (use_ssl ? 'https' : 'http') + host_name + '?' + query_params
16
+ @requested_at = Time.now
17
+
18
+ request_data = JSON.parse(open(self.request).read)
19
+
20
+ if request_data['results']
21
+ results = request_data['results']
22
+
23
+ @key_status = (params[:key] ? (request_data['valid_key'] ? :valid : :invalid) : :empty)
24
+
25
+ @taxes = Taxes.new(to_taxes(results['taxes'].first)) if @key_status == :valid
26
+
27
+ @currencies = {}
28
+ results['currencies'].each do |iso, currency|
29
+ next if iso == 'source'
30
+ @currencies[iso] = Currency.new(to_currency(currency, iso, results['currencies']['source']))
31
+ end
32
+
33
+ @stocks = {}
34
+ results['stocks'].each do |code, stock|
35
+ @stocks[code] = Stock.new(to_stock(stock))
36
+ end
37
+ end
38
+
39
+ return self
40
+ end
41
+
42
+ def to_taxes r
43
+ {
44
+ date: r['date'],
45
+ cdi: r['cdi'],
46
+ selic: r['selic'],
47
+ daily_factor: r['daily_factor']
48
+ }
49
+ end
50
+
51
+ def to_currency r, iso_code, source
52
+ {
53
+ source: source,
54
+ iso_code: iso_code,
55
+ name: r['name'],
56
+ buy: r['buy'],
57
+ sell: r['sell'],
58
+ variation: r['variation']
59
+ }
60
+ end
61
+
62
+ def to_stock r
63
+ {
64
+ name: r['name'],
65
+ location: r['location'],
66
+ variation: r['variation']
67
+ }
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,36 @@
1
+ module HG
2
+ module Finance
3
+
4
+ class Locale
5
+
6
+ @formats = {
7
+ en: {
8
+ date: '%Y-%m-%d',
9
+ short_date: '%m-%d',
10
+ datetime: '%Y-%m-%d %H:%M',
11
+ buy: :buy,
12
+ sell: :sell,
13
+ variation: :variation,
14
+ daily_factor: 'daily factor'
15
+ },
16
+ :'pt-br' => {
17
+ date: '%d/%m/%Y',
18
+ short_date: '%d/%m',
19
+ datetime: '%d/%m/%Y %H:%M',
20
+ buy: :compra,
21
+ sell: :venda,
22
+ variation: 'variação',
23
+ daily_factor: 'fator diário'
24
+ }
25
+ }
26
+
27
+ def self.get_format item
28
+ Finance.locale = :en unless @formats.has_key?(Finance.locale.to_sym)
29
+
30
+ return @formats[Finance.locale.to_sym][item]
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ require 'hg/finance/locale'
2
+
3
+ module HG
4
+ module Finance
5
+
6
+ class Stock
7
+ # Public: Name
8
+ attr_accessor :name
9
+
10
+ # Public: Market Identifier Code
11
+ # attr_accessor :mic
12
+
13
+ # Public: Location
14
+ attr_accessor :location
15
+
16
+ # Public: Last util day variation
17
+ attr_accessor :variation
18
+
19
+ def initialize(options = {})
20
+ if options.count != 0
21
+ @name = options[:name] if options[:name]
22
+ # @mic = options[:mic] if options[:mic]
23
+ @location = options[:location] if options[:location]
24
+ @variation = options[:variation] if options[:variation]
25
+ end
26
+ end
27
+
28
+ def to_s separator = ' - '
29
+ to_return = []
30
+
31
+ to_return << "#{self.name} - #{self.location}" if self.name && self.location
32
+ to_return << "#{Locale.get_format(:variation).to_s.capitalize}: " + self.variation.to_s if self.variation
33
+
34
+ return to_return.join(separator)
35
+ end
36
+
37
+ def inspect
38
+ self.to_s
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ require 'hg/finance/locale'
2
+
3
+ module HG
4
+ module Finance
5
+
6
+ class Taxes
7
+ # Public: Date
8
+ attr_accessor :date
9
+
10
+ # Public: ISO code
11
+ attr_accessor :cdi
12
+
13
+ # Public: Source currency
14
+ attr_accessor :selic
15
+
16
+ # Public: Price to buy
17
+ attr_accessor :daily_factor
18
+
19
+ def initialize(options = {})
20
+ if options.count != 0
21
+ @date = process_datetime(options[:date]) if options[:date]
22
+ @cdi = options[:cdi] if options[:cdi]
23
+ @selic = options[:selic] if options[:selic]
24
+ @daily_factor = options[:daily_factor] if options[:daily_factor]
25
+ end
26
+ end
27
+
28
+ def to_s separator = ' - '
29
+ to_return = []
30
+
31
+ to_return << self.date.strftime(Locale.get_format(:short_date)) if self.date && self.date.kind_of?(Time)
32
+
33
+ to_return << 'CDI: ' + self.cdi.to_s if self.cdi
34
+ to_return << 'SELIC: ' + self.selic.to_s if self.selic
35
+ to_return << "#{Locale.get_format(:daily_factor).to_s.capitalize}: " + self.daily_factor.to_s if self.daily_factor
36
+
37
+ return to_return.join(separator)
38
+ end
39
+
40
+ def inspect
41
+ self.to_s
42
+ end
43
+
44
+ protected
45
+ def process_datetime date, time = nil
46
+ return Time.now if date.nil?
47
+
48
+ return Time.strptime((date + ' ' + (time ? time : '00:00')), Locale.get_format(:datetime))
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module HG
2
+ module Finance
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/lib/hg/finance.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+
4
+ require 'hg/finance/version'
5
+ require 'hg/finance/data'
6
+
7
+ module HG
8
+ module Finance
9
+ HOST_NAME = '://api.hgbrasil.com/finance'
10
+
11
+ class << self
12
+ # API Key
13
+ attr_accessor :api_key
14
+
15
+ # Locale
16
+ attr_accessor :locale
17
+
18
+ # API Key status
19
+ attr_reader :api_key_status
20
+
21
+ # Use SSL to access the API
22
+ attr_accessor :use_ssl
23
+
24
+ # Use Rails cache for recieved data (realy recommended)
25
+ attr_accessor :use_rails_cache
26
+ end
27
+
28
+ def self.locale
29
+ @locale || :en
30
+ end
31
+
32
+ def self.use_ssl
33
+ @use_ssl || true
34
+ end
35
+
36
+ def self.setup(&block)
37
+ yield self
38
+ end
39
+
40
+ def self.get(options = {})
41
+ process({})
42
+ end
43
+
44
+ def self.process params
45
+ params = defaults.merge(params).delete_if{|k,v| v.nil?}
46
+
47
+ return HG::Finance::Data.new(params, HOST_NAME, self.use_ssl)
48
+ end
49
+
50
+ def self.defaults
51
+ {
52
+ key: self.api_key,
53
+ locale: self.locale,
54
+ format: :json,
55
+ sdk_version: "ruby_f#{HG::Finance::VERSION}"
56
+ }
57
+ end
58
+
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hg-finance
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hugo Demiglio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Get brazilian finance data using HG Finance API.
14
+ email:
15
+ - hugodemiglio@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hg/finance.rb
21
+ - lib/hg/finance/currency.rb
22
+ - lib/hg/finance/data.rb
23
+ - lib/hg/finance/locale.rb
24
+ - lib/hg/finance/stock.rb
25
+ - lib/hg/finance/taxes.rb
26
+ - lib/hg/finance/version.rb
27
+ homepage: http://hgbrasil.com/finance
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.5.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Simple gem to get finance data from HG Finance.
51
+ test_files: []