Class: Linkscape::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/linkscape/client.rb', line 3

def initialize(*args)
  options = Hash === args.last ? args.pop : {}
  @accessID = args.first ? args.shift : (options[:id] || options[:ID] || options[:accessID])
  @secretKey = args.first ? args.shift : (options[:secret] || options[:secretKey] || options[:key])
  
  @options = {
    :apiHost => 'lsapi.seomoz.com',
    :apiRoot => 'linkscape',
    :accessID => @accessID,
    :secretKey => @secretKey
  }.merge(options)
  
end

Instance Method Details

Raises:



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
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/linkscape/client.rb', line 79

def allLinks(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]
  scope = args.length >= 2 ? "#{args.shift}_to_#{args.shift}" : (args.first ? args.shift : options[:scope])
  sortOrder = (args.first ? args.shift : (options[:sort] || options[:sortOrder] || 'domains_linking_page')).to_sym
  filters = args.first ? args.shift : (options[:filters] || options[:filter] || '')

  raise MissingArgument, "allLinks requires a scope ([page, domain] to [page, subdomain, domain]) and a url." unless scope and url
  raise InvalidArgument, "allLinks scope must be valid ([page, domain] to [page, subdomain, domain])" unless scope.to_s =~ /^(page|domain)_to_(page|subdomain|domain)$/
  raise InvalidArgument, "allLinks sort order must be valid (domain_authority, page_authority, domains_linking_page, domains_linking_domain)" unless [:domain_authority, :page_authority, :domains_linking_page, :domains_linking_domain].include? sortOrder

  if String === filters
    filters = filters.downcase.split(/[,\s\+]+/).sort.collect(&:to_sym)
  elsif Hash === filters
    filters = filters.keys.collect{|k|k.to_s.downcase}.sort.collect(&:to_sym)
  elsif Array === filters
    filters = filters.collect{|k|k.to_s.downcase}.sort.collect(&:to_sym)
  elsif Symbol === filters
    filters = [filters]
  else
    raise InvalidArgument, "allLinks filters must be a string, hash, or array"
  end
  raise InvalidArgument, "allLinks filters must be from the set (:internal, :external, :redir301, :follow, :nofollow)" unless (filters - [:internal, :external, :redir301, :follow, :nofollow]).empty?

  options[:url] = url
  options[:api] = 'links'

  options[:query] = {
    'SourceCols' => translateBitfield(options[:sourcecols], options[:sourcecolumns], options[:urlcols], :type => :url),
    'TargetCols' => translateBitfield(options[:targetcols], options[:targetcolumns], options[:urlcols], :type => :url),
    'LinkCols' => translateBitfield(options[:cols], options[:columns], options[:linkcols], :type => :link),
    'Scope' => scope,
    'Filter' => filters.join(' ').gsub(/redir/, ''),
    'Sort' => sortOrder.to_s,
  }

  raise MissingArgument, "allLinks requires a list of columns for Source, Target, and/or Link." unless options[:query]['SourceCols'].nonzero? or options[:query]['TargetCols'].nonzero? or options[:query]['LinkCols'].nonzero?
  
  Linkscape::Request.run(@options.merge(options))
end

#anchorMetrics(*args) ⇒ Object

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/linkscape/client.rb', line 144

