Class: Hse::Remote
- Inherits:
-
Object
- Object
- Hse::Remote
- Defined in:
- lib/hse/remote.rb
Instance Attribute Summary collapse
-
#app_dir ⇒ Object
, :document, :config_path, :revision.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#env ⇒ Object
, :document, :config_path, :revision.
Instance Method Summary collapse
- #db_url ⇒ Object
- #get_doc_revision(doc) ⇒ Object
- #get_id(id) ⇒ Object
-
#initialize(app_dir, env = 'default') ⇒ Remote
constructor
, config_path = nil).
- #load_couchapprc ⇒ Object
-
#push_docs(docs) ⇒ Object
def put id, body = “” path = “##db_url/#id” puts “remote: PUT path #path” puts “remote: PUT body: #0.0..80 …” #response = Typhoeus::Request.put(path, :body => body) puts “PUT Response: #responseresponse.code #0.0..200” response end.
- #user(email, push) ⇒ Object
Constructor Details
#initialize(app_dir, env = 'default') ⇒ Remote
, config_path = nil)
6 7 8 9 10 |
# File 'lib/hse/remote.rb', line 6 def initialize(app_dir, env = 'default') #, config_path = nil) self.env = env self.app_dir = File.(app_dir) + '/' load_couchapprc end |
Instance Attribute Details
#app_dir ⇒ Object
, :document, :config_path, :revision
3 4 5 |
# File 'lib/hse/remote.rb', line 3 def app_dir @app_dir end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/hse/remote.rb', line 4 def config @config end |
#env ⇒ Object
, :document, :config_path, :revision
3 4 5 |
# File 'lib/hse/remote.rb', line 3 def env @env end |
Instance Method Details
#db_url ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/hse/remote.rb', line 17 def db_url if env =~ /^https?\:\/\// # the env is actual a db_url env else env_config = config['couchapprc']['env'][env] raise "No such env: #{env}" unless env_config && env_config['db'] puts "db_url #{env_config['db']}" env_config['db'] end end |
#get_doc_revision(doc) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/hse/remote.rb', line 59 def get_doc_revision doc puts "getting current revision #{doc[:_id]}" current = get_id doc[:_id] rescue nil if current puts "current revision: #{current['_rev']}" end current ? current['_rev'] : nil end |
#get_id(id) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/hse/remote.rb', line 28 def get_id id path = "#{db_url}/#{id}?include_docs=true" response = RestClient.get path, :content_type => :json, :accept => :json # rescue nil return "no _id #{id}" unless response.code == 200 result = JSON.parse(response.body) response.code == 200 ? result : nil end |
#load_couchapprc ⇒ Object
12 13 14 15 |
# File 'lib/hse/remote.rb', line 12 def load_couchapprc @config ||= {} @config['couchapprc'] = JSON.parse(File.read(File.join(app_dir, '.couchapprc'))) end |
#push_docs(docs) ⇒ Object
def put id, body = “”
path = "#{db_url}/#{id}"
puts "remote: PUT path #{path}"
puts "remote: PUT body: #{body[0..80]} ..."
#response = Typhoeus::Request.put(path, :body => body)
puts "PUT Response: #{response.code} #{response.body[0..200]}"
response
end
77 78 79 80 81 82 83 84 85 |
# File 'lib/hse/remote.rb', line 77 def push_docs docs docs = docs.kind_of?(Array) ? docs : [docs] docs.each_slice(1000) do |chunk| json =RestClient.post "#{db_url}/_bulk_docs", {"docs" => chunk}.to_json, :content_type => :json, :accept => :json JSON.parse(json) puts "pushed #{chunk.size}" end true end |
#user(email, push) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hse/remote.rb', line 36 def user email, push server = "#{db_url}".split("/")[0..-2].join("/") path = "#{server}/_users/org.couchdb.user:#{email}" puts "USER PATH #{path}" #response = Typhoeus::Request.get(path) response = RestClient.get path, :content_type => :json, :accept => :json # rescue nil puts "GET Response: #{response.code} #{response.body[0..200]}" response.code == 200 ? response.body : nil user = JSON.parse(response.body) pp user return unless push # curl -vX PUT $HOST/_users/org.couchdb.user:bob -d '{"name":"bob", "password":"bobspassword", "roles":[], "type":"user"}' -H "Content-Type: application/json" picture = email.split("@").first id = "org.couchdb.user:#{email}" newuser = {type:"user", roles:[], _id: id, name: email, password:email, email: email, picture: picture, iterations:1000} newuser["_rev"] = user["_rev"] if user json = newuser.to_json path = "#{server}/_users/#{id}" #headers = {"Content-Type" => "application/json"} #response = Typhoeus::Request.put(path, body:json) puts "PUT Response: #{response.code} #{response.body[0..200]}" end |