Class: MrMurano::ServiceBase

Inherits:
SolutionBase show all
Defined in:
lib/MrMurano/Solution-Services.rb

Overview

Things that servers do that is common.

Direct Known Subclasses

EventHandler, Library

Instance Method Summary collapse

Methods inherited from SolutionBase

#endPoint, #initialize

Methods included from SyncUpDown

#dodiff, #download, #ignoring, #localitems, #locallist, #removelocal, #searchFor, #status, #syncdown, #synckey, #syncup, #toRemoteItem, #tolocalname, #tolocalpath

Methods included from Verbose

#debug, #error, #outf, #tabularize, #verbose, #warning

Methods included from Http

#curldebug, #delete, #get, #http, #http_reset, #isJSON, #json_opts, #post, #postf, #put, #set_def_headers, #showHttpError, #token, #workit

Constructor Details

This class inherits a constructor from MrMurano::SolutionBase

Instance Method Details

#cachedUpdateTimeFor(local_path) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/MrMurano/Solution-Services.rb', line 131

def cachedUpdateTimeFor(local_path)
  cksm = Digest::SHA1.file(local_path.to_s).hexdigest
  cacheFile = $cfg.file_at(cacheFileName)
  return nil unless cacheFile.file?
  ret = nil
  cacheFile.open('r') do |io|
    cache = YAML.load(io)
    return nil unless cache
    if cache.has_key?(local_path.to_s) then
      entry = cache[local_path.to_s]
      debug("For #{local_path}:")
      debug(" cached: #{entry.to_s}")
      debug(" cm: #{cksm}")
      if entry.kind_of?(Hash) then
        if entry[:sha1] == cksm and entry.has_key?(:updated_at) then
          ret = DateTime.parse(entry[:updated_at])
        end
      end
    end
  end
  ret
end

#cacheFileNameObject



99
100
101
102
103
104
# File 'lib/MrMurano/Solution-Services.rb', line 99

def cacheFileName
  ['cache',
   self.class.to_s.gsub(/\W+/,'_'),
   @sid,
   'yaml'].join('.')
end

#cacheUpdateTimeFor(local_path, time = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/MrMurano/Solution-Services.rb', line 106

def cacheUpdateTimeFor(local_path, time=nil)
  time = Time.now.getutc if time.nil?
  entry = {
    :sha1=>Digest::SHA1.file(local_path.to_s).hexdigest,
    :updated_at=>time.to_datetime.iso8601(3)
  }
  cacheFile = $cfg.file_at(cacheFileName)
  if cacheFile.file? then
    cacheFile.open('r+') do |io|
      cache = YAML.load(io)
      cache = {} unless cache
      io.rewind
      cache[local_path.to_s] = entry
      io << cache.to_yaml
    end
  else
    cacheFile.open('w') do |io|
      cache = {}
      cache[local_path.to_s] = entry
      io << cache.to_yaml
    end
  end
  time
end

#docmp(itemA, itemB) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/MrMurano/Solution-Services.rb', line 81

def docmp(itemA, itemB)
  if itemA[:updated_at].nil? and itemA[:local_path] then
    ct = cachedUpdateTimeFor(itemA[:local_path])
    itemA[:updated_at] = ct unless ct.nil?
    itemA[:updated_at] = itemA[:local_path].mtime.getutc if ct.nil?
  elsif itemA[:updated_at].kind_of? String then
    itemA[:updated_at] = DateTime.parse(itemA[:updated_at]).to_time.getutc
  end
  if itemB[:updated_at].nil? and itemB[:local_path] then
    ct = cachedUpdateTimeFor(itemB[:local_path])
    itemB[:updated_at] = ct unless ct.nil?
    itemB[:updated_at] = itemB[:local_path].mtime.getutc if ct.nil?
  elsif itemB[:updated_at].kind_of? String then
    itemB[:updated_at] = DateTime.parse(itemB[:updated_at]).to_time.getutc
  end
  return itemA[:updated_at].to_time.round != itemB[:updated_at].to_time.round
end

#fetch(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/MrMurano/Solution-Services.rb', line 32

def fetch(name)
  raise "Missing name!" if name.nil?
  raise "Empty name!" if name.empty?
  ret = get('/'+CGI.escape(name))
  error "Unexpected result type, assuming empty instead: #{ret}" unless ret.kind_of? Hash
  ret = {} unless ret.kind_of? Hash
  if block_given? then
    yield (ret[:script] or '')
  else
    ret[:script] or ''
  end
end

#listObject



27
28
29
30
# File 'lib/MrMurano/Solution-Services.rb', line 27

def list
  ret = get()
  ret[:items]
end

#mkalias(remote) ⇒ Object



15
16
17
18
19
# File 'lib/MrMurano/Solution-Services.rb', line 15

def mkalias(remote)
  # :nocov:
  raise "Needs to be implemented in child"
  # :nocov:
end

#mkname(remote) ⇒ Object



21
22
23
24
25
# File 'lib/MrMurano/Solution-Services.rb', line 21

def mkname(remote)
  # :nocov:
  raise "Needs to be implemented in child"
  # :nocov:
end

#remove(name) ⇒ Object

??? remove



46
47
48
# File 'lib/MrMurano/Solution-Services.rb', line 46

def remove(name)
  delete('/'+name)
end

#upload(local, remote, modify = false) ⇒ Object

Parameters:

  • (defaults to: false)

    Bool: True if item exists already and this is changing it



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
77
78
79
# File 'lib/MrMurano/Solution-Services.rb', line 51

def upload(local, remote, modify=false)
  local = Pathname.new(local) unless local.kind_of? Pathname
  raise "no file" unless local.exist?

  # we assume these are small enough to slurp.
  script = local.read

  pst = remote.merge ({
    :solution_id => $cfg['solution.id'],
    :script => script,
    :alias=>mkalias(remote),
    :name=>mkname(remote),
  })
  debug "f: #{local} >> #{pst.reject{|k,_| k==:script}.to_json}"
  # try put, if 404, then post.
  put('/'+mkalias(remote), pst) do |request, http|
    response = http.request(request)
    case response
    when Net::HTTPSuccess
      #return JSON.parse(response.body)
    when Net::HTTPNotFound
      verbose "Doesn't exist, creating"
      post('/', pst)
    else
      showHttpError(request, response)
    end
  end
  cacheUpdateTimeFor(local)
end