heterocera 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .rvmrc
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in heterocera.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "heterocera/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "heterocera"
7
+ s.version = Heterocera::VERSION
8
+ s.authors = ["Dave ten Have"]
9
+ s.email = ["david@heterocera.com"]
10
+ s.homepage = "http://www.heterocera.com"
11
+ s.summary = "gem for talking to heterocera server"
12
+ s.description = "A simple wrapper for talking to the heterocera server"
13
+
14
+ s.rubyforge_project = "heterocera"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "json"
24
+ s.add_runtime_dependency "mechanize"
25
+ end
@@ -0,0 +1,23 @@
1
+ module HeteroceraArrayExtensions
2
+ def to_path
3
+ join("/")
4
+ end
5
+ end
6
+
7
+ module HeteroceraStringExtensions
8
+ def blank?
9
+ empty?
10
+ end
11
+
12
+ def present?
13
+ !blank?
14
+ end
15
+ end
16
+
17
+ class Array
18
+ include HeteroceraArrayExtensions
19
+ end
20
+
21
+ class String
22
+ include HeteroceraStringExtensions
23
+ end
@@ -0,0 +1,118 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'mechanize'
4
+
5
+ require 'heterocera/ext'
6
+
7
+ module Heterocera
8
+
9
+ class HeteroceraError < StandardError; end
10
+
11
+ class Client
12
+
13
+ READ = "read"
14
+ WRITE = "write"
15
+ TAKE = "take"
16
+
17
+ # get .json for free
18
+ XML = ".xml"
19
+ GZ = ".gz"
20
+ HTML = ".html"
21
+
22
+ OTHER_SUFFIXES = [XML, GZ, HTML]
23
+
24
+ def initialize server_address
25
+ @base_address = URI.parse server_address
26
+ @agent = Mechanize.new
27
+ end
28
+
29
+ def server_address
30
+ @base_address
31
+ end
32
+
33
+ def server_address= server_address
34
+ @base_address = URI.parse server_address
35
+ end
36
+
37
+ def read(tags = [], suffix = "")
38
+ get_query READ, tags, suffix
39
+ end
40
+
41
+ def write!(tags = [], value = "")
42
+ raise ArgumentError, 'You must provide a String or a File' unless value.class == String || value.class == File
43
+ raise ArgumentError, "You can't use wildcards when writing" if tags.include? '*'
44
+
45
+ case value.class.to_s
46
+ when "String"
47
+ if CGI.escape(value).bytesize < 1024
48
+ response = get(WRITE, tags, value)
49
+ else
50
+ response = post(WRITE, tags, value)
51
+ end
52
+ when "File"
53
+ raise TypeError, 'Please open files in binary mode' unless value.binmode?
54
+ response = post(WRITE, tags, value)
55
+ end
56
+
57
+ JSON.parse response.body
58
+
59
+ end
60
+
61
+ def take!(tags = [], suffix = "")
62
+ get_query TAKE, tags, suffix
63
+ end
64
+
65
+ private
66
+
67
+ def generate_url tuple_space_operation, tags
68
+ raise ArgumentError, "You must provide an array of tags ['foo', 'baa']" if tags.length == 0
69
+
70
+ "http://#{@base_address.host}:#{@base_address.port}/#{tuple_space_operation}/#{tags.to_path}"
71
+ end
72
+
73
+ def get tuple_space_operation, tags, value = "", suffix = ""
74
+ raise ArgumentError, 'You must provide a value' if value.blank? && tuple_space_operation == WRITE
75
+
76
+ request = generate_url tuple_space_operation, tags
77
+
78
+ if (suffix.present? && OTHER_SUFFIXES.include?(suffix))
79
+ request << suffix
80
+ elsif value.present?
81
+ request << "?value=#{CGI.escape(value)}"
82
+ end
83
+
84
+ @agent.get(request)
85
+
86
+ rescue Mechanize::ResponseCodeError => ex
87
+ raise HeteroceraError, "No data found" if ex.response_code == '404'
88
+ end
89
+
90
+ def post tuple_space_operation, tags, value = ""
91
+ raise TypeError, 'You must provide a value' unless value.present?
92
+
93
+ request = generate_url tuple_space_operation, tags
94
+
95
+ @agent.post(request, {"value" => value})
96
+
97
+ rescue Mechanize::ResponseCodeError => ex
98
+ raise HeteroceraError, "No data found" if ex.response_code == '404'
99
+ end
100
+
101
+ def get_query(action, tags, suffix)
102
+
103
+ response = get(action, tags, "", suffix).body
104
+
105
+ case suffix
106
+ when XML
107
+ response
108
+ when GZ
109
+ response
110
+ else
111
+ JSON.parse(response)
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,3 @@
1
+ module Heterocera
2
+ VERSION = "0.0.9"
3
+ end
data/lib/heterocera.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "heterocera/version"
2
+ require "heterocera/heterocera"
data/license.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2011, David ten Have
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the <organization> nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL DAVID TEN HAVE BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heterocera
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dave ten Have
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70248235040420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70248235040420
25
+ - !ruby/object:Gem::Dependency
26
+ name: mechanize
27
+ requirement: &70248235040000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70248235040000
36
+ description: A simple wrapper for talking to the heterocera server
37
+ email:
38
+ - david@heterocera.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Rakefile
46
+ - heterocera.gemspec
47
+ - lib/heterocera.rb
48
+ - lib/heterocera/ext.rb
49
+ - lib/heterocera/heterocera.rb
50
+ - lib/heterocera/version.rb
51
+ - license.txt
52
+ homepage: http://www.heterocera.com
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project: heterocera
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: gem for talking to heterocera server
76
+ test_files: []