百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

remotely

> 编程语言
开源

远程 API 模型关联。

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

远程 API 模型关联。

Remotely

Remotely lets you specify associations for your models that should be fetched from a remote API instead of the database.

Configuration

Apps are where Remotely goes to find association resources. You can define as many as you want, but if you define only one, you can omit the :app option from your associations. Remotely also supports any Faraday middleware, to configure just call use_middleware with the class name and options.

ruby
Remotely.configure do
  app :legsapp do
    url "http://somanylegs.com/api/v1"
    basic_auth "username", "password"
    use_middleware Faraday::HttpCache, store: MyCache.new
  end
end

class Millepied  "/legs"
end

One app & custom path

ruby
class Millepied  "/store/legs"
end

One app & custom path with id substitution

ruby
class Millepied  "/millepieds/:id/legs"
end

Multiple apps (all secondary conditions from above apply)

ruby
class Millepied  :legsapp, ...
end

Note about associations

If you do not specify the :app options in your associations, you need to create a Remotely::Model subclass for the associated object. This is so Remotely knows which app to use to retrieve entries of that type.

ruby
class Person  "/millepieds/:id/legs"
end

m = Millepied.new
m.id   # => 1
m.legs # => Requests "/millepieds/1/legs"

Authorization

Remotely is setup to allow basic auth, token auth, or custom authentication schemes.

Basic Auth

basic_auth accepts 2 String params: username and password

ruby
# 'Authorization' header => "Basic dXNlcjpwYXNzd29yZA=="

Remotely.configure do
  app :legsapp do
    url "http://somanylegs.com/api/v1"
    basic_auth "username", "password"
  end
end

Token Auth

token_auth accepts a String param for the token, and an optional Hash of token options

ruby
# 'Authorization' header => "Token token=\"abcdef\", foo=\"bar\""

Remotely.configure do
  app :legsapp do
    url "http://somanylegs.com/api/v1"
    token_auth "abcdef", {:foo => 'bar'}
  end
end

Custom Authorization

authorization accepts a String param for the type, and either a String or Hash token. A String value is taken literally, and a Hash is encoded into comma separated key/value pairs.

ruby
# 'Authorization' header => "Bearer mF_9.B5f-4.1JqM"

Remotely.configure do
  app :legsapp do
    url "http://somanylegs.com/api/v1"
    authorization 'Bearer', 'mF_9.B5f-4.1JqM'
  end
end

# 'Authorization' header => "OAuth token=\"abcdef\", foo=\"bar\""

Remotely.configure do
  app :legsapp do
    url "http://somanylegs.com/api/v1"
    authorization 'OAuth', {:token => 'abcdef', :foo => 'bar'}
  end
end

Fetched Objects

Remote associations are Remotely::Model objects. Whatever data the API returns, becomes the attributes of the Model.

ruby
m = Millepied.new
m.legs[0]         # => #"1mm"}>
m.legs[0].length  # => "1mm"

Fetched Object Associations

If a fetched object includes an attribute matching "*_id", Remotely tries to find the model it is for and retrieve it.

ruby
leg = m.legs.first
leg.user_id # => 2
leg.user    # => User.find(2)

Contributing

Fork, branch and pull-request. Bump versions in their own commit.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言