Class: MrMurano::Endpoint

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

Overview

…/endpoint

Instance Method Summary collapse

Methods inherited from SolutionBase

#endPoint

Methods included from SyncUpDown

#dodiff, #download, #localitems, #locallist, #removelocal, #status, #syncdown, #syncup, #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

#initializeEndpoint

Returns a new instance of Endpoint.



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

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

  @match_header = /--#ENDPOINT (?<method>\S+) (?<path>\S+)( (?<ctype>.*))?/
end

Instance Method Details

#docmp(itemA, itemB) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/MrMurano/Solution-Endpoint.rb', line 155

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] or itemA[:content_type] != itemB[:content_type])
end

#fetch(id) ⇒ Object



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
60
61
62
63
# File 'lib/MrMurano/Solution-Endpoint.rb', line 30

def fetch(id)
  ret = get('/' + id.to_s)
  ret[:content_type] = 'application/json' if ret[:content_type].empty?

  script = ret[:script].lines.map{|l|l.chomp}

  aheader = (script.first or "")

  rh = ['--#ENDPOINT', ret[:method].upcase, ret[:path]]
  rh << ret[:content_type] if ret[:content_type] != 'application/json'
  rheader = rh.join(' ')

  # if header is missing add it.
  # If header is wrong, replace it.

  md = @match_header.match(aheader)
  if md.nil? then
    # header missing.
    script.unshift rheader
  elsif md[:method] != ret[:method] or
        md[:path] != ret[:path] or
        md[:ctype] != ret[:content_type] then
    # header is wrong.
    script[0] = rheader
  end
  # otherwise current header is good.

  script = script.join("\n") + "\n"
  if block_given? then
    yield script
  else
    script
  end
end

#ignoringObject



111
112
113
# File 'lib/MrMurano/Solution-Endpoint.rb', line 111

def ignoring
  ($cfg['endpoints.ignoring'] or '').split
end

#listObject

This gets all data about all endpoints



20
21
22
23
24
25
26
27
28
# File 'lib/MrMurano/Solution-Endpoint.rb', line 20

def list
  get().map do |item|
    if item[:content_type].nil? or item[:content_type].empty? then
      item[:content_type] = 'application/json'
    end
    # XXX should this update the script header?
    item
  end
end

#remove(id) ⇒ Object

Delete an endpoint



103
104
105
# File 'lib/MrMurano/Solution-Endpoint.rb', line 103

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

#searchForObject



107
108
109
# File 'lib/MrMurano/Solution-Endpoint.rb', line 107

def searchFor
  ($cfg['endpoints.searchFor'] or '').split
end

#synckey(item) ⇒ Object



151
152
153
# File 'lib/MrMurano/Solution-Endpoint.rb', line 151

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

#tolocalname(item, key) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/MrMurano/Solution-Endpoint.rb', line 115

def tolocalname(item, key)
  name = ''
  name << item[:path].split('/').reject{|i|i.empty?}.join('-')
  name << '.'
  name << item[:method].downcase
  name << '.lua'
end

#toRemoteItem(from, path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/MrMurano/Solution-Endpoint.rb', line 123

def toRemoteItem(from, path)
  # Path could be have multiple endpoints in side, so a loop.
  items = []
  path = Pathname.new(path) unless path.kind_of? Pathname
  cur = nil
  lineno=0
  path.readlines().each do |line|
    md = @match_header.match(line)
    if not md.nil? then
      # header line.
      cur[:line_end] = lineno unless cur.nil?
      items << cur unless cur.nil?
      cur = {:method=>md[:method],
             :path=>md[:path],
             :content_type=> (md[:ctype] or 'application/json'),
             :local_path=>path,
             :line=>lineno,
             :script=>line}
    elsif not cur.nil? and not cur[:script].nil? then
      cur[:script] << line
    end
    lineno += 1
  end
  cur[:line_end] = lineno unless cur.nil?
  items << cur unless cur.nil?
  items
end

#upload(local, remote, modify) ⇒ Object

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

Parameters:

  • modify

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/MrMurano/Solution-Endpoint.rb', line 70

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

  # we assume these are small enough to slurp.
  unless remote.has_key? :script then
    script = local.read
    remote[:script] = script
  end
  limitkeys = [:method, :path, :script, :content_type, @itemkey]
  remote = remote.select{|k,v| limitkeys.include? k }
#      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