ambito 0.3.0 → 0.4.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: 626b627c3f3718bde249aa3d225720dcf255d7533409f0b5fa099a1f5152edb5
4
- data.tar.gz: cd38318ce750b2c7e594e79cad1aba342dc8dac0b89519a2bf1464d8ac464fd1
3
+ metadata.gz: ba13c62509e998895ea6d8170afc1cddd920cef11c5ba305a02a9e50c6b4b02b
4
+ data.tar.gz: 96c3c0fe54b4df10100e70e973e850e9222bd806cfe3c09926c8c90cffffb87a
5
5
  SHA512:
6
- metadata.gz: 2860ca43c23793ca8b82463dd326e7ff4a1bdbdd40ae67c085b843635d4cc40a4ffc25616e1279abde3ec34bbe5edf03e262aa7e3e27508bb5d4828b96517e13
7
- data.tar.gz: abcbe303761efed98fc3924fb0865eee13a58c819c51a350698a345c3e0d025678a5a92f4f2e169c30ebd96ee05f68ba7e43816cb5443e02b944cf5c636dc912
6
+ metadata.gz: d50f0ff5ae9f3d98d89c5bda8d0da55bef92e4ba1d562451138ef4ab5a02baa23a00edb60bd073830f6d50838eb33f29ed6b9d6d92a298bbdc05d4837744007a
7
+ data.tar.gz: ebc64d79fb35f9417214d0a877281e6fc5bf90f7f038ab88f4d7f7c68786d4a5d9adb60e80849a3dc2b1cc7a219175321edd2097c1b643e3812e0fc3e3744069
data/.standard.yml ADDED
@@ -0,0 +1 @@
1
+ ruby_version: 3.3.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0] - 2026-11-20
4
+
5
+ ### Added
6
+
7
+ - `Rate` exposes `dollar`, `buy`, `sell`, `price` and `variation`
8
+ - `Ambito::Error`, raised for an unknown dollar, an HTTP error or a response that cannot be read
9
+ - The CLI prints the error message and exits with status 1
10
+ - `Price#/`
11
+
12
+ ### Changed
13
+
14
+ - `Gateway.new` takes a `Dollar`, and `Gateway#rate` returns a `Rate`; `Gateway#get` is removed
15
+ - `Price.new` and `Variation.new` take numbers instead of strings
16
+ - `Price#+` returns a `Price` instead of a `Float`
17
+ - `Ambito.rates` raises `Ambito::Error` if any rate fails
18
+
19
+ ### Removed
20
+
21
+ - `Dollar#rate`; use `Ambito.rate` or `Gateway#rate`
22
+ - `Price.[]` and `Variation.[]`; use `.new`
23
+
24
+ ### Fixed
25
+
26
+ - A response with a missing field no longer prints `$ 0.00`
27
+
3
28
  ## [0.3.0] - 2023-11-27
4
29
 
5
30
  ### Added
data/README.md CHANGED
@@ -1,34 +1,58 @@
1
1
  # Ambito
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ambito`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Real-time USD exchange rates from Argentina's [Ambito Financiero](https://mercados.ambito.com).
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ ```
8
+ gem install ambito
9
+ ```
10
10
 
11
- Install the gem and add to the application's Gemfile by executing:
11
+ Or add to your Gemfile:
12
12
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
13
+ ```ruby
14
+ gem "ambito"
15
+ ```
14
16
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
17
+ ## Usage
16
18
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
19
+ ### Ruby
18
20
 
19
- ## Usage
21
+ ```ruby
22
+ # Fetch all rates (threaded)
23
+ Ambito.rates
24
+
25
+ # Fetch a single rate
26
+ rate = Ambito.rate("oficial")
27
+
28
+ rate.to_s # => "Oficial: $ 365.38 📈 0.23%"
29
+ rate.buy.quote # => 355.38
30
+ rate.sell.quote # => 375.38
31
+ rate.price.quote # => 365.38
32
+ rate.variation.value # => 0.23
33
+ ```
20
34
 
21
- TODO: Write usage instructions here
35
+ `price` is the value Ámbito publishes for the rate, or the average of buy and sell when it publishes none.
22
36
 
23
- ## Development
37
+ 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.
24
38
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+ ### CLI
26
40
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+ ```
42
+ ambito # all rates
43
+ ambito -d oficial # single rate
44
+ ```
28
45
 
29
- ## Contributing
46
+ ## Supported Dollar Types
30
47
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ambito. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ambito/blob/main/CODE_OF_CONDUCT.md).
48
+ | Name | Key |
49
+ |------|-----|
50
+ | Oficial | `oficial` |
51
+ | Informal (Blue) | `informal` |
52
+ | MEP | `mep` |
53
+ | CCL | `ccl` |
54
+ | Cripto | `cripto` |
55
+ | Turista | `turista` |
32
56
 
33
57
  ## License
34
58
 
@@ -36,4 +60,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
60
 
37
61
  ## Code of Conduct
38
62
 
39
- Everyone interacting in the Ambito project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ambito/blob/main/CODE_OF_CONDUCT.md).
63
+ Everyone interacting in the Ambito project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/arzezak/ambito/blob/main/CODE_OF_CONDUCT.md).
data/lib/ambito/cli.rb CHANGED
@@ -15,6 +15,8 @@ module Ambito
15
15
  else
16
16
  puts Ambito.rates
17
17
  end
18
+ rescue Error => e
19
+ abort e.message
18
20
  end
19
21
 
20
22
  private
data/lib/ambito/dollar.rb CHANGED
@@ -15,13 +15,6 @@ module Ambito
15
15
  @endpoint = endpoint
16
16
  end
17
17
 
18
- def rate
19
- gateway = Gateway.new(endpoint:)
20
- buy, sell, value, variation = gateway.get("compra", "venta", "valor", "variacion")
21
-
22
- Rate.new(dollar: self, buy:, sell:, value:, variation:)
23
- end
24
-
25
18
  def to_s
26
19
  display
27
20
  end
@@ -32,9 +25,8 @@ module Ambito
32
25
  end
33
26
 
34
27
  def find(name)
35
- LIST.find do |dollar|
36
- dollar.name == String(name)
37
- end
28
+ LIST.find { |dollar| dollar.name == String(name) } ||
29
+ raise(Error, "Unknown dollar: #{name}")
38
30
  end
39
31
  end
40
32
  end
@@ -7,24 +7,50 @@ module Ambito
7
7
  class Gateway
8
8
  BASE_URL = "https://mercados.ambito.com"
9
9
 
10
- def initialize(endpoint:)
11
- @endpoint = endpoint
10
+ def initialize(dollar)
11
+ @dollar = dollar
12
12
  end
13
13
 
14
- def get(*)
15
- JSON.parse(request).values_at(*)
14
+ def rate
15
+ Rate.new(
16
+ dollar:,
17
+ buy: number("compra"),
18
+ sell: number("venta"),
19
+ value: (number("valor") if payload.key?("valor")),
20
+ variation: number("variacion")
21
+ )
16
22
  end
17
23
 
18
24
  private
19
25
 
20
- attr_reader :endpoint
26
+ attr_reader :dollar
21
27
 
22
- def request
23
- Net::HTTP.get(uri)
28
+ def number(field)
29
+ parse(payload[field]) || raise(Error, "#{dollar}: bad #{field} in response")
24
30
  end
25
31
 
26
- def uri
27
- URI.join(BASE_URL, endpoint)
32
+ def parse(text)
33
+ Float(String(text).tr(",", ".").delete_suffix("%"), exception: false)
34
+ end
35
+
36
+ def payload
37
+ @payload ||= Hash.try_convert(json) || raise(Error, "#{dollar}: unexpected response")
38
+ end
39
+
40
+ def json
41
+ JSON.parse(body)
42
+ rescue JSON::ParserError
43
+ nil
44
+ end
45
+
46
+ def body
47
+ raise Error, "#{dollar}: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
48
+
49
+ response.body
50
+ end
51
+
52
+ def response
53
+ @response ||= Net::HTTP.get_response(URI.join(BASE_URL, dollar.endpoint))
28
54
  end
29
55
  end
30
56
  end
data/lib/ambito/price.rb CHANGED
@@ -3,25 +3,19 @@ module Ambito
3
3
  attr_reader :quote
4
4
 
5
5
  def initialize(quote)
6
- @quote = parse(quote)
6
+ @quote = quote
7
7
  end
8
8
 
9
9
  def +(other)
10
- quote + other.quote
10
+ self.class.new(quote + other.quote)
11
11
  end
12
12
 
13
- def parse(quote)
14
- String(quote).tr(",", ".").to_f
13
+ def /(other)
14
+ self.class.new(quote / other)
15
15
  end
16
16
 
17
17
  def to_s
18
- sprintf("$ %.2f" % quote)
19
- end
20
-
21
- class << self
22
- def [](quote)
23
- quote && new(quote) || nil
24
- end
18
+ "$ %.2f" % quote
25
19
  end
26
20
  end
27
21
  end
data/lib/ambito/rate.rb CHANGED
@@ -1,17 +1,21 @@
1
1
  module Ambito
2
2
  class Rate
3
- TRENDS = {up: "📈", down: "📉", equal: "📊"}.freeze
3
+ attr_reader :dollar, :buy, :sell, :variation
4
4
 
5
- def initialize(dollar:, buy:, sell:, value:, variation:)
5
+ def initialize(dollar:, buy:, sell:, variation:, value: nil)
6
6
  @dollar = dollar
7
- @buy = Price[buy]
8
- @sell = Price[sell]
9
- @value = Price[value]
10
- @variation = Variation[variation]
7
+ @buy = Price.new(buy)
8
+ @sell = Price.new(sell)
9
+ @value = value && Price.new(value)
10
+ @variation = Variation.new(variation)
11
+ end
12
+
13
+ def price
14
+ value || average
11
15
  end
12
16
 
13
17
  def average
14
- Price[(buy + sell) / 2]
18
+ (buy + sell) / 2
15
19
  end
16
20
 
17
21
  def to_s
@@ -20,10 +24,6 @@ module Ambito
20
24
 
21
25
  private
22
26
 
23
- attr_reader :dollar, :buy, :sell, :value, :variation
24
-
25
- def price
26
- value || average
27
- end
27
+ attr_reader :value
28
28
  end
29
29
  end
@@ -5,11 +5,11 @@ module Ambito
5
5
  attr_reader :value
6
6
 
7
7
  def initialize(value)
8
- @value = parse(value)
8
+ @value = value
9
9
  end
10
10
 
11
11
  def to_s
12
- sprintf("%.2f" % value) << "%"
12
+ "%.2f%%" % value
13
13
  end
14
14
 
15
15
  def trend
@@ -21,17 +21,5 @@ module Ambito
21
21
  TRENDS[:equal]
22
22
  end
23
23
  end
24
-
25
- private
26
-
27
- def parse(value)
28
- value.tr(",", ".").to_f
29
- end
30
-
31
- class << self
32
- def [](value)
33
- new(value)
34
- end
35
- end
36
24
  end
37
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ambito
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/ambito.rb CHANGED
@@ -9,17 +9,26 @@ require_relative "ambito/variation"
9
9
  require_relative "ambito/version"
10
10
 
11
11
  module Ambito
12
+ Error = Class.new(StandardError)
13
+
12
14
  class << self
13
15
  def rates
14
16
  Dollar.all.map do |dollar|
15
17
  Thread.new do
16
- dollar.rate
18
+ Thread.current.report_on_exception = false
19
+ fetch(dollar)
17
20
  end
18
21
  end.map(&:value)
19
22
  end
20
23
 
21
- def rate(dollar)
22
- Dollar.find(dollar).rate
24
+ def rate(name)
25
+ fetch(Dollar.find(name))
26
+ end
27
+
28
+ private
29
+
30
+ def fetch(dollar)
31
+ Gateway.new(dollar).rate
23
32
  end
24
33
  end
25
34
  end
metadata CHANGED
@@ -1,64 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
- - Ariel Rzezak
8
- autorequire:
7
+ - Ariel Rzezak
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-11-27 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description: A Ruby gem for retrieving real-time USD exchange rates and financial
14
- data.
12
+ description: A Ruby gem for retrieving real-time USD exchange rates and financial data.
15
13
  email:
16
- - arzezak@gmail.com
14
+ - arzezak@gmail.com
17
15
  executables:
18
- - ambito
16
+ - ambito
19
17
  extensions: []
20
18
  extra_rdoc_files: []
21
19
  files:
22
- - CHANGELOG.md
23
- - CODE_OF_CONDUCT.md
24
- - LICENSE.txt
25
- - README.md
26
- - Rakefile
27
- - ambito.gemspec
28
- - config/dollars.yml
29
- - exe/ambito
30
- - lib/ambito.rb
31
- - lib/ambito/cli.rb
32
- - lib/ambito/dollar.rb
33
- - lib/ambito/gateway.rb
34
- - lib/ambito/price.rb
35
- - lib/ambito/rate.rb
36
- - lib/ambito/variation.rb
37
- - lib/ambito/version.rb
38
- homepage: https://github.com/arzezak/ambito
20
+ - .standard.yml
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - config/dollars.yml
27
+ - exe/ambito
28
+ - lib/ambito.rb
29
+ - lib/ambito/cli.rb
30
+ - lib/ambito/dollar.rb
31
+ - lib/ambito/gateway.rb
32
+ - lib/ambito/price.rb
33
+ - lib/ambito/rate.rb
34
+ - lib/ambito/variation.rb
35
+ - lib/ambito/version.rb
36
+ homepage: "https://github.com/arzezak/ambito"
39
37
  licenses:
40
- - MIT
38
+ - MIT
41
39
  metadata:
42
- homepage_uri: https://github.com/arzezak/ambito
43
- source_code_uri: https://github.com/arzezak/ambito
44
- changelog_uri: https://github.com/arzezak/ambito/blob/main/CHANGELOG.md
45
- post_install_message:
40
+ homepage_uri: "https://github.com/arzezak/ambito"
41
+ source_code_uri: "https://github.com/arzezak/ambito"
42
+ changelog_uri: "https://github.com/arzezak/ambito/blob/main/CHANGELOG.md"
46
43
  rdoc_options: []
47
44
  require_paths:
48
- - lib
45
+ - lib
49
46
  required_ruby_version: !ruby/object:Gem::Requirement
50
47
  requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '0'
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
54
52
  required_rubygems_version: !ruby/object:Gem::Requirement
55
53
  requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- version: '0'
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
59
58
  requirements: []
60
- rubygems_version: 3.4.22
61
- signing_key:
59
+ rubygems_version: 4.1.0
62
60
  specification_version: 4
63
61
  summary: Retrieve real-time USD exchange rates.
64
62
  test_files: []
data/ambito.gemspec DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/ambito/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "ambito"
7
- spec.version = Ambito::VERSION
8
- spec.authors = ["Ariel Rzezak"]
9
- spec.email = ["arzezak@gmail.com"]
10
-
11
- spec.summary = "Retrieve real-time USD exchange rates."
12
- spec.description = "A Ruby gem for retrieving real-time USD exchange rates and financial data."
13
- spec.homepage = "https://github.com/arzezak/ambito"
14
- spec.license = "MIT"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = spec.homepage
18
- spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
-
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(__dir__) do
23
- `git ls-files -z`.split("\x0").reject do |f|
24
- (File.expand_path(f) == __FILE__) ||
25
- f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
26
- end
27
- end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
34
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
37
- end