Class: Flow::Cli::Utils::FlowApiManager

Inherits:
Object
  • Object
show all
Defined in:
lib/flow/cli/utils/api/flow_api_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) {|_self| ... } ⇒ FlowApiManager

Returns a new instance of FlowApiManager.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 9

def initialize(hash = {})
  %i[email password user_access_token].each do |item|
    send "#{item}=", hash[item.to_s]
  end
  yield self if block_given?
end

Instance Attribute Details

#current_flow_idObject

Returns the value of attribute current_flow_id.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def current_flow_id
  @current_flow_id
end

#current_org_idObject

Returns the value of attribute current_org_id.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def current_org_id
  @current_org_id
end

#current_project_idObject

Returns the value of attribute current_project_id.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def current_project_id
  @current_project_id
end

#current_project_nameObject

Returns the value of attribute current_project_name.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def current_project_name
  @current_project_name
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def email
  @email
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def password
  @password
end

#user_access_tokenObject

Returns the value of attribute user_access_token.



5
6
7
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 5

def user_access_token
  @user_access_token
end

Class Method Details

.load_from_dbObject



133
134
135
136
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 133

def load_from_db
  dict = DbManager.read
  new(dict)
end

.login(email, password) ⇒ Object



127
128
129
130
131
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 127

def (email, password)
  dict = FlowApiRest.post("/login", login: email, password: password)
  DbManager.save(email: email, password: password, user_access_token: dict[:access_token])
  { email: email, password: password, user_access_token: dict[:access_token] }
end

Instance Method Details

#delete_p12(p12_id, flow_id) ⇒ Object



52
53
54
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 52

def delete_p12(p12_id, flow_id)
  send_to_api(:delete, "/certificates/#{p12_id}", flow_id: flow_id)
end

#delete_provision(mobileprovisions_id, flow_id) ⇒ Object



66
67
68
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 66

def delete_provision(mobileprovisions_id, flow_id)
  send_to_api(:delete, "/mobileprovisions/#{mobileprovisions_id}", flow_id: flow_id)
end

#fetch_flow(flow_id, project_id) ⇒ Object



70
71
72
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 70

def fetch_flow(flow_id, project_id)
  send_to_api(:get, "/flows/#{flow_id}", project_id: project_id)
end

#fetch_flows(project_id) ⇒ Object



36
37
38
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 36

def fetch_flows(project_id)
  send_to_api(:get, "/projects/#{project_id}/flows")
end

#fetch_latest_jobs(flow_id, project_id) ⇒ Object



74
75
76
77
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 74

def fetch_latest_jobs(flow_id, project_id)
  answer = send_to_api(:get, "/projects/#{project_id}/jobs", flow_id: flow_id)
  answer[:list] || []
end

#fetch_orgsObject



20
21
22
23
24
25
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 20

def fetch_orgs
  raw_orgs = FlowApiRest.get("/orgs", access_token: user_access_token)
  raw_orgs.map do |org|
    org.slice(:id, :name)
  end
end

#fetch_project(project_id) ⇒ Object



32
33
34
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 32

def fetch_project(project_id)
  send_to_apapi_manageri(:get, "/projects/#{project_id}")
end

#fetch_projects(specify_org_id = nil) ⇒ Object



27
28
29
30
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 27

def fetch_projects(specify_org_id = nil)
  org_id = specify_org_id || current_org_id
  send_to_api(:get, "/projects", { org_id: org_id }, %i[id name git_url source])
end

#fetch_userObject



16
17
18
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 16

def fetch_user
  send_to_api("get", "/user")
end

#init_access_tokenObject



96
97
98
99
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 96

def init_access_token
  answer = self.class.(email, password)
  self.user_access_token = answer[:access_token]
end

#load_p12s(flow_id) ⇒ Object



48
49
50
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 48

def load_p12s(flow_id)
  send_to_api(:get, "/flows/#{flow_id}/certificates")
end

#load_provisions(flow_id) ⇒ Object



62
63
64
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 62

def load_provisions(flow_id)
  send_to_api(:get, "/flows/#{flow_id}/mobileprovisions")
end

#login(email, password) ⇒ Object



114
115
116
117
118
119
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 114

def (email, password)
  hash = self.class.(email, password)
  %i[email password user_access_token].each do |item|
    send "#{item}=", hash[item]
  end
end

#refresh_login(&proc) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 101

def (&proc)
  fetch_user
rescue FlowApiError
  puts "login fail, relogin..."
  tmp_email = nil
  tmp_password = nil
  tmp_email, tmp_password = yield unless proc.nil?
  self.email = tmp_email || email
  self.password = tmp_password || password
  (email, password)
  self
end

#run_manual_job(flow_id, project_id, branch) ⇒ Object



79
80
81
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 79

def run_manual_job(flow_id, project_id, branch)
  send_to_api(:post, "/projects/#{project_id}/manual_hook", flow_id: flow_id, branch: branch)
end

#send_to_api(action, url, params = {}, slice_items = nil, need_access_token = true) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 84

def send_to_api(action, url, params = {}, slice_items = nil, need_access_token = true)
  params[:access_token] = user_access_token if need_access_token
  params.compact!
  raw_answer = FlowApiRest.send(action, url, params)

  return raw_answer if slice_items.nil?
  raise "slice need be a array with symbols" unless slice_items.is_a? Array

  return raw_answer.map { |item| item.slice(*slice_items) } if raw_answer.is_a? Array
  raw_answer.slice(*slice_items)
end

#standard_file(file) ⇒ Object



121
122
123
124
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 121

def standard_file(file)
  return File.open(file) if file.is_a?(String)
  file
end

#upload_p12(flow_id, file, password = nil) ⇒ Object

5909e8c4ef2cb07bcefc3dbd



41
42
43
44
45
46
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 41

def upload_p12(flow_id, file, password = nil)
  send_to_api(:post, "/flows/#{flow_id}/certificates",
              file: standard_file(file),
              type: "ios",
              password: password)
end

#upload_provision(flow_id, file) ⇒ Object



56
57
58
59
60
# File 'lib/flow/cli/utils/api/flow_api_manager.rb', line 56

def upload_provision(flow_id, file)
  send_to_api(:post, "/flows/#{flow_id}/mobileprovisions",
              file: standard_file(file),
              flow_id: flow_id)
end