Class: DroneApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/drone_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



48
49
50
51
52
53
54
55
56
57
# File 'lib/drone_api.rb', line 48

def initialize
  @client_secret = DroneApi.configuration&.client_secret
  @client_id = DroneApi.configuration&.client_id
  @account_id = DroneApi.configuration&.
  @drone_env = DroneApi.configuration&.env

  raise DroneApi::ConfigurationError if !valid_for_execution

  @errors = Hash.new {|h,k| h[k] = Array.new }
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



46
47
48
# File 'lib/drone_api.rb', line 46

def 
  @account_id
end

#client_idObject (readonly)

Returns the value of attribute client_id.



46
47
48
# File 'lib/drone_api.rb', line 46

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



46
47
48
# File 'lib/drone_api.rb', line 46

def client_secret
  @client_secret
end

#response_dataObject (readonly)

Returns the value of attribute response_data.



46
47
48
# File 'lib/drone_api.rb', line 46

def response_data
  @response_data
end

#response_statusObject (readonly)

Returns the value of attribute response_status.



46
47
48
# File 'lib/drone_api.rb', line 46

def response_status
  @response_status
end

Instance Method Details

#api_pathObject



155
156
157
# File 'lib/drone_api.rb', line 155

def api_path
  raise 'Define api path of API in base class'
end

#env_urlObject



171
172
173
174
175
176
177
178
179
# File 'lib/drone_api.rb', line 171

def env_url
  if @drone_env == 'production'
    return 'http://192.168.33.10:81'
  elsif @drone_env == 'staging'
    return 'http://drone.stg.ccubeapp.com'
  else
    return 'http://localhost:3001'
  end
end

#errorsObject



75
76
77
# File 'lib/drone_api.rb', line 75

def errors
  @errors ||= {}
end

#executeObject



120
121
122
123
124
125
# File 'lib/drone_api.rb', line 120

def execute
  validate
  if valid?
    make_request
  end
end

#is_form_request?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/drone_api.rb', line 66

def is_form_request?
  false
end

#make_requestObject



130
131
132
133
134
135
# File 'lib/drone_api.rb', line 130

def make_request
  request(api_path, request_type) do |http, request|
    request.body = parse_payload if payload.any?
    process_request http, request
  end
end

#parse_payloadObject



163
164
165
166
167
168
169
# File 'lib/drone_api.rb', line 163

def parse_payload
  if is_form_request?
    return URI.encode_www_form(payload)
  else
    return payload.to_json
  end
end

#process_request(http, request) ⇒ Object

NOTE: raise configuration error if token is invalid Token is stored in DroneApi.configuration.current_token which can expire So once its expire we’ll rase authication error to let application handle it Refresh token using DroneApi.generate_token if this error is raised



142
143
144
145
146
147
148
149
# File 'lib/drone_api.rb', line 142

def process_request http, request
  response = http.request(request)
  @response_status = response.code.to_i

  raise DroneApi::AuthError if @response_status == 401

  process_response(response.body) if response.body 
end

#query_paramsObject



159
160
161
# File 'lib/drone_api.rb', line 159

def query_params
  {}
end

#request(path, request_type) {|http, request| ... } ⇒ Object

def success_hash

{}

end

Yields:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/drone_api.rb', line 91

def request path, request_type, &blck
  url = env_url+path
  url = URI(url)
  url.query = URI.encode_www_form(query_params) if query_params.any?
  http = Net::HTTP.new(url.host, url.port)
  # http.use_ssl = true
  # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  case request_type 
  when 'GET'
    request = Net::HTTP::Get.new(url)
  when 'POST'
    request = Net::HTTP::Post.new(url)
  when 'PUT'
    request = Net::HTTP::Put.new(url)
  when 'DELETE'
    request = Net::HTTP::Delete.new(url)
  else
    raise 'Invalid request type'
  end

  set_auth_header(request)
  if is_form_request?
    request["content-type"] = 'application/x-www-form-urlencoded'
  else
    request["content-type"] = 'application/json'
  end
  yield(http, request)
end

#request_typeObject



151
152
153
# File 'lib/drone_api.rb', line 151

def request_type
  raise 'Define request type in base class'
end

#requires_authentication?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/drone_api.rb', line 181

def requires_authentication?
  true
end

#responseObject



70
71
72
73
# File 'lib/drone_api.rb', line 70

def response
  execute
  return self
end

#set_auth_header(request) ⇒ Object



59
60
61
62
63
64
# File 'lib/drone_api.rb', line 59

def set_auth_header(request)
  if requires_authentication?
    DroneApi.generate_token if DroneApi.configuration.current_token.to_s.empty?
    request["Auth-Token"] = DroneApi.configuration.current_token
  end
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/drone_api.rb', line 79

def valid?
  errors.empty?
end

#validateObject



127
128
# File 'lib/drone_api.rb', line 127

def validate
end