cep_coinbase 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cep_coinbase.rb +148 -0
  3. metadata +86 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 33775fab3ace2c2f5933d9764e2a46f088d7d145953b26c0fb4158f0092bc78b
4
+ data.tar.gz: fdb6f5cbac64d9cdd69d501b25ea89bbe988b841bdc21c00eee8ffe3e3c394ce
5
+ SHA512:
6
+ metadata.gz: bf57bfb60440037d228234f8f1a99135fae4691b4edda4c7d85d2e0cc0dbbca7eadca900fec6a2e8628805499a4a4105f1edae4146cb8cdb5abf6ad1f8129d37
7
+ data.tar.gz: 7103aef4f915d1a437ee034f41cbffb52fb89d40a7d14405bfce3a5faedc7dafb47f8c3f2727ec6bb2db933b74c2cccc173d859bad3b9c4de6b605c0ba3e9cb1
@@ -0,0 +1,148 @@
1
+ # get, parse and show exchanges from coinbase.com
2
+ # CoinExchangeParser
3
+ class CEP_COINBASE
4
+ require 'json'
5
+ require 'ostruct'
6
+ require 'net/http'
7
+
8
+ attr_reader :exchanges, :currency_type
9
+
10
+ BASE_LINK = 'https://api.coinbase.com/v2/exchange-rates?currency=#CURRENCY#'.freeze
11
+
12
+ # XXX: this part can be added to the manual. But not now :)
13
+ # default value hash.
14
+ # TRY will be change to lira
15
+ VAR_NAMES = { 'TRY' => 'lira'}.freeze
16
+
17
+ # default coin currency type
18
+ CURRENCY_TYPE = 'ETH'.freeze
19
+ # default coinbase.com result json hierarchy
20
+ Hierarcy = ['data', 'rates'].freeze
21
+ # default cache folder
22
+ CacheFolder = 'logs/'.freeze
23
+
24
+ def initialize(
25
+ base_link: BASE_LINK,
26
+ vars: VAR_NAMES,
27
+ currency_type: CURRENCY_TYPE,
28
+ write_cache: true,
29
+ hierarcy: Hierarcy,
30
+ cache_file: nil,
31
+ cache_folder: CacheFolder
32
+ )
33
+ @currency_type= currency_type
34
+ @base_link = base_link
35
+
36
+ make_new_link
37
+
38
+ @var_names = vars
39
+ @write_status = write_cache
40
+ @hierarcy = hierarcy
41
+ @cache_file = cache_file
42
+ @cache_folder = cache_folder
43
+ @exchanges = {}
44
+ end
45
+
46
+ # get exchanges or read the cache file
47
+ def fetch
48
+ set_cache_file
49
+ body
50
+ end
51
+
52
+ # TODO: Write function more manageable
53
+ # parse data to object
54
+ def process
55
+ data = parse_exchanges
56
+ data = get_pure_data data
57
+
58
+ # make hash for currency array
59
+ data.each do |n, v|
60
+ v_f = v.to_f
61
+ @exchanges[n] = v_f if set_ins(n, v_f)
62
+ end
63
+ rescue
64
+ throw 'Initialization problem!'
65
+ end
66
+
67
+ # rename objects
68
+ # this function overwrite default exchange values
69
+ # with
70
+ def rename_param
71
+ @var_names.each do |old, new|
72
+ @body.gsub!(old, new)
73
+ end
74
+ end
75
+
76
+ # write @body to cache file
77
+ def cache
78
+ # if must write to cache
79
+ return false unless @write_status || @body
80
+
81
+ # if cache file exists ignore caching
82
+ return true if File.exists? @cache_file
83
+
84
+ File.open(@cache_folder + @cache_file, 'w') do |cache|
85
+ cache.write @body
86
+ end
87
+ rescue
88
+ throw 'caching problem(s) occurs!'
89
+ end
90
+
91
+ private
92
+
93
+ def make_new_link
94
+ @link = @base_link.gsub('#CURRENCY#',
95
+ @currency_type)
96
+ end
97
+
98
+ # name like 'name' and value is an object
99
+ # set attr_reader for name
100
+ # and add obj to variable
101
+ def set_ins(name, obj)
102
+ name = name.dup.downcase
103
+ self.class.__send__(:attr_reader, name)
104
+ instance_variable_set('@' + name, obj)
105
+ rescue
106
+ return false
107
+ end
108
+
109
+ def get_pure_data(data)
110
+ # go into hash
111
+ @hierarcy.each { |str| data = data[str] }
112
+ return data
113
+ rescue
114
+ throw 'unknowing exchange type'
115
+ end
116
+
117
+ # convert @body to hash
118
+ def parse_exchanges
119
+ @data_content = JSON.parse(@body)
120
+ rescue
121
+ throw 'JSON parse error!'
122
+ end
123
+
124
+ # get @body from url link
125
+ def link
126
+ Net::HTTP.get(URI.parse(@link))
127
+ end
128
+
129
+ # get @body from file
130
+ def file
131
+ File.read @cache_file
132
+ end
133
+
134
+ # if cache_file name not exits
135
+ # remake name
136
+ def set_cache_file
137
+ return @cache_file if @cache_file
138
+
139
+ # TODO: make cache file format changable
140
+ time = Time.new.strftime '%y.%m.%d_%H'
141
+ @cache_file = "#{@currency_type}_#{time}.data"
142
+ end
143
+
144
+ # select body source and get
145
+ def body
146
+ @body = File.exist?(@cache_file) ? file : link
147
+ end
148
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cep_coinbase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - tayak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: ostruct
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.5.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.1
55
+ description: Coin Exchange Parser(CEP) is a functional exchange calculation class
56
+ which converts received json data from coinbase.com and uses it as a class method.
57
+ email: yasir.kiroglu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/cep_coinbase.rb
63
+ homepage: https://github.com/taiak/cep
64
+ licenses:
65
+ - Apache-2.0
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.2.29
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Coin Exchange Parser which use coinbase.com values
86
+ test_files: []