Class: Linkscape::Request

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

Constant Summary collapse

URL_TEMPLATE =
%Q[http://:apiHost:/:apiRoot:/:api:/:url:?AccessID=:accessID:&Expires=:expiration:&Signature=:signature:]
MAX_URL_LENGTH =
500

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Request

Returns a new instance of Request.



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
44
45
46
47
# File 'lib/linkscape/request.rb', line 19

def initialize(options)
 
  new_vals = {}
  if options[:url] 
    case options[:url]
    when String
      new_vals = {:url => CGI::escape(options[:url].sub(/^https?:\/\//, '')) }
    when Array
      @body = options[:url].collect{ |u| u.sub(/^https?:\/\//, '') }
      new_vals = {:url => ""}
    else
      raise "URL most be a String or an Array"
    end
  end

  if Array(new_vals[:url]).any? { |u| u.length > MAX_URL_LENGTH }
    raise ArgumentError.new("Request URLs must be < #{MAX_URL_LENGTH} long")
  end
 
  @requestURL = URL_TEMPLATE.template(signRequest(options.merge(new_vals)))
  @requestURL += "&" + options[:query].collect{|k,v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}"}.join('&') if options[:query] && Hash === options[:query]
  @requestURL += "&" + options[:query] if options[:query] && String === options[:query]

  options[:offset] = 0 if options[:offset] && options[:offset] < 0
  @requestURL += "&Offset=#{options[:offset]}" if options[:offset]

  options[:limit] = 1000 if options[:limit] && options[:limit] > 1000
  @requestURL += "&Limit=#{options[:limit]}" if options[:limit]
end

Instance Attribute Details

#requestURLObject

require ‘base64’ require ‘rubygems’ require ‘hmac-sha1’



10
11
12
# File 'lib/linkscape/request.rb', line 10

def requestURL
  @requestURL
end

Class Method Details

.run(options) ⇒ Object



15
16
17
# File 'lib/linkscape/request.rb', line 15

def self.run(options)
  self.new(options).run
end

Instance Method Details

#inspectObject



55
56
57
58
# File 'lib/linkscape/request.rb', line 55

def inspect
  #<Linkscape::Request:0x1016228a0 @requestURL="http://lsapi.seomoz.com/linkscape/mozrank/www.martian.at%2F?AccessID=ose&Expires=1258772331&Signature=Hfwssn0ZbWMe9MEf6%2FWoHOGFHzQ%3D">
  %Q[#<#{self.class}="#{@requestURL}">]
end

#runObject



49
50
51
52
53
# File 'lib/linkscape/request.rb', line 49

def run
  res = fetch(URI.parse(@requestURL))
  # res = fetch(URI.parse('http://martian.at/other/ose.json'))
  return Response.new(self, res)
end