Class: Purls

Inherits:
Object
  • Object
show all
Defined in:
lib/purls.rb,
lib/purls/version.rb

Defined Under Namespace

Classes: ConfigError, FetchError, SocketError

Constant Summary collapse

MAXTIME =

timeout in seconds to complete parallel get

45
VERSION =
'0.9.1'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePurls

Returns a new instance of Purls.



36
37
38
39
40
41
42
# File 'lib/purls.rb', line 36

def initialize
  if self.class.service_uri.nil?
    raise ConfigError.new("You must set Purls.service_uri")
  end
  self.class.service_uri = URI.parse(service_uri) if service_uri.is_a?(String)
  self.class.maxtime ||= MAXTIME # set default timeout
end

Class Attribute Details

.maxtimeObject

Returns the value of attribute maxtime.



25
26
27
# File 'lib/purls.rb', line 25

def maxtime
  @maxtime
end

.service_uriObject

Returns the value of attribute service_uri.



25
26
27
# File 'lib/purls.rb', line 25

def service_uri
  @service_uri
end

Class Method Details

.get(urls, options = {}) ⇒ Object



31
32
33
# File 'lib/purls.rb', line 31

def get(urls, options={})
  instance.get(urls, options)
end

.instanceObject



27
28
29
# File 'lib/purls.rb', line 27

def instance
  Thread.current[:purls_instance] ||= self.new
end

Instance Method Details

#get(urls, options = {}) ⇒ Object



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
# File 'lib/purls.rb', line 48

def get(urls, options={})
  maxtime = options[:maxtime] || self.class.maxtime
  q = urls.map {|u| "url[]=#{CGI.escape(u)}" rescue nil }.compact.join('&')
  return [] if q.empty?

  uri = service_uri.dup
  uri.path = '/fetch'
  uri.query = q + "&maxtime=#{maxtime}"

  obj = nil
  begin
    resp = http.get(uri.to_s, {})
    if resp.header['Content-Type'].index('x-msgpack')
      obj = MessagePack.unpack(resp.body)
    else
      obj = resp.body
    end
  rescue => ex
    raise SocketError.new(ex.inspect)
  end

  if resp.code.to_i != 200
    raise FetchError.new("Server responded with #{resp.code}")
  end

  obj
end

#httpObject



44
45
46
# File 'lib/purls.rb', line 44

def http
  @http ||= Net::HTTP.new(service_uri.host, service_uri.port)
end