heater 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/heater.rb ADDED
@@ -0,0 +1,36 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ require 'rest_client'
4
+ require 'json'
5
+ require 'cgi'
6
+ require 'singleton'
7
+
8
+ require 'heater/point_set'
9
+ require 'heater/point'
10
+
11
+ module Heater
12
+ class NoApiKey < Exception
13
+ end
14
+
15
+ class API
16
+ include Singleton
17
+
18
+ API_ENDPOINT = 'http://heater.flatlab.info'.freeze
19
+ VERSION = 1.freeze
20
+
21
+ attr_accessor :api_key
22
+ attr_accessor :api_endpoint
23
+
24
+ def api_endpoint
25
+ @api_endpoint = API_ENDPOINT if @api_endpoint.nil?
26
+ @api_endpoint
27
+ end
28
+
29
+ def get_api_key(url = 'http://localhost:3000')
30
+ result = JSON.parse(RestClient.post("#{self.api_endpoint}/api_keys", :api_key => {:url => url}).body)
31
+ @api_key = result['api_key']
32
+ result
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ module Heater
2
+ class Point
3
+ attr_accessor :lat,:lng
4
+
5
+ def initialize(options={})
6
+ options.each do |key,value|
7
+ self.send("#{key}=",value) if self.respond_to?(key.to_sym)
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,61 @@
1
+ module Heater
2
+ class PointSet
3
+ attr_accessor :id, :name, :created_at, :updated_at, :points
4
+
5
+ def initialize(options={})
6
+ options.each do |key,value|
7
+ self.send("#{key}=",value) if self.respond_to?(key.to_sym)
8
+ end
9
+ end
10
+
11
+ def example_url
12
+ result = JSON.parse(RestClient.get("#{Heater::API.instance.api_endpoint}/point_sets/#{@name}/examples.json?api_key=#{Heater::API.instance.api_key}").body)
13
+ result['public_url']
14
+ end
15
+
16
+ def add_point(lat,lng)
17
+ result = JSON.parse(RestClient.post(@points, :point => {:lat => lat, :lng => lng}, :api_key => Heater::API.instance.api_key).body)
18
+ Point.new(result['point'])
19
+ end
20
+
21
+ # Must be array of points
22
+ def add_points(points)
23
+ return false unless points.is_a?(Array)
24
+ return false unless points.all? {|item| item.is_a?(Point) }
25
+ result = JSON.parse(RestClient.post(@points, :points => points.to_json, :api_key => Heater::API.instance.api_key).body)
26
+ result
27
+ end
28
+
29
+ def remove_point(lat,lng)
30
+ JSON.parse(RestClient.delete("#{@points}/(#{lat},#{lng}).json?api_key=#{Heater::API.instance.api_key}", :content_type => "application/json").body)
31
+ end
32
+
33
+ ### What does this do? :-)
34
+ def get_points_in_set
35
+ JSON.parse(RestClient.get("#{@id}?api_key=#{Heater::API.instance.api_key}").body)
36
+ end
37
+
38
+ def save
39
+ JSON.parse(RestClient.put("#{@id}", :point_set => {:name => @name}, :api_key => Heater::API.instance.api_key ).body)
40
+ end
41
+
42
+ def destroy
43
+ JSON.parse(RestClient.delete("#{@id}?api_key=#{Heater::API.instance.api_key}", :api_key => Heater::API.instance.api_key, :content_type => "application/json").body)
44
+ end
45
+
46
+ def self.create(name)
47
+ result = JSON.parse(RestClient.post("#{Heater::API.instance.api_endpoint}/point_sets",
48
+ :point_set => {:name => name}, :api_key => Heater::API.instance.api_key,
49
+ :content_type => "application/json").body)
50
+
51
+ PointSet.new(result['point_set'])
52
+ end
53
+
54
+ # def self.find(name,api_key,Heater::API.instance.api_endpoint)
55
+ def self.find(name)
56
+ result = JSON.parse(RestClient.get("#{Heater::API.instance.api_endpoint}/point_sets/#{name}?api_key=#{Heater::API.instance.api_key}").body)
57
+ PointSet.new(result['point_set'])
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ ACCESS_KEY = ENV['AWS_ACCESS_KEY'] || ENV['AMAZON_ACCESS_KEY_ID']
2
+ SECRET_KEY = ENV['AWS_SECRET_KEY'] || ENV['AMAZON_SECRET_ACCESS_KEY']
3
+
4
+ unless ACCESS_KEY and SECRET_KEY
5
+ raise RuntimeError, "You must set your AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables to run integration tests"
6
+ end
7
+
8
+ require File.dirname(__FILE__) + '/../spec_helper'
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/heater'
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/units_helper'
2
+
3
+ describe Heater::API do
4
+ before(:each) do
5
+ @api = Heater::API.new({:api_key => 'moo'})
6
+ @valid_api = Heater::API.new( {:api_key => @api.get_api_key['api_key'] })
7
+ end
8
+
9
+ it "should generate an API key" do
10
+ @api.get_api_key().should have_key('public_read')
11
+ end
12
+
13
+ it "should generate a pointset" do
14
+ result = @valid_api.create_point_set('test set')
15
+
16
+ # p result
17
+ result.should have_key('point_set')
18
+ #Nested results
19
+ result = result['point_set']
20
+ result.should have_key('id')
21
+ result.should have_key('points')
22
+ result.should have_key('name')
23
+ end
24
+
25
+ it "should add a point to a set" do
26
+ result = @valid_api.create_point_set('test set for points')
27
+ result = @valid_api.add_point_to_set(result['point_set']['points'], 33.455, -111.909)['point']
28
+
29
+ result.should have_key('id')
30
+ result.should have_key('lat')
31
+ result.should have_key('lng')
32
+ end
33
+
34
+ it "should retrieve points in a set" do
35
+ result = @valid_api.create_point_set('test set for point list')
36
+ @valid_api.add_point_to_set(result['point_set']['points'], 33.455, -111.909)['point']
37
+ result = @valid_api.get_points_in_set(result['point_set']['points'])
38
+ result['points'].size.should == 1
39
+ end
40
+
41
+ it "should not allow more than 100 points" do
42
+ # result = @valid_api.create_point_set('test set for point list')
43
+ # 101.times do
44
+ # @valid_api.add_point_to_set(result['point_set']['points'], 33.455, -111.909)['point']
45
+ # end
46
+
47
+ end
48
+
49
+ it "should remove points from a set" do
50
+ result = @valid_api.create_point_set('test set for point list')
51
+ @valid_api.add_point_to_set(result['point_set']['points'], 33.455, -111.909)['point']
52
+ point_data = @valid_api.get_points_in_set(result['point_set']['points'])
53
+ @valid_api.delete_point_in_set(point_data['points'].first['id'])
54
+ point_data = @valid_api.get_points_in_set(result['point_set']['points'])
55
+
56
+ point_data['points'].should be_empty
57
+ end
58
+
59
+ end
@@ -0,0 +1,3 @@
1
+ API_VERSION = "moo"
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heater
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Chris Chandler
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-08 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ - 2
31
+ version: 1.4.2
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: chris@flatterline.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/heater.rb
44
+ - lib/heater/point.rb
45
+ - lib/heater/point_set.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/cchandler/heater
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A simple gem for interacting with heater.
76
+ test_files:
77
+ - spec/units/api_spec.rb
78
+ - spec/integrations/integrations_helper.rb
79
+ - spec/spec_helper.rb
80
+ - spec/units/units_helper.rb