“ Her is an ORM (Object Relational Mapper) that maps REST resources to Ruby objects. It is designed to build applications that are powered by a RESTful API instead of a database.
require "open-uri" class GitHub def self.fetch_repository(repo) begin res = open("https://api.github.com/repos/#{repo}") rescue return nil end Hashie::Mash.construct(JSON.parse(res.read)) end end @repository = GitHub.fetch_repository("remiprev/her") @repository.url # => "http://remiprev.github.com/her"
class GitHub include HTTParty base_uri "https://api.github.com" def self.fetch_repository(repo) response = get("/repos/#{repo}") if response.success? Hashie::Mash.construct(JSON.parse(response.body)) else raise "Repository Not Found" end end end @repository = GitHub.fetch_repository("remiprev/her") @repository.url # => "http://remiprev.github.com/her"
Her::API.setup :url => "https://api.github.com" do |c| c.use Her::Middleware::DefaultParseJSON c.use Faraday::Adapter::NetHttp end class Repo include Her::Model end @repository = Repo.find("remiprev/her") @repository.url # => "http://remiprev.github.com/her"