ambito 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c265a6e3056079bd1e4a3649877cbc253dd2951b88cf09897fd77382de2a9f6a
4
- data.tar.gz: 6d8ba5bcfa6bf8f56243c13f8cf69d0f28f0d5336790fabb31395bd42fd8b072
3
+ metadata.gz: 9108ecf4cec0a17c75f766b42d6aab71e970927c6d853cdd7bcaee2b57d0c49a
4
+ data.tar.gz: 4289936f867c7f384282f695a1a256acf237cf7eb24abd1206a64d9054a58959
5
5
  SHA512:
6
- metadata.gz: 12d8278b1df950077cc1da344ec0bbd8aec1e99feae39cdee08bfcd18108dae5f91ea43a5dddf90f3ae350bc4b2dd9ae0bfd14c626d48d9dc8e96316ab8e6427
7
- data.tar.gz: 0c480f522071a99c8a25d7739d68ba479bcb4645af716bcc6af88a97eede71b60fa85af4e97891c994c3478ea2a0010b7ea1edfca29c6ada8defac549122c46c
6
+ metadata.gz: 5c0f57262a90b245b8ff3f54ab60f0d35a24fd3b746948a0d06b199885fbbd0f757f5f0297896a4eeba74b42afd8b31101b5f9b57adfd067238f469756152f27
7
+ data.tar.gz: 2d080b8844e91cea05c546cdd9c9684fb385617ada3a85f6689b0c28c9447653c6da15a4d944fe35b3eeba72c5719464c0ecfc7505c144cb14e6e9f54ce33f0a
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## [Unreleased]
1
+ # Changelog
2
+
3
+ ## [0.2.0] - 2023-11-24
4
+
5
+ ### Added
6
+
7
+ - Option to fetch a single rate
2
8
 
3
9
  ## [0.1.0] - 2023-11-22
4
10
 
@@ -0,0 +1,16 @@
1
+ - !ruby/object:Ambito::Dollar
2
+ name: oficial
3
+ display: Oficial
4
+ endpoint: /dolar/oficial/variacion
5
+ - !ruby/object:Ambito::Dollar
6
+ name: informal
7
+ display: Informal
8
+ endpoint: /dolar/informal/variacion
9
+ - !ruby/object:Ambito::Dollar
10
+ name: mep
11
+ display: MEP
12
+ endpoint: /dolarrava/mep/variacion
13
+ - !ruby/object:Ambito::Dollar
14
+ name: cripto
15
+ display: Cripto
16
+ endpoint: /dolarcripto/variacion
data/exe/ambito CHANGED
@@ -4,4 +4,4 @@ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
4
 
5
5
  require "ambito"
6
6
 
7
- Ambito::CLI.new.run
7
+ Ambito::CLI.new(ARGV).run
data/lib/ambito/cli.rb CHANGED
@@ -1,7 +1,29 @@
1
+ require "optparse"
2
+
1
3
  module Ambito
2
4
  class CLI
5
+ def initialize(argv)
6
+ @argv = argv
7
+ end
8
+
3
9
  def run
4
- puts Ambito.fetch
10
+ if options.any?
11
+ puts Ambito.rate(*options)
12
+ else
13
+ puts Ambito.rates
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def options
20
+ [].tap do |parsed_options|
21
+ OptionParser.new do |options|
22
+ options.on("-d", "--dollar DOLLAR", "Select the rate") do |dollar|
23
+ parsed_options << dollar
24
+ end
25
+ end.parse(@argv)
26
+ end
5
27
  end
6
28
  end
7
29
  end
data/lib/ambito/dollar.rb CHANGED
@@ -1,43 +1,45 @@
1
+ require "yaml"
2
+
1
3
  module Ambito
2
4
  class Dollar
3
- EMOJI = {up: "📈", down: "📉", equal: "📊"}.freeze
4
- TYPES = {oficial: "Oficial", informal: "Informal", mep: "MEP"}.freeze
5
-
6
- def initialize(buy:, sell:, type:, variation:)
7
- @buy = float(buy)
8
- @sell = float(sell)
9
- @type = TYPES.fetch(type.to_sym)
10
- @variation = float(variation)
5
+ LIST = YAML.safe_load_file(
6
+ "./config/dollars.yml",
7
+ permitted_classes: [Ambito::Dollar]
8
+ ).freeze
9
+
10
+ attr_reader :name, :display, :endpoint
11
+
12
+ def initialize(name:, display:, endpoint:)
13
+ @name = name
14
+ @display = display
15
+ @endpoint = endpoint
11
16
  end
12
17
 
