hookercookerman-amee 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest.txt +0 -1
- data/amee.gemspec +1 -1
- data/features/profile/create.profile.feature +6 -1
- data/features/step_definitions/profile_item_steps.rb +1 -1
- data/features/step_definitions/profile_steps.rb +7 -0
- data/lib/amee/data_api/data_category.rb +4 -2
- data/lib/amee/parser.rb +17 -0
- data/lib/amee/profile_api/profile.rb +3 -3
- data/lib/amee/profile_api/profile_item.rb +2 -2
- data/lib/amee/service.rb +4 -4
- data/lib/amee/session.rb +24 -12
- data/lib/amee.rb +1 -1
- data/spec/service_spec.rb +1 -1
- data/spec/session_spec.rb +1 -1
- metadata +1 -1
data/Manifest.txt
CHANGED
data/amee.gemspec
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
Feature: Creating a Profile
|
2
2
|
In order to keep a container for an object | user for calculating co2 emissions
|
3
3
|
As a amee developer
|
4
|
-
I want to be
|
4
|
+
I want to be able to create a profile
|
5
5
|
|
6
6
|
|
7
7
|
Scenario: Create a amee profile
|
8
8
|
Given I have a valid amee session
|
9
9
|
When I create an amee profile
|
10
10
|
Then I should have a valid amee profile
|
11
|
+
|
12
|
+
|
13
|
+
Scenario: Create a amee profile with representation none
|
14
|
+
Given I have a valid amee session
|
15
|
+
When I create an amee profile with representation false
|
11
16
|
|
12
17
|
|
13
18
|
|
@@ -22,7 +22,7 @@ When(/^I create a profile item with data_item_uid: \"(\S+)\" the path: \"(.*)\"/
|
|
22
22
|
FakeWeb.register_uri(:post, "http://stage.amee.com#{path}?kWhPerMonth=10&dataItemUid=66056991EE23&representation=full",
|
23
23
|
:file => File.join(AMEE_FIXTURE_PATH, "/profiles/BB1BDB4FDD77/home/energy/quantity/response.json")
|
24
24
|
)
|
25
|
-
@models["profile_category"] = @amee_session.create_profile_item(path, uid, :
|
25
|
+
@models["profile_category"] = @amee_session.create_profile_item(path, uid, :representation => true, :query => {:kWhPerMonth => "10"})
|
26
26
|
end
|
27
27
|
|
28
28
|
When(/^I update the profile item with: distance=400$/) do
|
@@ -5,6 +5,13 @@ When(/^I create an amee profile$/) do
|
|
5
5
|
@models["profile"] = @amee_session.create_profile
|
6
6
|
end
|
7
7
|
|
8
|
+
When(/^I create an amee profile with representation false$/) do
|
9
|
+
FakeWeb.register_uri(:post, "http://stage.amee.com/profiles?profile=true",
|
10
|
+
:file => File.join(AMEE_FIXTURE_PATH, "profiles/profile.json")
|
11
|
+
)
|
12
|
+
@models["profile"] = @amee_session.create_profile
|
13
|
+
end
|
14
|
+
|
8
15
|
Then(/^I should have a valid amee profile$/) do
|
9
16
|
@models["profile"].name.should == "180D73DA5229"
|
10
17
|
@models["profile"].uid.should == "180D73DA5229"
|
@@ -10,8 +10,10 @@ module Amee
|
|
10
10
|
:data_items => {:class => Amee::DataApi::DataItem}
|
11
11
|
item_populators :item_definition => {:class => Amee::DataApi::ItemDefinition}
|
12
12
|
|
13
|
-
# essentially the parent category
|
14
|
-
|
13
|
+
# data_category; essentially the parent category
|
14
|
+
# raw_data_items; the raw hash of data_items found form the data_category
|
15
|
+
# this allows quick easy access to the data_item_values; if needed
|
16
|
+
attr_accessor :data_category, :pager, :raw_data_items
|
15
17
|
|
16
18
|
# we can only drill with a data_category that has data_items
|
17
19
|
#
|
data/lib/amee/parser.rb
CHANGED
@@ -16,8 +16,10 @@ module Amee
|
|
16
16
|
pager = {"pager" => data["children"].delete("pager")}
|
17
17
|
categories = {"data_categories" => data["children"].delete("dataCategories")}
|
18
18
|
data_items = {"data_items" => data["children"]["dataItems"].delete("rows")} if data["children"]["dataItems"]
|
19
|
+
raw_data_items = {"raw_data_items" => data_items}
|
19
20
|
category.merge(categories).
|
20
21
|
merge(data_items).
|
22
|
+
merge(raw_data_items).
|
21
23
|
merge(pager).
|
22
24
|
merge({"resource_path" => data["path"]})
|
23
25
|
end
|
@@ -109,6 +111,18 @@ module Amee
|
|
109
111
|
end
|
110
112
|
end
|
111
113
|
|
114
|
+
class ProfileItemLocation < Parser
|
115
|
+
def self.process(data)
|
116
|
+
data.headers["location"].first
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class HttpOk < Parser
|
121
|
+
def self.process(data)
|
122
|
+
true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
112
126
|
|
113
127
|
class Raw < Parser
|
114
128
|
def self.process(data)
|
@@ -118,6 +132,9 @@ module Amee
|
|
118
132
|
|
119
133
|
class Parser
|
120
134
|
PARSERS = {
|
135
|
+
# @todo consitant naming
|
136
|
+
"ok" => HttpOk,
|
137
|
+
'profile_item.location' => ProfileItemLocation,
|
121
138
|
'data.category' => DataCategory,
|
122
139
|
'data.item' => DataItem,
|
123
140
|
"data.item_value" => DataItemValue,
|
@@ -15,7 +15,7 @@ module Amee
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def create_profile_item(path_or_category, data_item_uid, params = {})
|
18
|
-
path = full_path + (path_or_category.is_a?(String) ? "#{path_or_category}" : "#{path_or_category.
|
18
|
+
path = full_path + (path_or_category.is_a?(String) ? "#{path_or_category}" : "#{path_or_category.resource_path}")
|
19
19
|
session.create_profile_item(path, data_item_uid, params)
|
20
20
|
end
|
21
21
|
|
@@ -27,8 +27,8 @@ module Amee
|
|
27
27
|
session.get_profile_category((full_path + path), options)
|
28
28
|
end
|
29
29
|
|
30
|
-
def update_profile_item(path,
|
31
|
-
session.update_profile_item((full_path + path), :
|
30
|
+
def update_profile_item(path, query)
|
31
|
+
session.update_profile_item((full_path + path), :query => query)
|
32
32
|
end
|
33
33
|
|
34
34
|
def destroy
|
@@ -17,13 +17,13 @@ module Amee
|
|
17
17
|
# @todo This currently will not work as profile data_item brings back a profile_category
|
18
18
|
# How stupid is that!
|
19
19
|
def self.create(session, path, data_item_uid, fields ={})
|
20
|
-
hash = session.create_profile_item(path, data_item_uid, {:
|
20
|
+
hash = session.create_profile_item(path, data_item_uid, {:query => fields, :raw_response => true})
|
21
21
|
self.from_hash(hash, session)
|
22
22
|
end
|
23
23
|
|
24
24
|
def update(fields = {})
|
25
25
|
session.api_call(:put, "profile_item", self.full_path,
|
26
|
-
:query => fields
|
26
|
+
:query => fields) do |response|
|
27
27
|
populate_from_hash!(response)
|
28
28
|
self
|
29
29
|
end
|
data/lib/amee/service.rb
CHANGED
@@ -42,7 +42,7 @@ module Amee
|
|
42
42
|
def perform_request(type, method, path, options)
|
43
43
|
attach_headers(options)
|
44
44
|
response = self.class.send(type, path, options)
|
45
|
-
check_response(response
|
45
|
+
check_response(response)
|
46
46
|
Parser.parse(method, response)
|
47
47
|
rescue Errno::ECONNREFUSED, SocketError
|
48
48
|
raise Amee::Session::ServerNotFound, "Amee Server could not be found"
|
@@ -71,8 +71,8 @@ module Amee
|
|
71
71
|
# @raise [Amee::UnAuthorized] if a auth_token could not be generated
|
72
72
|
# @raise [Amee::SessionExpired] if a we get a 401
|
73
73
|
# @raise [Amee::UnknownError] if we get something strange
|
74
|
-
def check_response(
|
75
|
-
case code
|
74
|
+
def check_response(response)
|
75
|
+
case response.code
|
76
76
|
when 200, 201
|
77
77
|
true
|
78
78
|
when 403
|
@@ -82,7 +82,7 @@ module Amee
|
|
82
82
|
when 404
|
83
83
|
raise Amee::Session::NotFound.new("resource was not found")
|
84
84
|
else
|
85
|
-
raise Amee::Session::UnknownError.new("super strange type unknown error")
|
85
|
+
raise Amee::Session::UnknownError.new("super strange type unknown error: #{response.code} :body #{response.body}")
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
data/lib/amee/session.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
module Amee
|
2
2
|
class Session
|
3
3
|
|
4
|
-
class
|
5
|
-
class
|
6
|
-
class
|
7
|
-
class
|
8
|
-
class
|
9
|
-
class
|
10
|
-
class
|
4
|
+
class AmeeError < StandardError ;end
|
5
|
+
class Expired < AmeeError; end
|
6
|
+
class UnAuthorized < AmeeError; end
|
7
|
+
class NotAuthenticated < AmeeError; end
|
8
|
+
class PermissionDenied < AmeeError; end
|
9
|
+
class NotFound < AmeeError; end
|
10
|
+
class ServerNotFound < AmeeError; end
|
11
|
+
class UnknownError < AmeeError; end
|
11
12
|
attr_accessor :auth_token, :cache, :service
|
12
13
|
|
13
14
|
def self.create(username = nil, password = nil)
|
@@ -147,20 +148,30 @@ module Amee
|
|
147
148
|
end
|
148
149
|
end
|
149
150
|
|
151
|
+
# @note representation FULL is not working on AMEE VERSION 2.0
|
152
|
+
# ticket has been raised
|
153
|
+
#
|
150
154
|
# @return [Amee::ProfileApi::ProfileItem]
|
151
155
|
def update_profile_item(path, options = {})
|
152
156
|
@cache.clear
|
153
|
-
api_call(:put,
|
157
|
+
api_call(:put, options[:representation] ? "profile_item" : "ok", path,
|
158
|
+
:body => {:representation => options[:representation] ? "full" : ""}.merge(options[:query])) do |response|
|
159
|
+
return response if (options[:raw_response] || !response.is_a?(Hash))
|
154
160
|
Amee::ProfileApi::ProfileItem.from_hash(response, self)
|
155
161
|
end
|
156
162
|
end
|
157
163
|
|
164
|
+
|
165
|
+
# If you want a full representation; which is the profile category by the way
|
166
|
+
# then pass :representation => true in the options hash
|
167
|
+
#
|
158
168
|
# @return [Amee::ProfileApi::ProfileCategory]
|
169
|
+
# @return [String] the location of the new resource if representation is passed in options
|
159
170
|
def create_profile_item(path, uid, options = {})
|
160
171
|
@cache.clear
|
161
|
-
api_call(:post, "create.profile_item", path,
|
162
|
-
:query => {:dataItemUid => uid, :representation => "full"}.merge(options[:
|
163
|
-
return response if options[:
|
172
|
+
api_call(:post, options[:representation] ? "create.profile_item" : "profile_item.location", path,
|
173
|
+
:query => {:dataItemUid => uid, :representation => options[:representation] ? "full" : ""}.merge(options[:query])) do |response|
|
174
|
+
return response if (options[:raw_response] || !response.is_a?(Hash))
|
164
175
|
Amee::ProfileApi::ProfileCategory.from_hash(response, self) do |profile_category|
|
165
176
|
profile_category.lazy_loaded = true
|
166
177
|
end
|
@@ -233,8 +244,9 @@ module Amee
|
|
233
244
|
end
|
234
245
|
|
235
246
|
protected
|
247
|
+
require 'digest/md5'
|
236
248
|
def cache_key(path, params = {})
|
237
|
-
path + params.map { |key,value| "#{key}=#{value}" }.join('/')
|
249
|
+
Digest::MD5.hexdigest(path + params.map { |key,value| "#{key}=#{value}" }.join('/'))
|
238
250
|
end
|
239
251
|
|
240
252
|
end
|
data/lib/amee.rb
CHANGED
data/spec/service_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe "Given FakeRequests Are Setup" do
|
|
16
16
|
|
17
17
|
it "should send a request with headers of AuthToken And Accept" do
|
18
18
|
Amee::Service.should_receive(:get).
|
19
|
-
with("/path", {:headers => {"authToken" => "whatever token", "Accept" => Amee::Config[:accept]}}).
|
19
|
+
with("http://stage.amee.com/path", {:headers => {"authToken" => "whatever token", "Accept" => Amee::Config[:accept]}}).
|
20
20
|
and_return(mock("response",:code => 200))
|
21
21
|
@service.get("raw", "/path")
|
22
22
|
end
|
data/spec/session_spec.rb
CHANGED
@@ -38,7 +38,7 @@ describe "Given we have setup the fake requests" do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should try to use default cache expires_in if none given" do
|
41
|
-
@amee_session.cache.should_receive(:store).with("/path", "result", {:expires_in=>86400})
|
41
|
+
@amee_session.cache.should_receive(:store).with(Digest::MD5.hexdigest("/path"), "result", {:expires_in=>86400})
|
42
42
|
@amee_session.store_resource("/path", "result")
|
43
43
|
end
|
44
44
|
end
|