ambito 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 462d50880203564140c94ddc453453e037d5f4f8ea850c2b16a4068f9cf0d3ee
4
- data.tar.gz: bd0f0043ba559b415293bc3bc83efad1c887680e7b977fbb1de38ea2a4fc6d0b
3
+ metadata.gz: a63b3377e8682e02fc63067ce58b68a21904f7af2cac2233030d02221876b767
4
+ data.tar.gz: 7f0de8a97fd26b2618b3dfa7a3bca359ac78739cec25a65d5617fb67c4e1b965
5
5
  SHA512:
6
- metadata.gz: e739540eca436c28cfa4528c2fa92e971e2ebe082ac083d580ee63af3c8ac25f65a6b836bb4d6d13c8fb91a27ae0acb7c548b59c13e1720f88e5bbf2366e2d71
7
- data.tar.gz: 62d62c492dd899c97899384e0bc1927d300e2eef8c6f8b80a3bb712c232bb485d19a25c4e8a326c2fde3f220e0377ce148be4b13ed3c9c13c8d5e2e7a3f0c250
6
+ metadata.gz: 7f3e887d17edf54c5d1e7d231319f344b9eae0b6366eca8da5fe82359f951ff9bc8514b0e645abeeaff4087af00d2a6b497808e8b19534497cb58be79c7fb99d
7
+ data.tar.gz: 9cdd04adeeb9489a048241076732f90c6b28ba4494eb649ec60b072051e9edcd3bc50284e586ab1ab3c4b8f2aeb019be508fc209c363fc8db6714968817dbcb7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.6.0] - 2026-11-26
4
+
5
+ ### Added
6
+
7
+ - `Ambito.history(name, from:, to:)`, the daily quotes of a dollar between two dates, as an `Ambito::History`
8
+ - `Ambito::Chart`, which draws a `History` as a line chart with its first, last, highest and lowest quote and the change
9
+
10
+ ### Changed
11
+
12
+ - `ambito -d DOLLAR` prints a chart of the last 30 days in place of the table with one rate
13
+
14
+ ### Removed
15
+
16
+ - `Rate#price` and `Rate#average`; read `value`, or `buy` and `sell`
17
+ - `Price#+` and `Price#/`
18
+ - `Variation#trend`
19
+
3
20
  ## [0.5.0] - 2026-11-25
4
21
 
5
22
  ### Added
data/README.md CHANGED
@@ -28,21 +28,32 @@ rate = Ambito.rate("oficial")
28
28
  rate.buy.quote # => 355.38
29
29
  rate.buy.to_s # => "$ 355.38"
30
30
  rate.sell.quote # => 375.38
31
- rate.price.quote # => 365.38
31
+ rate.value # => nil
32
32
  rate.variation.value # => 0.23
33
33
  rate.variation.to_s # => "+0.23%"
34
34
  rate.updated_at # => 2026-11-25 13:48:00 -0300
35
+
36
+ # Fetch the daily quotes between two dates
37
+ history = Ambito.history("bna", from: Date.new(2026, 10, 27), to: Date.new(2026, 11, 26))
38
+
39
+ history.opening.date # => #<Date: 2026-10-27>
40
+ history.closing.price.quote # => 1625.0
41
+ history.high.price.to_s # => "$ 1630.00"
42
+ history.change # => 65.0
43
+ history.variation.to_s # => "+4.17%"
35
44
  ```
36
45
 
37
- `price` is the value Ámbito publishes for the rate, or the average of buy and sell when it publishes none. `value` is that published value, or `nil`. To print rates, pass them to `Ambito::Table.new(rates)`, which is what the CLI does.
46
+ `value` is the single value Ámbito publishes for a rate with no buy and sell, such as MEP or Turista, as a price like `buy` and `sell`; it is `nil` for the rest. To print rates, pass them to `Ambito::Table.new(rates)`, which is what the CLI does.
38
47
 
39
48
  Both methods raise `Ambito::Error` for an unknown dollar, an HTTP error or a response that cannot be read. `Ambito.rates` raises if any one rate fails.
40
49
 
50
+ A `History` holds one quote per day, the one Ámbito charts: the sell price for a rate with buy and sell. `points` has every day, each with a `date` and a `price`. To print it, pass it to `Ambito::Chart.new(history)`.
51
+
41
52
  ### CLI
42
53
 
43
54
  ```
44
55
  ambito # all rates
45
- ambito -d oficial # single rate
56
+ ambito -d bna # chart of the last 30 days of one rate
46
57
  ```
47
58
 
48
59
  The CLI prints a table, in colour when the output is a terminal. A rate with no buy and sell shows the value Ámbito publishes across both columns. `Actualización` is the time since Ámbito last updated the rate.
@@ -59,6 +70,36 @@ The CLI prints a table, in colour when the output is a terminal. A rate with no
59
70
  └─────────┴───────────────────────┴────────┴───────────────┘
60
71
  ```
