Module: Wassup::Helpers::Shortcut

Defined in:
lib/wassup/helpers/shortcut.rb,
lib/wassup/helpers/shortcut.rb

Defined Under Namespace

Modules: Formatter

Class Method Summary collapse

Class Method Details

.membersObject



7
8
9
10
11
12
13
14
15
# File 'lib/wassup/helpers/shortcut.rb', line 7

def self.members
  endpoint = "https://api.app.shortcut.com"
  resp = RestClient::Request.execute(
    method: :get, 
    url: "#{endpoint}/api/v3/members",
    headers: { "Shortcut-Token": ENV['WASSUP_SHORTCUT_TOKEN'], "Content-Type": "application/json" }
  )
  return JSON.parse(resp)
end

.search_stories(query:, page_size: 25) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wassup/helpers/shortcut.rb', line 27

def self.search_stories(query:, page_size: 25)
  endpoint = "https://api.app.shortcut.com"

  members = self.members
  workflows = self.workflows

  stories = []

  resp = RestClient::Request.execute(
    method: :get, 
    url: "#{endpoint}/api/v3/search/stories",
    payload: { page_size: page_size, query: query },
    headers: { "Shortcut-Token": ENV['WASSUP_SHORTCUT_TOKEN'], "Content-Type": "application/json" }
  )
  json = JSON.parse(resp)
  stories += json["data"]

  next_url = json["next"]
  while !next_url.nil?
    resp = RestClient::Request.execute(
      method: :get, 
      url: "#{endpoint}#{next_url}",
      headers: { "Shortcut-Token": ENV['WASSUP_SHORTCUT_TOKEN'], "Content-Type": "application/json" }
    )
    json = JSON.parse(resp)
    stories += json["data"]
    next_url = json["next"]
  end

  stories = stories.map do |story|
    story["followers"] = story["follower_ids"].map do |owner_id|
      members.find do |member|
        member["id"] == owner_id
      end 
    end

    story["owners"] = story["owner_ids"].map do |owner_id|
      members.find do |member|
        member["id"] == owner_id
      end 
    end

    if (workflow_id = story["workflow_id"]) && (workflow_state_id = story["workflow_state_id"])
      workflow = workflows.find do |workflow|
        workflow["id"] == workflow_id
      end

      story["workflow"] = workflow
      if workflow
        story["workflow_state"] = workflow["states"].find do |workflow_state|
          workflow_state["id"] == workflow_state_id
        end
      end
    end

    story
  end

  return stories
end

.workflowsObject



17
18
19
20
21
22
23
24
25
# File 'lib/wassup/helpers/shortcut.rb', line 17

def self.workflows
  endpoint = "https://api.app.shortcut.com"
  resp = RestClient::Request.execute(
    method: :get, 
    url: "#{endpoint}/api/v3/workflows",
    headers: { "Shortcut-Token": ENV['WASSUP_SHORTCUT_TOKEN'], "Content-Type": "application/json" }
  )
  return JSON.parse(resp)
end