【问题标题】:Using the ruby-trello gem, how can I determine whether a board is public or not?使用 ruby​​-trello gem,我如何确定一个板子是否是公开的?
【发布时间】:2018-12-01 10:55:45
【问题描述】:

我正在使用ruby-trello gem 访问Trello API

我试过了:

> board = Trello::Member.find('me').boards.first
> board.prefs['permissionLevel']
=> "org"
> board.organization.prefs
Traceback (most recent call last):
        1: from (irb):20
NoMethodError (undefined method `prefs' for #<Trello::Organization:0x0000562900d7bb88>)

我这样做对吗?

【问题讨论】:

标签: ruby trello


【解决方案1】:

this answer 为基础,这里有一个解决方法:

def org_public?(org)
  # If org comes from a board, it may be nil. We'll skip the philosophical conundrums
  # and assume an inaccessible or non-existent board is not public.
  return false if org.nil?

  path = "/organizations/#{org.id}/prefs"
  response = org.client.get(path)
  JSON.parse(response.body)['permissionLevel']
end

def board_public?(board)
  # Sometimes board.organization will return a 401 error when there is a board. 
  # Maybe it's been deleted?
  org = board.organization rescue nil

  # If board is public, sure
  return true if board.prefs['permissionLevel'] == 'public' 

  # If board permission tied to org
  return true if board.prefs['permissionLevel'] == 'org' && org_public?(org)

  # Any other cases I'm missing?
  false 
end

> board = Trello::Member.find('me').boards.first
> board_public?(board)
=> true
> board = Trello::Member.find('me').boards[1]
> board_public?(board)
=> false

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多