Class: Leadsquared::Client

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

Constant Summary collapse

ENDPOINT =
'https://api.leadsquared.com'.freeze
HEADERS =
{'Content-Type' => 'application/json', 'Accept' => 'application/json'}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, secret = nil, endpoint = nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/leadsquared/client.rb', line 10

def initialize(key = nil, secret = nil, endpoint = nil)
  @key      = key       || Leadsquared.config.key
  @secret   = secret    || Leadsquared.config.secret
  @endpoint = endpoint  || Leadsquared.config.endpoint || ENDPOINT
  raise ArgumentError.new("Missing key or secret") unless @secret and @key
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/leadsquared/client.rb', line 8

def endpoint
  @endpoint
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/leadsquared/client.rb', line 8

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/leadsquared/client.rb', line 8

def secret
  @secret
end

Instance Method Details

#get(url, params = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/leadsquared/client.rb', line 29

def get(url, params = {})
  conn = Faraday.new(url: @endpoint)
  merged_params = {accessKey: @key, secretKey: @secret}.merge(params)
  response = conn.get(url) do |req|
    req.headers = HEADERS
    req.params  = merged_params
  end

  response
end

#post(url, params = {}, body = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/leadsquared/client.rb', line 17

def post(url, params = {}, body = nil)
  conn = Faraday.new(url: @endpoint)
  merged_params = {accessKey: @key, secretKey: @secret}.merge(params)
  response = conn.post(url) do |req|
    req.headers = HEADERS
    req.params  = merged_params
    req.body    = body if body
  end

  response
end