Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ecfr

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ecfr

  • 1.1.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Ecfr

Provides a Ruby API client for the eCFR.gov APIs. Not all services and API endpoints in this gem are publically available. See https://www.ecfr.gov/developers/documentation/api/v1 for public endpoint documentation.

Installation

Add this line to your application's Gemfile:

gem 'ecfr'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ecfr

Usage

Configuration is supported via a configuration block. Only user_agent is required to be configured. See lib/ecfr/configuration.rb for options.

Ecfr.configure do |config|
  config.user_agent = "MyProject(me@myproject.com)"
end

XML parsing

Some endpoints return XML (like the Structure) and will be automatically parsed using Nokogiri. However we do not bundle this as a gem dependency since it may not always be used. To use this you will need to add the Nokogiri gem to your code.

Response caching

Response caching can be enabled via a configuration option. This is a performance feature that allows the calling code to not worry about performance when different parts of the application call the same endpoints at different times in the same request. Responses are cached my HTTP method, api endpoint and requested parameters. You will need to install the request_store gem when enabling this option.

If you are using request store outside of a Rails project or in a background process (eg Sidekiq) check the RequestStore documentation for how to use the required middleware.

Parallel client

A basic parallel client is provided to support some use cases (we use it internally for our status page). This requires the use of faraday-typhoeus adapter. However we do not bundle this as a gem dependency since it may not always be used. To use the parallel client you will need to add the typhoeus faraday adapter gem to your code.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

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.

Simple endpoints that return an array of data under a single key

For endpoints that return results like:

{
  items: [
    {name: "Item 1", active: true},
    {name: "Item 2", active: true},
    {name: "Item 3", active: false}
  ]
}

We support a result_key in the definition. In this example result_key: :items. This will cause each item in the items array to be instantiated as an instance of the class and delegate enumerable methods such as .each, .first, .last.

So the following definition:

class Items < Base
  result_key: :items

  attribute :name
  attribute :active, type: boolean

  def self.all
    new(
      get(...)
    )
  end
end

could be used Items.all.each which would iterate through an array of Item instances with name and active (and active?) methods.

More complex endpoints

If however the endpoint returns data more like:

{
  items: [
    {name: "Item 1", active: true},
    {name: "Item 2", active: true},
    {name: "Item 3", active: false}
  ],
  warehouse: "Warehouse 32",
  dock: "Dock A"
}

then we support the following definition:

class Items < Base
  class Item
    include AttributeMethodDefinition

    attribute :name
    attribute :active, type: boolean
  end

  attribute: :items, type: Array(Item)
  attribute: :dock, :warehouse

  def self.all
    new(
      get(...)
    )
  end
end

which can then be used like result = Items.all; result.items.each...; result.warehouse.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/criticaljuncture/ecfr.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 10 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc