class Octadesk::Api
Attributes
user_token[RW]
Public Class Methods
new(args={})
click to toggle source
Aqui nós recebemos a chave da Api
quando o usuário inicializa a nossa classe
# File lib/octadesk.rb, line 11 def initialize(args={}) @user_email = args[:user_email] @api_token = args[:api_token] raise ArgumentError if (args[:user_email] == nil or args[:user_email] == "") and (args[:api_token] == nil or args[:api_token] == "") if args[:endpoint_url] @endpoint_url = args[:endpoint_url] else @endpoint_url = 'https://api.octadesk.services' end @user_token = get_token(@user_email).body['token'] #raise ArgumentError if @token == false end
Public Instance Methods
delete_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 117 def delete_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.delete("#{@endpoint_url}#{action}", params)) rescue => e parse_response('object', e.response) end end
get_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 27 def get_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.get("#{@endpoint_url}#{action}", {params: params}.merge(headers))) rescue => e parse_response('object', e.response) end end
head_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 99 def head_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.head("#{@endpoint_url}#{action}", params)) rescue => e parse_response('object', e.response) end end
patch_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 81 def patch_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.patch("#{@endpoint_url}#{action}", params, headers)) rescue => e parse_response('object', e.response) end end
post_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 45 def post_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.post("#{@endpoint_url}#{action}", params, headers)) rescue => e parse_response('object', e.response) end end
put_request(action, params={}, headers={})
click to toggle source
# File lib/octadesk.rb, line 63 def put_request(action, params={}, headers={}) begin headers = headers.merge({ 'accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@user_token}" }) api_response_kind = headers.delete('api_response_kind') api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil? api_response_kind = 'object' if api_response_kind.nil? parse_response(api_response_kind, RestClient.put("#{@endpoint_url}#{action}", params, headers)) rescue => e parse_response('object', e.response) end end
Private Instance Methods
get_token(user_email)
click to toggle source
# File lib/octadesk.rb, line 137 def get_token(user_email) header = { 'accept' => 'application/json', 'apiToken' => @api_token, 'username' => "#{user_email}".downcase } request = post_request("/login/apiToken", {}, header ) request end
parse_response(api_response_kind, response)
click to toggle source
# File lib/octadesk.rb, line 147 def parse_response(api_response_kind, response) result = OpenStruct.new result.status_code = response.code if api_response_kind == 'object' result.headers = (JSON.parse(response.headers.to_json, object_class: OpenStruct) rescue response.headers) result.body = (JSON.parse(response.body, object_class: OpenStruct) rescue response.body) elsif api_response_kind == 'hash' result.headers = response.headers result.body = (JSON.parse(response.body) rescue response.body) else result.headers = response.headers result.body = response.body end @last_result = result result end