Class: Salesfly::REST

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

Instance Method Summary collapse

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(options = {})
  @api_key = options[:api_key]
  @api_base_url = options.fetch(:api_base_url) { "https://api.salesfly.com" }   
  @timeout = options.fetch(:timeout) { 30 }   
  @user_agent =  "salesfly-ruby/#{VERSION}"                   
end

Instance Method Details

#api_keyObject



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.message)
        rescue Errno::ECONNREFUSED, Errno::EINVAL, Errno::ECONNRESET, EOFError,
          Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
          SocketError => e
          raise APIConnectionError.new(e.message)
        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