13
- def average
14
- (buy + sell) / 2
18
+ def rate
19
+ gateway = Gateway.new(endpoint:)
20
+ buy, sell, variation = gateway.get("compra", "venta", "variacion")
21
+
22
+ Rate.new(dollar: self, buy:, sell:, variation:)
15
23
  end
16
24
 
17
25
  def to_s
18
- "#{type}: $ #{format(average)} #{emoji} #{format(variation)}%"
26
+ display
19
27
  end
20
28
 
21
- private
22
-
23
- attr_reader :buy, :sell, :type, :variation
24
-
25
- def emoji
26
- if variation.positive?
27
- EMOJI[:up]
28
- elsif variation.negative?
29
- EMOJI[:down]
30
- else
31
- EMOJI[:equal]
29
+ class << self
30
+ def all
31
+ LIST
32
32
  end
33
- end
34
33
 
35
- def float(value)
36
- value.tr(",", ".").to_f
37
- end
34
+ def find(name)
35
+ LIST.find do |dollar|
36
+ dollar.name == String(name)
37
+ end
38
+ end
38
39
 
39
- def format(number)
40
- sprintf("%.2f" % number)
40
+ def where(name: nil)
41
+ name && find(name) || all
42
+ end
41
43
  end
42
44
  end
43
45
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ module Ambito
7
+ class Gateway
8
+ BASE_URL = "https://mercados.ambito.com"
9
+
10
+ def initialize(endpoint:)
11
+ @endpoint = endpoint
12
+ end
13
+
14
+ def get(*)
15
+ JSON.parse(request).values_at(*)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :endpoint
21
+
22
+ def request
23
+ Net::HTTP.get(uri)
24
+ end
25
+
26
+ def uri
27
+ URI.join(BASE_URL, endpoint)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ module Ambito
2
+ class Rate
3
+ TRENDS = {up: "📈", down: "📉", equal: "📊"}.freeze
4
+
5
+ def initialize(dollar:, buy:, sell:, variation:)
6
+ @dollar = dollar
7
+ @buy = float(buy)
8
+ @sell = float(sell)
9
+ @variation = float(variation)
10
+ end
11
+
12
+ def average
13
+ (buy + sell) / 2
14
+ end
15
+
16
+ def to_s
17
+ "#{dollar}: $ #{format(average)} #{trend} #{format(variation)}%"
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :dollar, :buy, :sell, :variation
23
+
24
+ def trend
25
+ if variation.positive?
26
+ TRENDS[:up]
27
+ elsif variation.negative?
28
+ TRENDS[:down]
29
+ else
30
+ TRENDS[:equal]
31
+ end
32
+ end
33
+
34
+ def float(value)
35
+ value.tr(",", ".").to_f
36
+ end
37
+
38
+ def format(number)
39
+ sprintf("%.2f" % number)
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ambito
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/ambito.rb CHANGED
@@ -1,32 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
3
  require_relative "ambito/cli"
6
4
  require_relative "ambito/dollar"
5
+ require_relative "ambito/gateway"
6
+ require_relative "ambito/rate"
7
7
  require_relative "ambito/version"
8
8
 
9
9
  module Ambito
10
- class Error < StandardError; end
11
-
12
- DOLLARS = %w[
13
- dolar/oficial
14
- dolar/informal
15
- dolarrava/mep
16
- ].freeze
17
-
18
10
  class << self
19
- def fetch
20
- DOLLARS.map do |dollar|
21
- res = JSON.parse(Net::HTTP.get(uri(dollar)))
22
- type = dollar.split("/").last
23
- buy, sell, variation = res.values_at("compra", "venta", "variacion")
24
- Dollar.new(type:, buy:, sell:, variation:)
25
- end
11
+ def rates
12
+ Dollar.all.map(&:rate)
26
13
  end
27
14
 
28
- def uri(dollar)
29
- URI("https://mercados.ambito.com/#{dollar}/variacion")
15
+ def rate(dollar)
16
+ Dollar.find(dollar).rate
30
17
  end
31
18
  end
32
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Rzezak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-22 00:00:00.000000000 Z
11
+ date: 2023-11-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby gem for retrieving real-time USD exchange rates and financial
14
14
  data.
@@ -25,12 +25,14 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - ambito.gemspec
28
+ - config/dollars.yml
28
29
  - exe/ambito
29
30
  - lib/ambito.rb
30
31
  - lib/ambito/cli.rb
31
32
  - lib/ambito/dollar.rb
33
+ - lib/ambito/gateway.rb
34
+ - lib/ambito/rate.rb
32
35
  - lib/ambito/version.rb
33
- - sig/ambito.rbs
34
36
  homepage: https://github.com/arzezak/ambito
35
37
  licenses:
36
38
  - MIT
data/sig/ambito.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Ambito
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end