Using the ruby-trello gem, how can I determine whether a board is public or not?

大憨熊 提交于 2019-12-13 08:35:39

问题


I'm using the ruby-trello gem to access the Trello API.

I tried this:

> 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>)

Am I going about this the right way?


回答1:


Building off this answer, here's a workaround:

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


来源:https://stackoverflow.com/questions/50979870/using-the-ruby-trello-gem-how-can-i-determine-whether-a-board-is-public-or-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!