Module: OCTranspo::RemoteResourceMethods

Included in:
MobileResourceMethods, StopResource
Defined in:
lib/octranspo/remote_resource_methods.rb

Instance Method Summary collapse

Instance Method Details

#cache(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/octranspo/remote_resource_methods.rb', line 75

def cache (options={})
  address = cache_path_for(options)
  system "mkdir -p #{File.dirname(address)}"
  File.open(address, "w") do |f|
    f.write read_from_remote(options)
  end
  puts "Cached #{address}"
  read_from_cache(options)
end

#cache_pathObject



61
62
63
# File 'lib/octranspo/remote_resource_methods.rb', line 61

def cache_path
  "cache/octranspo/#{self.name.demodulize.underscore}"
end

#cache_path_for(options = {}) ⇒ Object



53
54
55
# File 'lib/octranspo/remote_resource_methods.rb', line 53

def cache_path_for (options={}); 
  "#{cache_path}/#{path_for(options)}"
end

#create_fixture(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/octranspo/remote_resource_methods.rb', line 65

def create_fixture (options={})
  address = fixture_path_for(options)
  puts "Creating fixture #{address}"
  system "mkdir -p #{File.dirname(address)}"
  File.open(address, "w") do |f|
    f.write read_from_remote(options)
  end
  read_from_fixture(options)
end

#fixture_path_for(options = {}) ⇒ Object



49
50
51
# File 'lib/octranspo/remote_resource_methods.rb', line 49

def fixture_path_for (options={}); 
  "#{fixtures_path}/#{path_for(options)}"
end

#fixtures_pathObject



57
58
59
# File 'lib/octranspo/remote_resource_methods.rb', line 57

def fixtures_path
  "test/fixtures/#{self.name.demodulize.underscore}"
end

#loggerObject



85
86
87
# File 'lib/octranspo/remote_resource_methods.rb', line 85

def logger
  RAILS_DEFAULT_LOGGER
end

#path_for(options = {}) ⇒ Object



47
# File 'lib/octranspo/remote_resource_methods.rb', line 47

def path_for (options={}); end

#read(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/octranspo/remote_resource_methods.rb', line 6

def read (options={})
  # puts url_for(options)
  case @source
  when :fixtures
    read_from_fixture(options)
  else
    read_from_cache(options) || cache(options)
  end
end

#read_from_cache(options) ⇒ Object



28
29
30
31
32
33
# File 'lib/octranspo/remote_resource_methods.rb', line 28

def read_from_cache (options)
  address = cache_path_for(options)
  if File.exist?(address)
    open(address).read
  end
end

#read_from_fixture(options) ⇒ Object



22
23
24
25
26
# File 'lib/octranspo/remote_resource_methods.rb', line 22

def read_from_fixture (options)
  address = fixture_path_for(options)
  create_fixture(options) unless File.exist?(address)
  open(address).read
end

#read_from_remote(options) ⇒ Object



16
17
18
19
20
# File 'lib/octranspo/remote_resource_methods.rb', line 16

def read_from_remote (options)
  url = url_for(options)
  puts "Opening #{url}"
  open(url).read
end

#reset!Object



35
36
37
# File 'lib/octranspo/remote_resource_methods.rb', line 35

def reset!
  @source = nil
end

#siteObject



43
# File 'lib/octranspo/remote_resource_methods.rb', line 43

def site; end

#url_for(options = {}) ⇒ Object



45
# File 'lib/octranspo/remote_resource_methods.rb', line 45

def url_for (options={}); end

#use_fixtures!Object



39
40
41
# File 'lib/octranspo/remote_resource_methods.rb', line 39

def use_fixtures!
  @source = :fixtures
end