61
72
 
73
+ With `-d`, the CLI prints a chart of the last 30 days.
74
+
75
+ ```
76
+ BNA 27/10/2026 – 26/11/2026 +4.17%
77
+
78
+ │ $ 1630.00
79
+ │ ⢠⠓⠤⡀
80
+ │ ⢀⠇ ⠈
81
+ │ ⡜
82
+ │ ⡠⠜
83
+ │ ⢀⠔⠊
84
+ │ ⢀⡠⠊⠁
85
+ │ ⡠⠔⠁
86
+ │ ⢰⠁
87
+ │ ⡇
88
+ │ ⡸
89
+ │ ⢀⠔⠁
90
+ │ ⣀⠔⠁
91
+ │ ⣀⡠⠤⠒⠒⠒⠊
92
+ │ ⡠⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠔⠒⠉
93
+ │ ⢀⣀⠤⠊
94
+ │ ⣀⣀⣀⣀⣀⣀⣀⣀⠤⠔⠒⠉⠁
95
+ │ $ 1560.00
96
+ └─────────────────────────────────────────────────────────────────────────
97
+
98
+ Inicio: $ 1560.00 (27/10) // Fin: $ 1625.00 (26/11)
99
+ Máximo: $ 1630.00 (25/11) // Mínimo: $ 1560.00 (27/10)
100
+ Cambio: +65.00 (+4.17%)
101
+ ```
102
+
62
103
  ## Supported Dollar Types
63
104
 
64
105
  | Name | Key |
@@ -0,0 +1,119 @@
1
+ require_relative "paint"
2
+
3
+ module Ambito
4
+ class Chart
5
+ include Paint
6
+
7
+ BLANK_BRAILLE = 0x2800
8
+ DOTS_PER_CELL_ROW = 4
9
+ DOTS_PER_CELL_COLUMN = 2
10
+
11
+ DOT_BITS = [
12
+ [0x01, 0x08],
13
+ [0x02, 0x10],
14
+ [0x04, 0x20],
15
+ [0x40, 0x80]
16
+ ].freeze
17
+
18
+ def initialize(history, width: 72, height: 16, color: false)
19
+ @history = history
20
+ @width = width
21
+ @height = height
22
+ @color = color
23
+ end
24
+
25
+ def to_s
26
+ [heading, "", *plot, "", *summary].join("\n")
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :history, :width, :height, :color
32
+
33
+ def heading
34
+ "#{paint(history.dollar, :cyan)} #{day(history.opening, "%d/%m/%Y")} – #{day(history.closing, "%d/%m/%Y")} " \
35
+ "#{paint(history.variation, trend)}"
36
+ end
37
+
38
+ def plot
39
+ [
40
+ axis(history.high.price),
41
+ *rows.map { |row| axis(paint(row, :cyan)) },
42
+ axis(history.low.price),
43
+ paint(" └#{"─" * (width + 1)}", :dim)
44
+ ]
45
+ end
46
+
47
+ def summary
48
+ separator = paint("//", :dim)
49
+
50
+ [
51
+ "#{label("Inicio")} #{stamp(history.opening)} #{separator} #{label("Fin")} #{stamp(history.closing)}",
52
+ "#{label("Máximo")} #{stamp(history.high)} #{separator} #{label("Mínimo")} #{stamp(history.low)}",
53
+ "#{label("Cambio")} #{paint("%+.2f" % history.change, trend)} (#{paint(history.variation, trend)})"
54
+ ]
55
+ end
56
+
57
+ def axis(text)
58
+ "#{paint(" │", :dim)} #{text}".rstrip
59
+ end
60
+
61
+ def label(name)
62
+ paint("#{name}:", :dim)
63
+ end
64
+
65
+ def stamp(point)
66
+ "#{point.price} (#{day(point, "%d/%m")})"
67
+ end
68
+
69
+ def day(point, format)
70
+ point.date.strftime(format)
71
+ end
72
+
73
+ def trend
74
+ history.change.negative? ? :red : :green
75
+ end
76
+
77
+ def rows
78
+ cells = Array.new(height) { Array.new(width, 0) }
79
+
80
+ dots.each do |column, level|
81
+ cells[height - 1 - level / DOTS_PER_CELL_ROW][column / DOTS_PER_CELL_COLUMN] |=
82
+ DOT_BITS[DOTS_PER_CELL_ROW - 1 - level % DOTS_PER_CELL_ROW][column % DOTS_PER_CELL_COLUMN]
83
+ end
84
+
85
+ cells.map do |cells_in_row|
86
+ cells_in_row.map { |bits| bits.zero? ? " " : (BLANK_BRAILLE + bits).chr(Encoding::UTF_8) }.join.rstrip
87
+ end
88
+ end
89
+
90
+ def dots
91
+ marks.each_cons(2).flat_map do |(from_column, from_level), (to_column, to_level)|
92
+ steps = [to_column - from_column, (to_level - from_level).abs, 1].max
93
+
94
+ (0..steps).map do |step|
95
+ [
96
+ from_column + ((to_column - from_column) * step / steps.to_f).round,
97
+ from_level + ((to_level - from_level) * step / steps.to_f).round
98
+ ]
99
+ end
100
+ end
101
+ end
102
+
103
+ def marks
104
+ first_day = history.opening.date
105
+ lowest_quote = history.low.price.quote
106
+
107
+ history.points.map do |point|
108
+ [
109
+ scale(point.date - first_day, history.closing.date - first_day, width * DOTS_PER_CELL_COLUMN),
110
+ scale(point.price.quote - lowest_quote, history.high.price.quote - lowest_quote, height * DOTS_PER_CELL_ROW)
111
+ ]
112
+ end
113
+ end
114
+
115
+ def scale(offset, extent, dot_count)
116
+ extent.zero? ? 0 : (offset * (dot_count - 1) / extent.to_f).round
117
+ end
118
+ end
119
+ end
data/lib/ambito/cli.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require "date"
1
2
  require "optparse"
