throw 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d44b37fdc5f96f9e9656b8c4046b44b82658c0ac
4
+ data.tar.gz: d6c265e7a572a9fb43bf879f5098d386cc279877
5
+ SHA512:
6
+ metadata.gz: c68125cd1cf8ecd641d82a10dbfeead29c06ab57f97daddca414290bff0a10e6d9aca42f052cc305da4890ad8d0339149bb45bb2f4d982f210f357e829d79bd9
7
+ data.tar.gz: 52e8c2c5b950993b1757ccb5d58b776873e7ab8f3aad387aad7816c70a7f89a7eee48334a95a79db8dce9dda4afc8caca104bd9f20b9976f1993d310d0168748
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Matías Aguirre
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,10 @@
1
+ clean:
2
+ @ rm -f throw*.gem
3
+
4
+ build: clean
5
+ @ gem build throw.gemspec
6
+
7
+ publish: build
8
+ @ gem push throw-*.gem
9
+
10
+ .PHONY: clean build publish
@@ -0,0 +1,9 @@
1
+ Throw
2
+ =====
3
+
4
+
5
+ ``throw`` aims to be a simple API wrapper for `Apache CouchDB™`_.
6
+
7
+ **Docs and tests comming soon.**
8
+
9
+ .. _Apache CouchDB™: http://couchdb.apache.org/
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'throw'
6
+
7
+
8
+ $options = OpenStruct.new
9
+ $options.db = nil
10
+ $options.docs = []
11
+
12
+ optparser = OptionParser.new do |opts|
13
+ opts.banner = 'Usage: upload-views.rb [options]'
14
+
15
+ opts.on('-d', '--db=', 'CouchDB Database name') do |d|
16
+ $options.db = d
17
+ end
18
+
19
+ opts.on('-n', '--doc-name=', 'Design document name') do |d|
20
+ $options.docs << d
21
+ end
22
+ end
23
+
24
+ optparser.parse!
25
+
26
+ if $options.db.nil? || $options.doc.empty?
27
+ puts "Wrong options\n\n"
28
+ puts optparser
29
+ exit 1
30
+ end
31
+
32
+ def load_views(doc_name)
33
+ views = {}
34
+
35
+ Dir["**/#{doc_name}/views/*"].each do |path|
36
+ if path.ends_with? '.js'
37
+ content = File.open(path, 'r'){|f| f.readlines.join}
38
+ name = path.split('/').last.gsub(/\.js$/, '')
39
+ details = name.rpartition('-')
40
+ name, type = details.first.to_sym, details.last.to_sym
41
+ views[name] ||= {}
42
+ views[name][type] = content
43
+ end
44
+ end
45
+
46
+ views
47
+ end
48
+
49
+ def load_lists(doc_name)
50
+ lists = {}
51
+
52
+ Dir["**/#{doc_name}/lists/*"].each do |path|
53
+ if path.ends_with? '.js'
54
+ content = File.open(path, 'r'){|f| f.readlines.join}
55
+ name = path.split('/').last.gsub(/\.js$/, '')
56
+ lists[name] = content
57
+ end
58
+ end
59
+
60
+ lists
61
+ end
62
+
63
+ server = Throw::Server.new($options.db)
64
+
65
+ $options.docs.each do |name|
66
+ design = {
67
+ views: load_views(name),
68
+ lists: load_lists(name)
69
+ }.select{|k, v| !v.nil? && !v.empty?}
70
+
71
+ server.update_design(name, design)
72
+ end
@@ -0,0 +1,3 @@
1
+ require_relative 'throw/version.rb'
2
+ require_relative 'throw/design.rb'
3
+ require_relative 'throw/server.rb'
@@ -0,0 +1,66 @@
1
+ module Throw
2
+ class Design
3
+ def initialize(server, details, query = nil)
4
+ @server = server
5
+ @details = details
6
+ @id = details[:id] || details[:_id]
7
+ @name = @id.split('/').last if @details
8
+ @query = query || []
9
+
10
+ @views = @details[:views].inject({}) do |memo, (name, value)|
11
+ memo[name] = name
12
+ memo[name.to_s.gsub('-', '_').to_sym] = name
13
+ memo
14
+ end
15
+
16
+ @lists = @details[:lists].inject({}) do |memo, (name, value)|
17
+ memo[name] = name
18
+ memo[name.to_s.gsub('-', '_').to_sym] = name
19
+ memo
20
+ end
21
+ end
22
+
23
+ def clone
24
+ Design.new(@server, @details, @query ? @query.clone : nil)
25
+ end
26
+
27
+ def <<(name)
28
+ @query << name
29
+ self
30
+ end
31
+
32
+ def run(*args, &block)
33
+ view_name = get_query_view
34
+ list_name = get_query_list
35
+
36
+ res = if view_name && list_name
37
+ @server.get("_design/#{@name}/_list/#{list_name}/#{view_name}", *args)
38
+ elsif view_name
39
+ @server.get("_design/#{@name}/_view/#{view_name}", *args)
40
+ end
41
+
42
+ return res unless block_given?
43
+ yield res
44
+ end
45
+
46
+ private
47
+
48
+ def get_query_view
49
+ name = @query.select{|q| @views.has_key? q.to_sym}.first
50
+ name = @views[name] if name
51
+ name
52
+ end
53
+
54
+ def get_query_list
55
+ name = @query.select{|q| @lists.has_key? q.to_sym}.first
56
+ name = @lists[name] if name
57
+ name
58
+ end
59
+
60
+ def method_missing(name, *args, &block)
61
+ copy = clone << name
62
+ return copy unless block_given?
63
+ yield copy
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,118 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+
5
+ module Throw
6
+ class Server
7
+ attr_reader :db_name
8
+ attr_reader :host
9
+
10
+ def initialize(db_name, host = nil)
11
+ @db_name = db_name
12
+ @host = host || 'localhost:5984'
13
+ @_designs = load_designs
14
+ end
15
+
16
+ def update_design(name, details = {})
17
+ details = begin
18
+ get("_design/#{name}").merge(details)
19
+ rescue RestClient::ResourceNotFound
20
+ details
21
+ end
22
+
23
+ put("_design/#{name}", details)
24
+
25
+ @_designs[name.to_sym] = Design.new(self, details)
26
+ end
27
+
28
+ def find(id)
29
+ get id
30
+ end
31
+
32
+ def save(doc, opts = {})
33
+ doc.has_key?(:_id) ? put(doc[:_id], doc) : post('', doc)
34
+ end
35
+
36
+ def exists?(id)
37
+ !!head(id)
38
+ rescue RestClient::ResourceNotFound
39
+ false
40
+ end
41
+
42
+ def uuids(count: 10)
43
+ request(:get, '_uuids', count: count)[:uuids]
44
+ end
45
+
46
+ def delete(path)
47
+ current = get(path)
48
+ data = {_rev: current[:_rev]} if current
49
+ db_request(:delete, path, data)
50
+ end
51
+
52
+ def head(path)
53
+ db_request(:head, path)
54
+ end
55
+
56
+ def get(path, params = {})
57
+ db_request(:get, path, params)
58
+ end
59
+
60
+ def put(path, data = {})
61
+ db_request(:put, path, data)
62
+ end
63
+
64
+ def post(path, data = {})
65
+ db_request(:post, path, data)
66
+ end
67
+
68
+ def reset!
69
+ request(:delete, @db_name)
70
+ request(:put, @db_name)
71
+ end
72
+
73
+ private
74
+
75
+ def method_missing(name, *args, &block)
76
+ @_designs[name.to_sym] || super
77
+ end
78
+
79
+ def db_request(method, path, params = {})
80
+ request(method, "#{@db_name}/#{path}", params)
81
+ end
82
+
83
+ def request(method, path, params = {})
84
+ url = "http://#{@host}/#{path}"
85
+
86
+ response = case method
87
+ when :get
88
+ params = quote_params(params)
89
+ RestClient.get(url, params: params, accept: :json, content_type: :json)
90
+ when :post
91
+ RestClient.post(url, JSON.dump(params), accept: :json, content_type: :json)
92
+ when :put
93
+ RestClient.put(url, JSON.dump(params), accept: :json, content_type: :json)
94
+ when :delete
95
+ RestClient.delete(url, params: JSON.dump(params), accept: :json)
96
+ when :head
97
+ RestClient.head(url, accept: :json)
98
+ end
99
+
100
+ JSON.parse(response, symbolize_names: true) unless response.nil? || response.empty?
101
+ end
102
+
103
+ def load_designs
104
+ designs = get('_all_docs', startkey: '_design/', endkey: '_design0')
105
+ designs[:rows].inject({}) do |memo, row|
106
+ id = row[:id] || row[:_id]
107
+ memo[id.split('/').last.to_sym] = Design.new(self, get(id))
108
+ memo
109
+ end
110
+ end
111
+
112
+ def quote_params(params)
113
+ params[:startkey] = JSON.dump(params[:startkey]) if params.has_key? :startkey
114
+ params[:endkey] = JSON.dump(params[:endkey]) if params.has_key? :endkey
115
+ params
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ module Throw
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'throw'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-10-14'
5
+ s.summary = 'CouchDB simple wrapper'
6
+ s.description = 'A simple CouchDB API wrapper'
7
+ s.authors = ['Matías Aguirre']
8
+ s.email = 'matiasaguirre@gmail.com'
9
+ s.files = `git ls-files`.split($/)
10
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
11
+ s.add_dependency 'rest-client', '~> 1.7'
12
+ s.homepage = 'https://github.com/omab/throw'
13
+ s.license = 'MIT'
14
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: throw
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matías Aguirre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description: A simple CouchDB API wrapper
28
+ email: matiasaguirre@gmail.com
29
+ executables:
30
+ - throw-design.rb
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - LICENSE
36
+ - Makefile
37
+ - README.rst
38
+ - bin/throw-design.rb
39
+ - lib/couchdb.rb
40
+ - lib/couchdb/design.rb
41
+ - lib/couchdb/server.rb
42
+ - lib/couchdb/version.rb
43
+ - throw.gemspec
44
+ homepage: https://github.com/omab/throw
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: CouchDB simple wrapper
68
+ test_files: []