electionbuddy-ruby 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: e4d7bab3a1ccbb73ba611b3f24efe2cb30f46f8971fbdfcc9f7199d4cd90a5d1
4
- data.tar.gz: f7ad3cf9058cf3e87e8b270223b593e8a3a3703a34d0a72f8441ac6a6a1111e1
3
+ metadata.gz: 609fbcd8ec13547ffc65048afb495236d9eecbe0a787bea74bee39315b3e92c1
4
+ data.tar.gz: 35682f5a36ea65fe229f9239cf14335b132e6ef94e7befc79d9e601338262e81
5
5
  SHA512:
6
- metadata.gz: fd1e47298c9ede0a03d6e2ff07be9118d7f41ccfacb1acf1635d55e06ddd63ea7309fa965fac97a5dc2ea1b8e20d0eed20714af0761390197d2389661456a734
7
- data.tar.gz: 61e1aac0c642c65f791f4b1f93f7602cc3e34c4c94f3a5d4875e223b483703dc56521a5961199d5675123d5c93b16110107d64a89eed38c93d133c7c221f3ca0
6
+ metadata.gz: 7cbf3bc35b59210fcf1a8a0e906dafa5112ea801568edc5aee762019edc3dcdc49936087e7c58613657b3d732af42400ea735da644f717e38d89a2e1eb8b074b
7
+ data.tar.gz: 7fadf9a933f95c94b949dbec9533bf6c775cc25c16252e30f80d3895ab7c49408e29d01518e034d9d71148e6e258634ce01df6f7d4710625d21cfee4ff9ba431
data/.rubocop.yml CHANGED
@@ -6,3 +6,13 @@ Style/StringLiterals:
6
6
 
7
7
  Style/StringLiteralsInInterpolation:
8
8
  EnforcedStyle: double_quotes
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Naming/FileName:
14
+ Exclude:
15
+ - 'lib/electionbuddy-ruby.rb'
16
+
17
+ Metrics/MethodLength:
18
+ Max: 15
data/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
1
8
  ## [Unreleased]
2
9
 
3
10
  ## [0.1.0] - 2024-10-29
4
11
 
5
12
  - Initial release
