Class: Sfn::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/satisfaction/loader.rb

Defined Under Namespace

Classes: CacheRecord, HashCache, MemcacheCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



13
14
15
16
# File 'lib/satisfaction/loader.rb', line 13

def initialize(options={})
  @options = options.reverse_merge({:cache => :hash})
  reset_cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/satisfaction/loader.rb', line 10

def cache
  @cache
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/satisfaction/loader.rb', line 11

def options
  @options
end

Instance Method Details

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/satisfaction/loader.rb', line 27

def get(url, options = {})
  uri = get_uri(url)
  request = Net::HTTP::Get.new(uri.request_uri)
  cache_record = cache.get(uri)
  
  if cache_record && !options[:force]
    request["If-None-Match"] = cache_record.etag
  end
  
  http = Net::HTTP.new(uri.host, uri.port)
  add_authentication(request, http, options)
  response = execute(http, request)
  
  case response
  when Net::HTTPNotModified
    return [:ok, cache_record.body]
  when Net::HTTPSuccess
    cache.put(uri, response)
    [:ok, response.body]
  when Net::HTTPMovedPermanently
    limit = options[:redirect_limit] || 3
    raise ArgumentError, "Too many redirects" unless limit > 0 #TODO: what is a better error here?
    get(response['location'], options.merge(:redirect_limit => limit - 1))
  when Net::HTTPBadRequest
    [:bad_request, response.body]
  when Net::HTTPForbidden
    [:forbidden, response.body]
  when Net::HTTPUnauthorized
    [:unauthorized, response.body]
  else
    raise "Explode: #{response.to_yaml}"
  end
end

#post(url, options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/satisfaction/loader.rb', line 61

def post(url, options)
  uri = get_uri(url)
  form = options[:form] || {}
  method_klass =  case options[:method]
                  when :put     then Net::HTTP::Put
                  when :delete  then Net::HTTP::Delete
                  else
                     Net::HTTP::Post
                  end
                  
  request = method_klass.new(uri.request_uri)
  
  request.set_form_data(form)
  
  http = Net::HTTP.new(uri.host, uri.port)
  add_authentication(request, http, options)
  response = execute(http, request)
  
  case response
  when Net::HTTPUnauthorized
    [:unauthorized, response.body]
  when Net::HTTPBadRequest
    [:bad_request, response.body]
  when Net::HTTPForbidden
    [:forbidden, response.body]
  when Net::HTTPSuccess
    [:ok, response.body]
  else
    raise "Explode: #{response.to_yaml}"
  end
end

#reset_cacheObject



18
19
20
21
22
23
24
25
# File 'lib/satisfaction/loader.rb', line 18

def reset_cache
  @cache =  case @options[:cache]
            when :hash then HashCache.new
            when :memcache then MemcacheCache.new(@options[:memcache] || {})
            else
              raise ArgumentError, "Invalid cache spec: #{@options[:cache]}"
            end
end