Class: DroneApi::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/drone_api.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
46
47
48
|
# File 'lib/drone_api.rb', line 46
def account_id
@account_id
end
|
#client_id ⇒ Object
Returns the value of attribute client_id.
46
47
48
|
# File 'lib/drone_api.rb', line 46
def client_id
@client_id
end
|
#client_secret ⇒ Object
Returns the value of attribute client_secret.
46
47
48
|
# File 'lib/drone_api.rb', line 46
def client_secret
@client_secret
end
|
#response_data ⇒ Object
Returns the value of attribute response_data.
46
47
48
|
# File 'lib/drone_api.rb', line 46
def response_data
@response_data
end
|
#response_status ⇒ Object
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_path ⇒ Object
155
156
157
|
# File 'lib/drone_api.rb', line 155
def api_path
raise 'Define api path of API in base class'
end
|
#env_url ⇒ Object
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
|
#errors ⇒ Object
75
76
77
|
# File 'lib/drone_api.rb', line 75
def errors
@errors ||= {}
end
|
#execute ⇒ Object
120
121
122
123
124
125
|
# File 'lib/drone_api.rb', line 120
def execute
validate
if valid?
make_request
end
end
|
66
67
68
|
# File 'lib/drone_api.rb', line 66
def is_form_request?
false
end
|
#make_request ⇒ Object
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_payload ⇒ Object
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_params ⇒ Object
159
160
161
|
# File 'lib/drone_api.rb', line 159
def query_params
{}
end
|
#request(path, request_type) {|http, request| ... } ⇒ Object
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)
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
(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_type ⇒ Object
151
152
153
|
# File 'lib/drone_api.rb', line 151
def request_type
raise 'Define request type in base class'
end
|
#requires_authentication? ⇒ Boolean
181
182
183
|
# File 'lib/drone_api.rb', line 181
def requires_authentication?
true
end
|
#response ⇒ Object
70
71
72
73
|
# File 'lib/drone_api.rb', line 70
def response
execute
return self
end
|
#valid? ⇒ Boolean
79
80
81
|
# File 'lib/drone_api.rb', line 79
def valid?
errors.empty?
end
|
#validate ⇒ Object
127
128
|
# File 'lib/drone_api.rb', line 127
def validate
end
|