2
3
 
3
4
  module Ambito
4
5
  class CLI
6
+ CHART_DAYS = 30
7
+
5
8
  def initialize(argv, now: Time.now)
6
9
  @argv = argv
7
10
  @now = now
@@ -10,7 +13,7 @@ module Ambito
10
13
  def run
11
14
  case options
12
15
  in {dollar:}
13
- puts table([Ambito.rate(dollar)])
16
+ puts chart(dollar)
14
17
  in {version: true}
15
18
  puts Ambito::VERSION
16
19
  else
@@ -22,6 +25,12 @@ module Ambito
22
25
 
23
26
  private
24
27
 
28
+ def chart(dollar)
29
+ today = @now.to_date
30
+
31
+ Chart.new(Ambito.history(dollar, from: today - CHART_DAYS, to: today), color: $stdout.tty?)
32
+ end
33
+
25
34
  def table(rates)
26
35
  Table.new(rates, now: @now, color: $stdout.tty?)
27
36
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "date"
3
4
  require "net/http"
4
5
  require "json"
5
6
  require "time"
@@ -8,6 +9,7 @@ module Ambito
8
9
  class Gateway
9
10
  BASE_URL = "https://mercados.ambito.com"
10
11
  TIME_FORMAT = "%d/%m/%Y %H:%M %z"
12
+ DATE_FORMAT = "%d/%m/%Y"
11
13
  UTC_OFFSET = "-03:00"
12
14
 
13
15
  def initialize(dollar)
@@ -25,6 +27,17 @@ module Ambito
25
27
  )
26
28
  end
27
29
 
30
+ def history(from:, to:)
31
+ _titles, *rows = Array.try_convert(fetch("#{File.dirname(dollar.endpoint)}/grafico/#{from}/#{to}")) ||
32
+ raise(Error, "#{dollar}: unexpected response")
33
+
34
+ raise Error, "#{dollar}: no history in response" if rows.empty?
35
+
36
+ History.new(dollar:, quotes: rows.to_h { |date, quote| [Date.strptime(date, DATE_FORMAT), Float(quote)] })
37
+ rescue ArgumentError, TypeError
38
+ raise Error, "#{dollar}: bad history in response"
39
+ end
40
+
28
41
  private
29
42
 
30
43
  attr_reader :dollar
@@ -44,23 +57,16 @@ module Ambito
44
57
  end
45
58
 
46
59
  def payload
47
- @payload ||= Hash.try_convert(json) || raise(Error, "#{dollar}: unexpected response")
48
- end
49
-
50
- def json
51
- JSON.parse(body)
52
- rescue JSON::ParserError
53
- nil
60
+ @payload ||= Hash.try_convert(fetch(dollar.endpoint)) || raise(Error, "#{dollar}: unexpected response")
54
61
  end
55
62
 
56
- def body
63
+ def fetch(path)
64
+ response = Net::HTTP.get_response(URI.join(BASE_URL, path))
57
65
  raise Error, "#{dollar}: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
58
66
 
59
- response.body
60
- end
61
-
62
- def response
63
- @response ||= Net::HTTP.get_response(URI.join(BASE_URL, dollar.endpoint))
67
+ JSON.parse(response.body)
68
+ rescue JSON::ParserError
69
+ nil
64
70
  end
65
71
  end
66
72
  end
