Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#es_index(db, doc) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cncflora_commons.rb', line 78

def es_index(db,doc)
  settings = Sinatra::Application.settings
  redoc = doc.clone
  redoc["id"] = doc["_id"]
  redoc["rev"] = doc["_rev"]
  redoc.delete("_id")
  redoc.delete("_rev")
  redoc.delete("_attachments")
  type = doc["metadata"]["type"]
  r = http_post("#{settings.elasticsearch}/#{db}/#{type}/#{URI.encode(redoc["id"])}",redoc)
  if r.has_key?("error")
    puts "index err = #{r}"
  end
end

#etcd2config(server) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cncflora_commons.rb', line 162

def etcd2config(server)
  cfg = flatten( http_get("#{server}/v2/keys/?recursive=true")["node"] )
  to_add={}
  cfg.each {|k,v|
    if k.match(/_port$/) then
      name = /(\w+)_port/.match(k).captures[0]
      ip   = cfg["#{name}_networksettings_ipaddress"]
      port = 80
      cfg.each {|kk,vv|
        if /^#{name}_networksettings/.match(kk) && vv == v then 
          port = /ports_(\d+)_tcp/.match(kk).captures[0] 
        end
      }
      to_add[name] = "http://#{ip}:#{port}"
    end
  }
  to_add.each{|k,v| cfg[k]=v}
  cfg
end

#flatten(obj) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cncflora_commons.rb', line 149

def flatten(obj)
  flat = {}
  if obj["dir"] && obj["nodes"] then
    obj["nodes"].each { |n|
      flat = flat.merge(flatten(n))
    }
  else
    key = obj["key"].gsub("/","_").gsub("-","_")
    flat[key[1..key.length]]=obj["value"]
  end
  flat
end

#http_delete(uri) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cncflora_commons.rb', line 55

def http_delete(uri)
    uri = URI.parse(uri)
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Delete.new(uri.request_uri)
    response = http.request(request)
    JSON.parse(response.body)
end

#http_get(uri) ⇒ Object



6
7
8
# File 'lib/cncflora_commons.rb', line 6

def http_get(uri)
    JSON.parse(Net::HTTP.get(URI(uri)))
end

#http_post(uri, doc) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cncflora_commons.rb', line 10

def http_post(uri,doc)
    uri = URI.parse(uri)
    http = Net::HTTP.new(uri.host, uri.port)

    if doc.class == Hash
        header = {'Content-Type'=> 'application/json'}
    elsif doc.class == String
        header = {'Content-Type'=> 'application/x-www-form-urlencoded'}
    end

    request = Net::HTTP::Post.new(uri.request_uri, header)

    if doc.class == Hash
        request.body = doc.to_json
    elsif doc.class == String
        request.body = doc
    end

    response = http.request(request)
    JSON.parse(response.body)
end

#http_put(uri, doc) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cncflora_commons.rb', line 32

def http_put(uri,doc) 
    uri = URI.parse(uri)
    http = Net::HTTP.new(uri.host, uri.port)

    if doc.class == Hash
        header = {'Content-Type'=> 'application/json'}
    elsif doc.class == String
        header = {'Content-Type'=> 'application/x-www-form-urlencoded'}
    end

    request = Net::HTTP::Put.new(uri.request_uri, header)

    if doc.class == Hash
        request.body = doc.to_json
    elsif doc.class == String
        request.body = doc
    end

    response = http.request(request)

    JSON.parse(response.body)
end

#index(db, doc) ⇒ Object



93
94
95
96
# File 'lib/cncflora_commons.rb', line 93

def index(db,doc)
  es_index(db,doc)
  sleep 1
end

#index_bulk(db, docs) ⇒ Object



98
99
100
101
# File 'lib/cncflora_commons.rb', line 98

def index_bulk(db,docs)
  docs.each{|doc| es_index(db,doc) }
  sleep 1
end

#onchange(etcd) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/cncflora_commons.rb', line 182

def onchange(etcd)
  Thread.new do
    while true do
      a = http_get("#{etcd}/v2/keys/?recursive=true&wait=true")
      puts "etcd updated"
      yield etcd2config(etcd)
    end
  end
end

#search(db, index, query) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cncflora_commons.rb', line 63

def search(db,index,query)
    query="scientificName:'Aphelandra longiflora'" unless query != nil && query.length > 0
    query_json={"query"=>{"query_string"=>{"query"=>query}}}
    result = []
    r = http_post("#{settings.elasticsearch}/#{db}/#{index}/_search?size=99999",query_json)
    if r['hits'] && r['hits']['hits'] then
        r['hits']['hits'].each{|hit|
            result.push(hit["_source"])
        }
    else
        puts "search error #{r}"
    end
    result
end

#setup(file) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/cncflora_commons.rb', line 103

def setup(file)
    #config_file file

    @config = YAML.load_file(file)[ENV['RACK_ENV'] || 'development']

    @config.each {|ck,cv|
      ENV.each {|ek,ev|
        if cv =~ /\$#{ek}/ then
          @config[ck] = cv.gsub(/\$#{ek}/,ev)
        end
      }
    }

    if ENV["ETCD"] || @config["etcd"]  then
      etcd_cfg = etcd2config(ENV["ETCD"] || @config["etcd"])
      onchange(ENV["ETCD"] || @config["etcd"]) do |newconfig|
        newconfig.each {|k,v| @config[k] = v }
        if defined? settings then
          newconfig.each {|k,v| set k.to_sym,v }
        end
      end
      etcd_cfg.each {|k,v| @config[k] = v }
    end

    ENV.each {|k,v| @config[k]=v }

    if @config["lang"] then
        @config["strings"] = JSON.parse(File.read("src/locales/#{@config["lang"]}.json", :encoding => "BINARY"))
    end

    if defined? settings then
      @config.each {|k,v| set k.to_sym,v }
      set :config, @config

      use Rack::Session::Pool

      set :session_secret, '1flora2'
      set :views, 'src/views'
    end

    puts "@config loaded"
    puts @config

    @config
end