Class: MyAsk::Api::ProjectHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/myask/api/project_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(global_options, options, args) ⇒ ProjectHelper

Returns a new instance of ProjectHelper.



5
6
7
8
9
10
11
12
# File 'lib/myask/api/project_helper.rb', line 5

def initialize(global_options, options, args)
  @global_options = global_options
  @options = options
  @args = args

  @api_host = global_options[:api_host]
  @api_key = global_options[:api_key]
end

Instance Method Details

#project_id_from_selectionObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/myask/api/project_helper.rb', line 14

def project_id_from_selection
  uri = URI("#{@api_host}/api/projects")
  response = MyAsk::Api::Helper.make_request('Get', uri, @api_key)

  projects = JSON.parse(response.body)
  if projects.empty?
    puts 'No projects found.'
    exit
  end

  select_project(projects)
end

#select_project(projects) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/myask/api/project_helper.rb', line 27

def select_project(projects)
  prompt = TTY::Prompt.new
  selected_project = prompt.select(
    "Select MyAsk Project:",
    projects.map { |project| {"#{project["name"]}": project["id"]} }
  )
end

#show_project_info(project_id) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/myask/api/project_helper.rb', line 35

def show_project_info(project_id)
  uri = URI("#{@api_host}/api/projects/#{project_id}")
  response = MyAsk::Api::Helper.make_request('Get', uri, @api_key)

  project_info = JSON.parse(response.body)
  puts JSON.pretty_generate(project_info.except("question_ids"))
  project_info.except("question_ids")
end