在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:codeforamerica/ohanakapa-ruby开源软件地址:https://github.com/codeforamerica/ohanakapa-ruby开源编程语言:Ruby 53.5%开源软件介绍:OhanakapaA ruby wrapper for the Ohana API Quick startInstall via Rubygems
... or add to your Gemfile
Example appPrefer to learn from live implementations? We built a website that consumes the Ohana API via this wrapper. The code is open source in this repo. Making requestsAPI methods are available as module methods (consuming module-level configuration) or as client instance methods. # Provide an API Token
Ohanakapa.configure do |c|
c.api_token = 'your_token'
end
# Fetch all locations
Ohanakapa.locations or # Provide an API Token
client = Ohanakapa::Client.new :api_token => 'your_token'
# Fetch all locations
client.locations A token will automatically be generated for you when you register an Ohana API app. Consuming resourcesMost methods return a # Fetch a location
location = Ohanakapa.location '1'
puts location.name
# => "Center on Homelessness"
puts location.fields
# => <Set: {:id, :accessibility, :address, :ask_for, :contacts, :coordinates, :description, :emails, :faxes, :languages, :mail_address, :name, :phones, :short_desc, :transportation, :updated_at, :urls, :url, :services, :organization, :other_locations}>
puts location[:address]
# => {
street: "472 Harbor Blvd.",
city: "Belmont",
state: "CA",
zip: "94002"
} Searching# Search for food in ZIP code 94403
Ohanakapa.search("search", :keyword => "food", :location => "94403")
# Find all parks in the database
Ohanakapa.search("search", :kind => "Parks")
# Find all farmers' markets that participate in Market Match
Ohanakapa.search("search", :kind => "Farmers' Markets", :market_match => "1") See the API search docs for all the possible parameters. Note: URL fields are culled into a separate # Fetch an organization
org = Ohanakapa.organization '2'
org.rels[:locations].href
# => "http://ohana-api-demo.herokuapp.com/api/organizations/2/locations" Accessing HTTP responsesWhile most methods return a location = Ohanakapa.location '2'
response = Ohanakapa.last_response
etag = response.headers[:etag]
total_count = response.headers["X-Total-Count"] See the API docs for a full list of custom response headers. AuthenticationOhanakapa supports the API Token method supported by the Ohana API: Application authenticationOhanakapa supports application-only authentication using API Token application client credentials. Using application credentials will result in making API calls on behalf of an application in order to take advantage of the higher rate limit. client = Ohanakapa::Client.new(:api_token => "<your 32 char token>")
location = client.locations '2' Configuration and defaultsWhile Hypermedia agentOhanakapa is hypermedia-enabled. Under the hood, {Ohanakapa::Client} uses Sawyer, a hypermedia client built on Faraday. Hypermedia in OhanakapaResources returned by Ohanakapa methods contain not only data but hypermedia link relations: org = Ohanakapa.organization '2'
# Get the locations rel, returned from the API
# as locations_url in the resource
org.rels[:locations].href
# => "http://ohana-api-demo.herokuapp.com/api/organizations/2/locations"
locations = org.rels[:locations].get.data
locations.last.name
# => "Vocational Rehabilitation Services (VRS) Workcenter" When processing API responses, all Advanced usageSince Ohanakapa employs Faraday under the hood, some behavior can be extended via middleware. DebuggingOften, it helps to know what Ohanakapa is doing under the hood. Faraday makes it easy to peek into the underlying HTTP traffic: stack = Faraday::RackBuilder.new do |builder|
builder.response :logger
builder.use Ohanakapa::Response::RaiseError
builder.adapter Faraday.default_adapter
end
Ohanakapa.middleware = stack
Ohanakapa.location '2'
See the Faraday README for more middleware magic. CachingIf you want to boost performance, stretch your API rate limit, or avoid paying the hypermedia tax, you can use Faraday Http Cache. Add the gem to your Gemfile
Next, construct your own Faraday middleware. The example below assumes you are
using Memcache via # config/initializers/ohanakapa.rb
cache_store = ActiveSupport::Cache.lookup_store(:dalli_store)
stack = Faraday::RackBuilder.new do |builder|
builder.use Faraday::HttpCache, store: cache_store
builder.use Ohanakapa::Response::RaiseError
builder.adapter Faraday.default_adapter
end
Ohanakapa.middleware = stack Once configured, the middleware will store responses in cache based on ETag
fingerprint and serve those back up for future Hacking on OhanakapaIf you want to hack on Ohanakapa locally, we try to make bootstrapping the project as painless as possible. Just clone and run:
This will install project dependencies and get you up and running. If you want to run a Ruby console to poke on Ohanakapa, you can crank one up with:
Using the scripts in Running and writing new testsOhanakapa uses VCR for recording and playing back API fixtures during test
runs. These fixtures are part of the Git project in the Since we periodically refresh our cassettes, please keep some points in mind when writing new specs.
Submitting a Pull Request
Supported Ruby VersionsThis library aims to support and is tested against the following Ruby implementations:
If something doesn't work on one of these Ruby versions, it's a bug. This library may inadvertently work (or seem to work) on other Ruby implementations, however support will only be provided for the versions listed above. If you would like this library to support another Ruby version, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped. VersioningThis library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:
CopyrightCopyright (c) 2013 Code for America. See LICENSE for details. This wrapper is based on the excellent GitHub Ruby API wrapper, which is Copyright (c) 2009-2013 Wynn Netherland, Adam Stacoviak, Erik Michaels-Ober. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论