13
+
14
+ ## [0.2.0] - 2024-11-07
15
+
16
+ ### Added
17
+
18
+ - Faraday as a runtime dependency
19
+ - Debug as a development dependency
20
+ - API call framework: resource-oriented design to structure and support API calls
21
+ - Ability to validate the voter list of a given vote
22
+
23
+ ### Changed
24
+
25
+ - Update README.md with usage instructions
26
+ - Update .rubocop.yml with new rules
27
+
28
+ ### Fixed
29
+
30
+ - Fix console startup issue caused by incorrect code reference
31
+
32
+ [unreleased]: https://github.com/electionbuddy/electionbuddy-ruby/compare/v0.2.0...HEAD
33
+ [0.2.0]: https://github.com/electionbuddy/electionbuddy-ruby/compare/v0.1.0...v0.2.0
34
+ [0.1.0]: https://github.com/electionbuddy/electionbuddy-ruby/releases/tag/v0.1.0
35
+
data/README.md CHANGED
@@ -1,24 +1,60 @@
1
- # Electionbuddy::Ruby
1
+ # ElectionBuddy Ruby
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/electionbuddy/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A Ruby client for interacting with the ElectionBuddy API.
4
+ You can find the API documentation [here](https://api.electionbuddy.com).
6
5
 
7
6
  ## Installation
8
7
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_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.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
8
+ Install the gem and add it to the application's Gemfile by executing:
12
9
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ bundle add electionbuddy-ruby
14
11
 
15
12
  If bundler is not being used to manage dependencies, install the gem by executing:
16
13
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+ gem install electionbuddy-ruby
15
+
16
+ Alternatively, add the gem to the Gemfile:
17
+
18
+ ```ruby
19
+ gem 'electionbuddy-ruby'
20
+ ```
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ Ensure you have an API key from Electionbuddy, and initialize the client as follows:
25
+
26
+ ```ruby
27
+ client = ElectionBuddy::Client.new('your-api-key')
28
+ ```
29
+
30
+ ### Voter List Validation
31
+
32
+ To validate a voter list, use the `voter_list.validate(vote_id)` method:
33
+
34
+ ```ruby
35
+ validation = client.voter_list.validate(1)
36
+
37
+ if validation.done?
38
+ puts "Validation completed successfully! Identifier: #{validation.identifier}"
39
+ # "Validation completed successfully! Identifier: ae0a1724-9791-4bb2-8331-6d4e55a9b7c8"
40
+ else
41
+ puts "Validation failed: #{validation.error}"
42
+ # "Validation failed: Vote: not found"
43
+ end
44
+ ```
45
+
46
+ ### Possible Errors
47
+
48
+ The following errors may be raised by the API:
49
+
50
+ - **400**: Malformed request.
51
+ - **401**: Invalid authentication credentials.
52
+ - **403**: Unauthorized.
53
+ - **404**: Resource not found.
54
+ - **429**: Your request exceeded the API rate limit.
55
+ - **500**: We were unable to perform the request due to server-side problems.
56
+
57
+ Each error will raise an `Error` exception with a message detailing the status code and the error message returned by the API.
22
58
 
23
59
  ## Development
24
60
 
@@ -28,7 +64,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
64
 
29
65
  ## Contributing
30
66
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/electionbuddy-ruby.
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/electionbuddy/electionbuddy-ruby.
32
68
 
33
69
  ## License
34
70
 
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class Client
5
+ BASE_URL = "https://secure.electionbuddy.com/api/v2"
6
+
7
+ def initialize(api_key, adapter: Faraday.default_adapter, stubs: nil)
8
+ @api_key = api_key
9
+ @adapter = adapter
10
+ @stubs = stubs
11
+ end
12
+
13
+ def voter_list
14
+ VoterListResource.new(connection)
15
+ end
16
+
17
+ private
18
+
19
+ def connection
20
+ @connection ||= Faraday.new(BASE_URL) do |conn|
21
+ conn.headers["authorization"] = @api_key
22
+ conn.request :json
23
+ conn.response :json, content_type: /\bjson$/
24
+ conn.adapter @adapter, @stubs
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class Validation
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def identifier
10
+ @response["validation_identifier"] || "Not available"
11
+ end
12
+
13
+ def done?
14
+ @response["error"].nil?
15
+ end
16
+
17
+ def error
18
+ return nil if done?
19
+
20
+ ErrorFormatter.format(@response["error"])
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class ErrorFormatter
5
+ def self.format(error_hash)
6
+ return if error_hash.nil? || error_hash.empty?
7
+
8
+ error_hash.map do |key, value|
9
+ formatted_key = key.gsub("_", " ").split.map(&:capitalize).join(" ")
10
+ formatted_value = value.is_a?(Array) ? value.join(", ") : value
11
+ "#{formatted_key}: #{formatted_value}"
12
+ end.join(", ")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class Resource
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def get_request(url, params = {}, headers = {}, &block)
10
+ handle_response(@connection.get(url, params, headers, &block))
11
+ end
12
+
13
+ def post_request(url, body = {}, headers = {}, &block)
14
+ handle_response(@connection.post(url, body, headers, &block))
15
+ end
16
+
17
+ private
18
+
19
+ def handle_response(response)
20
+ return response.body if response.success? || response.status == 422
21
+
22
+ raise_error(response.status, ErrorFormatter.format(response.body))
23
+ end
24
+
25
+ def raise_error(status, formatted_error)
26
+ base_message = error_messages(status)
27
+ message = if formatted_error
28
+ "Error #{status}: #{base_message} - #{formatted_error}."
29
+ else
30
+ "Error #{status}: #{base_message}."
31
+ end
32
+
33
+ raise Error, message
34
+ end
35
+
36
+ def error_messages(status)
37
+ messages = {
38
+ 400 => "Malformed request",
39
+ 401 => "Invalid authentication credentials",
40
+ 403 => "Unauthorized",
41
+ 404 => "Resource not found",
42
+ 429 => "Your request exceeded the API rate limit",
43
+ 500 => "We were unable to perform the request due to server-side problems"
44
+ }
45
+ messages.fetch(status, "Unexpected status code.")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ class VoterListResource < Resource
5
+ def validate(vote_id)
6
+ response = post_request("/api/v2/votes/voters/validations", vote_id: vote_id.to_i)
7
+
8
+ Validation.new(response)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElectionBuddy
4
+ VERSION = "0.2.0"
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/middleware"
5
+ require "json"
6
+
7
+ require "election_buddy/version"
8
+ require "election_buddy/client"
9
+ require "election_buddy/error"
10
+ require "election_buddy/resource"
11
+ require "election_buddy/resources/voter_list_resource"
12
+ require "election_buddy/entities/validation"
13
+ require "election_buddy/error_formatter"
14
+
15
+ module ElectionBuddy
16
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "election_buddy"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: electionbuddy-ruby
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
  - Alberto Rocha
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-10-29 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2024-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.12'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.12'
14
28
  description: This gem provides a Ruby SDK for easy interaction with the ElectionBuddy
15
29
  API
16
30
  email:
@@ -25,8 +39,15 @@ files:
25
39
  - LICENSE.txt
26
40
  - README.md
27
41
  - Rakefile
28
- - lib/electionbuddy/ruby.rb
29
- - lib/electionbuddy/ruby/version.rb
42
+ - lib/election_buddy.rb
43
+ - lib/election_buddy/client.rb
44
+ - lib/election_buddy/entities/validation.rb
45
+ - lib/election_buddy/error.rb
46
+ - lib/election_buddy/error_formatter.rb
47
+ - lib/election_buddy/resource.rb
48
+ - lib/election_buddy/resources/voter_list_resource.rb
49
+ - lib/election_buddy/version.rb
50
+ - lib/electionbuddy-ruby.rb
30
51
  - sig/electionbuddy/ruby.rbs
31
52
  homepage: https://github.com/electionbuddy/electionbuddy-ruby
32
53
  licenses:
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Electionbuddy
4
- module Ruby
5
- VERSION = "0.1.0"
6
- end
7
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "ruby/version"
4
-
5
- module Electionbuddy
6
- module Ruby
7
- class Error < StandardError; end
8
- # Your code goes here...
9
- end
10
- end