Class: Salesfly::REST
- Inherits:
-
Object
- Object
- Salesfly::REST
- Defined in:
- lib/salesfly/rest.rb
Instance Method Summary collapse
- #api_key ⇒ Object
- #delete(path, headers = {}) ⇒ Object
- #execute(method, path, data, headers = {}) ⇒ Object
- #get(path, headers = {}) ⇒ Object
-
#initialize(options = {}) ⇒ REST
constructor
A new instance of REST.
- #patch(path, data, headers = {}) ⇒ Object
- #post(path, data, headers = {}) ⇒ Object
- #put(path, data, headers = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ REST
Returns a new instance of REST.
4 5 6 7 8 9 |
# File 'lib/salesfly/rest.rb', line 4 def initialize( = {}) @api_key = [:api_key] @api_base_url = .fetch(:api_base_url) { "https://api.salesfly.com" } @timeout = .fetch(:timeout) { 30 } @user_agent = "salesfly-ruby/#{VERSION}" end |
Instance Method Details
#api_key ⇒ Object
11 12 13 14 15 16 |
# File 'lib/salesfly/rest.rb', line 11 def api_key if @api_key.to_s.empty? raise ArgumentError.new("No API key provided") end @api_key end |
#delete(path, headers = {}) ⇒ Object
34 35 36 |
# File 'lib/salesfly/rest.rb', line 34 def delete(path, headers={}) execute("DELETE", path, nil, headers) end |
#execute(method, path, data, headers = {}) ⇒ Object
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/salesfly/rest.rb', line 38 def execute(method, path, data, headers={}) std_headers = { "User-Agent" => @user_agent, "Authorization" => "Bearer #{api_key}", "Accept" => "application/json", "Content-Type" => "application/json" } headers = std_headers.merge(headers) begin RestClient::Request.execute(method: method, url: @api_base_url + path, payload: data, headers: headers, timeout: @timeout) { |response, request, result, &block| # print &block case response.code when 200, 201 if headers["Accept"] == "application/pdf" return response end body = JSON.parse(response) return body["data"] else begin err = JSON.parse(response) raise ResponseError.new(err["message"], response.code, err["code"]) rescue JSON::ParserError raise ResponseError.new(response.body, response.code) # code? rescue Timeout::Error, Net::ReadTimeout => e raise APITimeoutError.new(e.) rescue Errno::ECONNREFUSED, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError => e raise APIConnectionError.new(e.) end end } end # begin # case method # when "GET" # resp = self.class.get(path, base_uri: @api_base_url, timeout: @timeout, :headers => headers) # when "POST" # resp = self.class.post(path, base_uri: @api_base_url, timeout: @timeout, :headers => headers, :body => data, multipart: true) # when "PUT" # resp = self.class.put(path, base_uri: @api_base_url, timeout: @timeout, :headers => headers, :body => data) # when "PATCH" # resp = self.class.patch(path, base_uri: @api_base_url, timeout: @timeout, :headers => headers, :body => data) # when "DELETE" # resp = self.class.delete(path, base_uri: @api_base_url, timeout: @timeout, :headers => headers) # end # if [200,201].include?(resp.code) # result = JSON.parse(resp.body) #FIXME: Handle 204 No content # return result["data"] # else # begin # err = JSON.parse(resp.body) # raise ResponseError.new(err["message"], resp.code, err["code"]) # rescue JSON::ParserError # raise ResponseError.new(resp.message, resp.code) # code? # end # end # rescue Timeout::Error, Net::ReadTimeout => e # raise APITimeoutError.new(e.message) # rescue Errno::ECONNREFUSED, Errno::EINVAL, Errno::ECONNRESET, EOFError, # Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, # HTTParty::Error, SocketError => e # raise APIConnectionError.new(e.message) # end end |
#get(path, headers = {}) ⇒ Object
18 19 20 |
# File 'lib/salesfly/rest.rb', line 18 def get(path, headers={}) execute("GET", path, nil, headers) end |
#patch(path, data, headers = {}) ⇒ Object
30 31 32 |
# File 'lib/salesfly/rest.rb', line 30 def patch(path, data, headers={}) execute("PATCH", path, data, headers) end |
#post(path, data, headers = {}) ⇒ Object
22 23 24 |
# File 'lib/salesfly/rest.rb', line 22 def post(path, data, headers={}) execute("POST", path, data, headers) end |
#put(path, data, headers = {}) ⇒ Object
26 27 28 |
# File 'lib/salesfly/rest.rb', line 26 def put(path, data, headers={}) execute("PUT", path, data, headers) end |