@@ -0,0 +1,36 @@
1
+ module Ambito
2
+ class History
3
+ Point = Data.define(:date, :price)
4
+
5
+ attr_reader :dollar, :points
6
+
7
+ def initialize(dollar:, quotes:)
8
+ @dollar = dollar
9
+ @points = quotes.sort.map { |date, quote| Point.new(date:, price: Price.new(quote)) }
10
+ end
11
+
12
+ def opening
13
+ points.first
14
+ end
15
+
16
+ def closing
17
+ points.last
18
+ end
19
+
20
+ def high
21
+ points.max_by { |point| point.price.quote }
22
+ end
23
+
24
+ def low
25
+ points.min_by { |point| point.price.quote }
26
+ end
27
+
28
+ def change
29
+ closing.price.quote - opening.price.quote
30
+ end
31
+
32
+ def variation
33
+ Variation.new(change / opening.price.quote * 100)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module Ambito
2
+ module Paint
3
+ STYLES = {dim: 2, red: 31, green: 32, yellow: 33, cyan: 36}.freeze
4
+
5
+ private
6
+
7
+ def paint(text, style)
8
+ color ? "\e[#{STYLES[style]}m#{text}\e[0m" : text
9
+ end
10
+ end
11
+ end
data/lib/ambito/price.rb CHANGED
@@ -6,14 +6,6 @@ module Ambito
6
6
  @quote = quote
7
7
  end
8
8
 
9
- def +(other)
10
- self.class.new(quote + other.quote)
11
- end
12
-
13
- def /(other)
14
- self.class.new(quote / other)
15
- end
16
-
17
9
  def to_s
18
10
  "$ %.2f" % quote
19
11
  end
data/lib/ambito/rate.rb CHANGED
@@ -10,13 +10,5 @@ module Ambito
10
10
  @variation = Variation.new(variation)
11
11
  @updated_at = updated_at
12
12
  end
13
-
14
- def price
15
- value || average
16
- end
17
-
18
- def average
19
- (buy + sell) / 2
20
- end
21
13
  end
22
14
  end
data/lib/ambito/table.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require_relative "paint"
2
+
1
3
  module Ambito
2
4
  class Table
5
+ include Paint
6
+
3
7
  TITLES = ["", "Compra", "Venta", "Cambio", "Actualización"].freeze
4
- STYLES = {dim: 2, red: 31, green: 32, yellow: 33, cyan: 36}.freeze
5
8
  GAP_WIDTH = 3
6
9
 
7
10
  BETWEEN_BUY_AND_SELL = {
@@ -89,10 +92,6 @@ module Ambito
89
92
  cell.ljust(widths[column])
90
93
  end
91
94
 
92
- def paint(text, style)
93
- color ? "\e[#{STYLES[style]}m#{text}\e[0m" : text
94
- end
95
-
96
95
  def widths
97
96
  @widths ||= [TITLES, *rates.map { |rate| cells(rate) }].transpose.map do |cells_in_column|
98
97
  cells_in_column.map(&:length).max
@@ -1,7 +1,5 @@
1
1
  module Ambito
2
2
  class Variation
3
- TRENDS = {up: "📈", down: "📉", equal: "📊"}.freeze
4
-
5
3
  attr_reader :value
6
4
 
7
5
  def initialize(value)
@@ -11,15 +9,5 @@ module Ambito
11
9
  def to_s
12
10
  "#{"+" if value.positive?}%.2f%%" % value
13
11
  end
14
-
15
- def trend
16
- if value.positive?
17
- TRENDS[:up]
18
- elsif value.negative?
19
- TRENDS[:down]
20
- else
21
- TRENDS[:equal]
22
- end
23
- end
24
12
  end
25
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ambito
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/ambito.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "ambito/age"
4
+ require_relative "ambito/chart"
4
5
  require_relative "ambito/cli"
5
6
  require_relative "ambito/dollar"
6
7
  require_relative "ambito/gateway"
8
+ require_relative "ambito/history"
7
9
  require_relative "ambito/price"
8
10
  require_relative "ambito/rate"
9
11
  require_relative "ambito/table"
@@ -27,6 +29,10 @@ module Ambito
27
29
  fetch(Dollar.find(name))
28
30
  end
29
31
 
32
+ def history(name, from:, to:)
33
+ Gateway.new(Dollar.find(name)).history(from:, to:)
34
+ end
35
+
30
36
  private
31
37
 
32
38
  def fetch(dollar)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Rzezak
@@ -28,9 +28,12 @@ files:
28
28
  - exe/ambito
29
29
  - lib/ambito.rb
30
30
  - lib/ambito/age.rb
31
+ - lib/ambito/chart.rb
31
32
  - lib/ambito/cli.rb
32
33
  - lib/ambito/dollar.rb
33
34
  - lib/ambito/gateway.rb
35
+ - lib/ambito/history.rb
36
+ - lib/ambito/paint.rb
34
37
  - lib/ambito/price.rb
35
38
  - lib/ambito/rate.rb
36
39
  - lib/ambito/table.rb