hoccer-api 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 hukl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ h2. Usage
2
+
3
+ bc.. # Create new LinccerClient instance
4
+ client = LinccerClient.new
5
+
6
+ # Update client environment
7
+ client.update_environment(
8
+ :gps => {:longitude => 12.2, :latitude => 14.2, :accuracy => 23.0}
9
+ )
10
+
11
+ # Share data
12
+ # The share method expects a mode and an arbitrary ruby hash which will
13
+ # be transformed to JSON and used as the payload.
14
+ c.share( "one-to-one", :foo => "bar" )
15
+
16
+ # Receive data
17
+ # The receive method expects a mode parameter and returns a ruby hash
18
+ # containing the payload
19
+ c.receive( "one-to-one" )
20
+
21
+ p. The share and receive method have threaded counterparts allowing to perform
22
+ both actions non blocking on the same ruby process.
23
+
24
+ h2. Install
25
+
26
+ Install the gem via rubygems (once the api is public)
27
+
28
+ bc. gem install hoccer-api
29
+
30
+ or clone and build it manually via:
31
+
32
+ bc.. git clone git@github.com:hoccer/ruby-api.git
33
+ cd ruby-api && gem build hoccer-api.gemspec && gem install hoccer-api-1.0.0.gem
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "geo_store_client"
8
+ gem.summary = %Q{ one-line summary of your gem}
9
+ gem.description = %Q{ longer description of your gem}
10
+ gem.email = "contact@smyck.org"
11
+ gem.homepage = "http://github.com/hukl/geo_store_client"
12
+ gem.authors = ["hukl"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "geo_store_client #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.10.0
@@ -0,0 +1,91 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Hoccer
5
+
6
+ class GeoStoreClient
7
+
8
+ def initialize
9
+ @server = "geostore.beta.hoccer.com"
10
+ @port = 80
11
+ end
12
+
13
+ def validate options, key, class_name
14
+ unless options[key] && options[key].is_a?( class_name )
15
+ raise(
16
+ ArgumentError,
17
+ "#{key.capitalize} is missing or not a #{class_name.to_s}"
18
+ )
19
+ end
20
+ end
21
+
22
+ def prepare options
23
+ validate options, :longitude, Float
24
+ validate options, :latitude, Float
25
+ validate options, :params, Hash
26
+
27
+ {
28
+ :environment => {
29
+ :gps => {
30
+ :longitude => options[:longitude],
31
+ :latitude => options[:latitude],
32
+ :accuracy => options[:accuracy] || 5
33
+ }
34
+ },
35
+ :params => options[:params],
36
+ :lifetime => options[:liftime]
37
+ }
38
+ end
39
+
40
+ def store options
41
+ data = prepare( options )
42
+ post "/store", data
43
+ end
44
+
45
+ def remove uuid
46
+ delete "/store/#{uuid}"
47
+ end
48
+
49
+ def search_within_radius options
50
+ validate options, :longitude, Float
51
+ validate options, :latitude, Float
52
+ validate options, :radius, Float
53
+
54
+ options[:accuracy] = options.delete(:radius)
55
+
56
+ post "/query", { :gps => options }
57
+ end
58
+
59
+ def search_within_region options
60
+ validate options, :region, Array
61
+
62
+ unless options[:region].all? {|x| x.is_a? Array}
63
+ raise ArgumentError, "Region must be of format [[lon1,lat1], [lon2,lat2]]"
64
+ end
65
+
66
+ post "/query", { :box => options[:region] }
67
+ end
68
+
69
+ def request method, path, data = {}
70
+ if method == :delete
71
+ Net::HTTP.start(@server, @port) do |http|
72
+ req = Net::HTTP::Delete.new(path)
73
+ response = http.request(req)
74
+ end
75
+ else
76
+ Net::HTTP.start(@server, @port) do |http|
77
+ http.send( "request_#{method}".to_sym, path, data.to_json)
78
+ end
79
+ end
80
+ end
81
+
82
+ def method_missing name, *args
83
+ if %w(get post put delete).include? name.to_s
84
+ request name, *args
85
+ else
86
+ super
87
+ end
88
+ end
89
+ end
90
+
91
+ end
@@ -0,0 +1,148 @@
1
+ require 'uuid'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ class Hash
6
+ def to_url_params
7
+ elements = []
8
+ keys.size.times do |i|
9
+ elements << "#{keys[i]}=#{values[i]}"
10
+ end
11
+ elements.join('&')
12
+ end
13
+ end
14
+
15
+ module Hoccer
16
+ class LinccerClient
17
+
18
+ attr_accessor :uuid
19
+
20
+ def self.create
21
+ client = self.new
22
+ client
23
+ end
24
+
25
+ def initialize options = {}
26
+ @uuid = UUID.generate
27
+ @host = options[:host] || "linccer.beta.hoccer.com"
28
+ @port = options[:port] || 80
29
+ end
30
+
31
+ def client_path
32
+ "/v3/clients/#{@uuid}"
33
+ end
34
+
35
+ def environment_path
36
+ "/v3/clients/#{@uuid}/environment"
37
+ end
38
+
39
+ def action_path mode, options = {}
40
+ path = "/v3/clients/#{@uuid}/action/#{mode}"
41
+ path << '?' + options.to_url_params unless options.empty?
42
+
43
+ path
44
+ end
45
+
46
+ def peek_path id = nil
47
+ path = "/v3/clients/#{uuid}/peek"
48
+ if id
49
+ path << '?' + 'group_id=' + id
50
+ end
51
+
52
+ path
53
+ end
54
+
55
+ def update_environment data
56
+ handle_response put(environment_path, data.to_json)
57
+ end
58
+
59
+ def share mode, data
60
+ handle_response put(action_path(mode), data.to_json)
61
+ end
62
+
63
+ def receive mode, options = {}
64
+ handle_response get(action_path(mode, options))
65
+ end
66
+
67
+ def peek id = nil
68
+ handle_response get(peek_path(id))
69
+ end
70
+
71
+ def handle_response response = nil
72
+ if response.nil?
73
+ raise Hoccer::NoServerResponse
74
+ end
75
+
76
+ case response.header.code
77
+
78
+ when "200"
79
+ return parse_json( response.body )
80
+ when "201"
81
+ return true
82
+ when "204"
83
+ return nil
84
+ when "409"
85
+ return nil
86
+ when "412"
87
+ raise Hoccer::InvalidEnvironment
88
+ else
89
+ raise(
90
+ Hoccer::InvalidServerResponse,
91
+ "#{response.header.code} + #{response.header.message} +\n\n #{ response.header.inspect}"
92
+ )
93
+ end
94
+ end
95
+
96
+ def parse_json response_body
97
+ begin
98
+ payload = JSON.parse( response_body )
99
+ rescue JSON::ParserError => e
100
+ raise Hoccer::InvalidJsonResponse, "Could not parse JSON"
101
+ end
102
+ end
103
+
104
+ def follow_redirect
105
+ if @redirect_location
106
+ url = URI.parse @redirect_location
107
+ Net::HTTP.start(url.host, url.port) do |http|
108
+ http.get( url.path )
109
+ end
110
+ end
111
+ end
112
+
113
+ def follow_redirect_threaded
114
+ t = Thread.new do
115
+ follow_redirect
116
+ end
117
+ t.value
118
+ end
119
+
120
+ def delete_environment
121
+ req = Net::HTTP::Delete.new(environment_path)
122
+ Net::HTTP.start(@host, @port) do |http|
123
+ response = http.request(req)
124
+ end
125
+ end
126
+
127
+ def request method, path, data = nil
128
+ Net::HTTP.start(@host, @port) do |http|
129
+ http.send( "request_#{method}".to_sym, path, data )
130
+ end
131
+ end
132
+
133
+ def method_missing name, *args
134
+ if %w(get post put delete).include? name.to_s
135
+ request name, *args
136
+ else
137
+ super
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ class NoServerResponse < ArgumentError; end
144
+ class InvalidServerResponse < ArgumentError; end
145
+ class InvalidJsonResponse < ArgumentError; end
146
+ class InvalidEnvironment < ArgumentError; end
147
+
148
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'geo_store_client'
7
+ require 'linccer_client'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class TestGeoStoreClient < Test::Unit::TestCase
4
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ class TestLinccerClient < Test::Unit::TestCase
4
+ include Hoccer
5
+
6
+ def test_linccer_client
7
+ assert c = LinccerClient.new( :host => "linccer.beta.hoccer.com", :port => 80)
8
+ assert d = LinccerClient.new( :host => "linccer.beta.hoccer.com", :port => 80)
9
+ puts c.inspect
10
+ puts
11
+
12
+ response = c.update_environment(
13
+ :gps => {:longitude => 12.2, :latitude => 14.2, :accuracy => 23.0}
14
+ )
15
+ puts response.inspect
16
+ puts
17
+
18
+ response = d.update_environment(
19
+ :gps => {:longitude => 12.2, :latitude => 14.2, :accuracy => 23.0}
20
+ )
21
+ puts response.inspect
22
+ puts
23
+
24
+ t1 = c.share_threaded( "one-to-one", :foo => "bar" )
25
+ t2 = d.receive( "one-to-one" )
26
+
27
+ puts t2.inspect
28
+ puts t1.value.inspect
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hoccer-api
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 4
9
+ version: 1.0.4
10
+ platform: ruby
11
+ authors:
12
+ - hoccer GmbH
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-27 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " Simple hoccer api clients "
22
+ email: info@hoccer.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.textile
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.textile
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/geo_store_client.rb
38
+ - lib/linccer_client.rb
39
+ - test/helper.rb
40
+ - test/test_geo_store_client.rb
41
+ - test/test_linccer_client.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/hoccer/ruby-api
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Simple hoccer api clients
74
+ test_files:
75
+ - test/helper.rb
76
+ - test/test_geo_store_client.rb
77
+ - test/test_linccer_client.rb