Class: MrMurano::Endpoint

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

Overview

…/endpoint

Instance Method Summary collapse

Methods inherited from SolutionBase

#dodiff, #download, #endPoint, #localitems, #locallist, #removelocal, #status, #syncdown, #syncup, #tolocalpath

Methods included from Verbose

#debug, #verbose

Methods included from Http

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

Constructor Details

#initializeEndpoint

Returns a new instance of Endpoint.



10
11
12
13
14
# File 'lib/MrMurano/Solution-Endpoint.rb', line 10

def initialize
  super
  @uriparts << 'endpoint'
  @location = $cfg['location.endpoints']
end

Instance Method Details

#docmp(itemA, itemB) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/MrMurano/Solution-Endpoint.rb', line 101

def docmp(itemA, itemB)
  if itemA[:script].nil? and itemA[:local_path] then
    itemA[:script] = itemA[:local_path].read
  end
  if itemB[:script].nil? and itemB[:local_path] then
    itemB[:script] = itemB[:local_path].read
  end
  return itemA[:script] != itemB[:script]
end

#fetch(id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/MrMurano/Solution-Endpoint.rb', line 22

def fetch(id)
  ret = get('/' + id.to_s)
  aheader = (ret[:script].lines.first or "").chomp
  dheader = /^--#ENDPOINT (?i:#{ret[:method]}) #{ret[:path]}$/
  rheader = %{--#ENDPOINT #{ret[:method]} #{ret[:path]}\n}
  if block_given? then
    yield rheader unless dheader =~ aheader
    yield ret[:script]
  else
    res = ''
    res << rheader unless dheader =~ aheader
    res << ret[:script]
    res
  end
end

#listObject

This gets all data about all endpoints



18
19
20
# File 'lib/MrMurano/Solution-Endpoint.rb', line 18

def list
  get()
end

#remove(id) ⇒ Object

Delete an endpoint



73
74
75
# File 'lib/MrMurano/Solution-Endpoint.rb', line 73

def remove(id)
  delete('/' + id.to_s)
end

#synckey(item) ⇒ Object



97
98
99
# File 'lib/MrMurano/Solution-Endpoint.rb', line 97

def synckey(item)
  "#{item[:method].upcase}_#{item[:path]}"
end

#tolocalname(item, key) ⇒ Object



77
78
79
80
81
82
# File 'lib/MrMurano/Solution-Endpoint.rb', line 77

def tolocalname(item, key)
  name = item[:method].downcase
  name << '_'
  name << item[:path].gsub(/\//, '-')
  name << '.lua'
end

#toRemoteItem(from, path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/MrMurano/Solution-Endpoint.rb', line 84

def toRemoteItem(from, path)
  # read first line of file and get method/path from it?
  path = Pathname.new(path) unless path.kind_of? Pathname
  aheader = path.readlines().first
  md = /--#ENDPOINT (\S+) (.*)/.match(aheader)
  if md.nil? then
    rp = path.relative_path_from(Pathname.new(Dir.pwd))
    say_warning "Not an Endpoint: #{rp.to_s}"
    return nil
  end
  {:method=>md[1], :path=>md[2]}
end

#upload(local, remote) ⇒ Object

Upload endpoint :local path to file to push :remote hash of method and endpoint path



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/MrMurano/Solution-Endpoint.rb', line 42

def upload(local, remote)
  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
  limitkeys = [:method, :path, :script, @itemkey]
  remote = remote.select{|k,v| limitkeys.include? k }
  remote[:script] = script
#      post('', remote)
  if remote.has_key? @itemkey then
    put('/' + remote[@itemkey], remote) do |request, http|
      response = http.request(request)
      case response
      when Net::HTTPSuccess
        #return JSON.parse(response.body)
      when Net::HTTPNotFound
        verbose "\tDoesn't exist, creating"
        post('/', remote)
      else
        showHttpError(request, response)
      end
    end
  else
    verbose "\tNo itemkey, creating"
    post('/', remote)
  end
end