def anchorMetrics(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]
  scope = args.length >= 2 ? "#{args.shift}_to_#{args.shift}" : (args.first ? args.shift : options[:scope])
  filters = args.first ? args.shift : (options[:filters] || options[:filter] || '')
  sortOrder = (args.first ? args.shift : (options[:sort] || options[:sortOrder] || 'domains_linking_page')).to_sym

  raise InvalidArgument, "anchorMetrics scope must be valid ([phrase, term] to [page, subdomain, domain])" unless scope.to_s =~ /^(phrase|term)_to_(page|subdomain|domain)$/
  raise InvalidArgument, "anchorMetrics sort order must be valid (domain_authority, page_authority, domains_linking_page, domains_linking_domain)" unless [:domain_authority, :page_authority, :domains_linking_page, :domains_linking_domain].include? sortOrder

  if String === filters
    filters = filters.downcase.split(/[,\s\+]+/).sort.collect(&:to_sym)
  elsif Hash === filters
    filters = filters.keys.collect{|k|k.to_s.downcase}.sort.collect(&:to_sym)
  elsif Array === filters
    filters = filters.collect{|k|k.to_s.downcase}.sort.collect(&:to_sym)
  elsif Symbol === filters
    filters = [filters]
  else
    raise InvalidArgument, "anchorMetrics filters must be a string, hash, or array"
  end
  raise InvalidArgument, "anchorMetrics filters must be from the set (:internal, :external)" unless (filters - [:internal, :external]).empty?

  options[:url] = url
  options[:api] = 'anchor-text'

  options[:query] = {
    'Cols' => translateBitfield(options[:cols], options[:columns], options[:anchorcols], :type => :anchors),
    'Scope' => scope,
    'Filter' => filters.join(' ').gsub(/redir/, ''),
    'Sort' => sortOrder.to_s,
  }

  # raise MissingArgument, "anchorMetrics requires a list of columns to return." unless options[:query]['Cols'].nonzero?
  
  Linkscape::Request.run(@options.merge(options))
end

#inspectObject



185
186
187
# File 'lib/linkscape/client.rb', line 185

def inspect
  %Q[#<#{self.class}:#{"0x%x" % self.object_id} api="#{@options[:apiHost]}/#{@options[:apiRoot]}" accessID="#{@options[:accessID]}">]
end

#mozRank(*args) ⇒ Object

Raises:



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

def mozRank(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]

  raise MissingArgument, "mozRank requires a url." unless url

  options[:url] = url
  options[:api] = 'mozrank'

  Linkscape::Request.run(@options.merge(options))
end

#status(*args) ⇒ Object



137
138
139
140
141
# File 'lib/linkscape/client.rb', line 137

def status(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  options[:api] = 'status'
  Linkscape::Request.run(@options.merge(options))
end

Raises:



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
# File 'lib/linkscape/client.rb', line 50

def topLinks(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]
  whichSet = (args.first ? args.shift : (options[:set] || 'none')).to_sym
  
  raise MissingArgument, "topLinks requires a set (:page, :subdomain, :domain) and a url." unless whichSet and url
  
  options[:url] = url

  options[:api] = {
    :page => 'page-linklist',
    :subdomain => 'subdomain-linklist',
    :domain => 'rootdomain-linklist',
  }[whichSet]
  
  raise InvalidArgument, "topLinks set must be one of :page, :subdomain, :domain" unless options[:api]
  
  options[:query] = {
    'SourceCols' => translateBitfield(options[:sourcecols], options[:sourcecolumns], options[:urlcols], :type => :url),
    'TargetCols' => translateBitfield(options[:targetcols], options[:targetcolumns], options[:urlcols], :type => :url),
    'LinkCols' => translateBitfield(options[:cols], options[:columns], options[:linkcols], :type => :link),
  }

  raise MissingArgument, "topLinks requires a list of columns for Source, Target, and/or Link." unless options[:query]['SourceCols'].nonzero? or options[:query]['TargetCols'].nonzero? or options[:query]['LinkCols'].nonzero?
  
  Linkscape::Request.run(@options.merge(options))
end

#topPages(*args) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/linkscape/client.rb', line 121

def topPages(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]
  
  options[:url] = url
  options[:api] = 'top-pages'
  
  options[:query] = {
    'Cols' => translateBitfield(options[:cols], options[:columns], options[:sourcecols], options[:sourcecolumns], options[:urlcols], :type => :url),
  }

  raise MissingArgument, "topPages requires a list of columns to return." unless options[:query]['Cols'].nonzero?
  
  Linkscape::Request.run(@options.merge(options))
end

#urlMetrics(*args) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linkscape/client.rb', line 31

def urlMetrics(*args)
  options = Hash === args.last ? args.pop.symbolize_keys : {}
  url = args.first ? args.shift : options[:url]

  raise MissingArgument, "urlMetrics requires a url." unless url

  options[:url] = url
  options[:api] = 'url-metrics'

  options[:query] = {
    'Cols' => translateBitfield(options[:cols], options[:columns], options[:fields], :type => :url)
  }
  
  raise MissingArgument, "urlMetrics requires a list of columns to return." unless options[:query]['Cols'].nonzero?
  
  Linkscape::Request.run(@options.merge(options))
end