Class: LsLinkdirectAPI::APIResource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ls_linkdirect_api/api_resource.rb

Direct Known Subclasses

BannerLinks, DRMLinks, TextLinks

Instance Method Summary collapse

Instance Method Details

#base_pathObject



9
10
11
12
13
14
15
16
# File 'lib/ls_linkdirect_api/api_resource.rb', line 9

def base_path
  if self.class == LsLinkdirectAPI::APIResource
    raise NotImplementedError.new(
      "APIResource is an abstract class. You should perform actions on its subclasses (i.e. TextLinks)"
    )
  end
  "/get#{CGI.escape(class_name)}/"
end

#class_nameObject



5
6
7
# File 'lib/ls_linkdirect_api/api_resource.rb', line 5

def class_name
  self.class.name.split('::')[-1]
end

#get(params = {}) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ls_linkdirect_api/api_resource.rb', line 18

def get(params = {})
  
  unless token ||= LsLinkdirectAPI.token
    raise AuthenticationError.new(
      "No token provided. Set your token key using: LsLinkdirectAPI.token = 'TOKEN' " +
      "You can retrieve your Your Web Services Token from the Linkshare web interface. " +
      "http://helpcenter.linkshare.com/publisher/questions.php?questionid=58 for details."
    )
  end

  if token =~ /\s/
    raise AuthenticationError.new(
      "Your token looks invalid. " +
      "Double-check your token at http://linkshare.com"
    )
  end
  
  raise ArgumentError, "Params must be a Hash; got #{params.class} instead" unless params.is_a? Hash

  params.merge!({
    token: token,
  })
  make_params_valid(params)
  resource_url = LsLinkdirectAPI.api_base_url + base_path + self.params_path(params)
  request(resource_url, params)
end

#request(resource_url, params) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/ls_linkdirect_api/api_resource.rb', line 45

def request(resource_url, params)
  timeout = LsLinkdirectAPI.api_timeout
  begin
    response = self.class.get(resource_url, query: nil, timeout: timeout)
  rescue Timeout::Error
    raise ConnectionError.new("Timeout error (#{timeout}s)")
  end
  process(response, "#{class_name}", params)
end