calculated 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +290 -0
- data/lib/calculated/models/formula_input.rb +1 -0
- data/lib/calculated/version.rb +1 -1
- metadata +4 -3
data/README.md
ADDED
@@ -0,0 +1,290 @@
|
|
1
|
+
#Calculated#
|
2
|
+
|
3
|
+
Calculated allows you to interact with the [CarbonCalculated API](http://www.carboncalculated.com/platform) in
|
4
|
+
a ruby way.
|
5
|
+
|
6
|
+
"The Carbon Calculated platform is a freely accessible aggregation system containing up to the minute data on carbon emissions and emission calculation"
|
7
|
+
|
8
|
+
* Idiomatic Ruby
|
9
|
+
* Concrete classes and methods modelling CarbonCalculated data
|
10
|
+
|
11
|
+
Please visit [The Blog](http://blog.carboncalculated.com) For Developer examples and information
|
12
|
+
|
13
|
+
DOCS Are located [here](http://rdoc.info/projects/hookercookerman/calculated/blob/dd7ed3f73d987492486abb7091f6321e8b04443e)
|
14
|
+
|
15
|
+
Super Simple Example Apps Using the API
|
16
|
+
|
17
|
+
[http://miniapps.carboncalculated.com/](http://miniapps.carboncalculated.com/)
|
18
|
+
|
19
|
+
##Getting Started##
|
20
|
+
|
21
|
+
Get An API KEY
|
22
|
+
|
23
|
+
contact support@carboncalculated.com
|
24
|
+
|
25
|
+
You can install it as:
|
26
|
+
|
27
|
+
sudo gem install calculated (dont use sudo if using RVM)
|
28
|
+
|
29
|
+
##Creating A Session##
|
30
|
+
|
31
|
+
@session = Calculated::Session.create(:api_key => "your_api_key")
|
32
|
+
|
33
|
+
##Overwriting defaults##
|
34
|
+
|
35
|
+
**These are the defaults**
|
36
|
+
|
37
|
+
caching # => true (boolean)
|
38
|
+
expires_in # => 60*60*24 (seconds)
|
39
|
+
cache # => Moneta::Memory.new "(Moneta Supported Cache) http://github.com/wycats/moneta
|
40
|
+
server # => "api.carboncalculated.com" (string)
|
41
|
+
api_version #=> "v1" (string)
|
42
|
+
logging # => true (boolean)
|
43
|
+
|
44
|
+
**This is overriding the defaults**
|
45
|
+
|
46
|
+
@session = Calculated::Session.create(
|
47
|
+
:api_key => "your_api_key",
|
48
|
+
:cache => Moneta::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'], :bucket => 'carbon')
|
49
|
+
:expires_in => 60*60*24*14
|
50
|
+
)
|
51
|
+
|
52
|
+
This has created a session with S3 Moneta cache that expires in 14 days
|
53
|
+
|
54
|
+
##Basic Usage##
|
55
|
+
|
56
|
+
Via using the CarbonCalculated Browser http://browser.carboncalculated.com/ you can easily see what is available for you to interact with in terms of
|
57
|
+
data and calculations;
|
58
|
+
|
59
|
+
Now that you have a session you can call methods on the session to interact with carbon calculated api
|
60
|
+
|
61
|
+
**Example**
|
62
|
+
|
63
|
+
**Very simple materials Calculator**
|
64
|
+
|
65
|
+
Lets first got to the calculator on the browser (click here to see what I mean http://browser.carboncalculated.com/calculators/4bab7e4ff78b122cdd000004/computations/4bab7e64f78b122cdd000005"
|
66
|
+
|
67
|
+
@answer = @session.answer_from_computation("4bab7e64f78b122cdd000005", {"material_category"=>"4bf42d8046a95925b5000efb", "type_of_material"=>"4bf42d8046a95925b5000f40", "material"=>"4bf42d8046a95925b5000f2a", "amount_of_material"=>"10", "formula_input_name"=>"emissions_by_kg"})
|
68
|
+
@answer.calculations["co2"]["value"]
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
OR
|
73
|
+
|
74
|
+
|
75
|
+
@answer = @session.answer_from_calculator("4bab7e4ff78b122cdd000004", {"material_category"=>"4bf42d8046a95925b5000efb", "type_of_material"=>"4bf42d8046a95925b5000f40", "material"=>"4bf42d8046a95925b5000f2a", "amount_of_material"=>"10", "formula_input_name"=>"emissions_by_kg"})
|
76
|
+
@answer.calculations["co2"]["value"]
|
77
|
+
|
78
|
+
This will return a Calculated::Models::Answer Object
|
79
|
+
|
80
|
+
|
81
|
+
The reason that you can get an answer from either a calculator or a computation is the power of the validations; its cool
|
82
|
+
trust me.
|
83
|
+
|
84
|
+
##Object Template Api##
|
85
|
+
|
86
|
+
A Object Template is a template for the building of generic objects; these contain information about what
|
87
|
+
all the object for the object template should contain; as well as getting relatable categories which can be
|
88
|
+
used to filter to specific generic objects which you can then later be used in answer calls
|
89
|
+
|
90
|
+
@object_templates = @session.object_templates # returns an Array of ObjectTemplate Objects
|
91
|
+
@object_templates[0].name
|
92
|
+
@object_templates[0].characteristics
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
@object_template = @session.generic_objects_for_object_template("car") # returns an ObjectTemplate Object with paginate GenericObjects
|
98
|
+
@object_template.generic_objects[0].identifier
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
params can be given for pagination
|
105
|
+
@session.object_templates(:page => 50, :per_page => 3)
|
106
|
+
@session.generic_objects_for_object_template("car", :page => 50, :per_page => 3)
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
# Getting object template with its relatable categories
|
113
|
+
# this will return ObjectTemplate with Relatable Category Objects
|
114
|
+
@object_template = @session.relatable_categories_for_object_template("car", "manufacture")
|
115
|
+
@object_template.relatable_categories[0].name
|
116
|
+
@object_template.relatable_categories[0].relatable_categories
|
117
|
+
|
118
|
+
# yes relatable category know about other ones very cool when building "successive drop downs"[http://blog.carboncalculated.com/2010/06/16/creating-successive-drop-downs/]
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
# @param [String] name # the object_template name ie "car", "airport", "material"
|
125
|
+
# @param [String] filter # the string you want to filter
|
126
|
+
#
|
127
|
+
@object_template = @session.generic_objects_for_object_template_with_filter("airport", "london")
|
128
|
+
@object_template.generic_objects[0].identifier
|
129
|
+
|
130
|
+
##Object Template Object##
|
131
|
+
|
132
|
+
property :id # [String]
|
133
|
+
property :template_name # [String]
|
134
|
+
property :identifier # [String]
|
135
|
+
property :characteristics # [Array<Calculated::Models::Characteristic>]
|
136
|
+
property :formula_inputs # [Array<Calculated::Models::FormulaInput>]
|
137
|
+
|
138
|
+
##Generic Objects API##
|
139
|
+
|
140
|
+
A Generic Object represents and object that is used in computations OR calculators; a computation will always
|
141
|
+
need at least one object to talk about; The "formula_inputs" of an object are the values that are used
|
142
|
+
in formulas and can be versioned to specific times for equations that change over time.
|
143
|
+
|
144
|
+
This is a list of car for instance CLICK http://browser.carboncalculated.com/object_templates/car/generic_objects
|
145
|
+
|
146
|
+
Finding and using generic objects is a major part of the platform apart from the actual calculations themselves
|
147
|
+
|
148
|
+
# Finding generic objects
|
149
|
+
@generic_objects = @session.generic_objects
|
150
|
+
@generic_objects[0].identifier
|
151
|
+
|
152
|
+
|
153
|
+
# Specific Generic Object
|
154
|
+
@generic_object = @session.generic_object("4c370c11ae2b7b418e00232c")
|
155
|
+
@generic_object.identifier
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
# Getting just the "Formula Inputs" of a generic Object
|
160
|
+
@formula_inputs = @session.formula_inputs_for_generic_object("4c370c11ae2b7b418e00232c")
|
161
|
+
@formula_inputs[0].values
|
162
|
+
|
163
|
+
##Generic Object (Object)##
|
164
|
+
|
165
|
+
# properties
|
166
|
+
property :id # [String]
|
167
|
+
property :template_name # [String]
|
168
|
+
property :identifier # [String]
|
169
|
+
property :characteristics # [Array<Calculated::Models::Characteristic>]
|
170
|
+
property :formula_inputs # [Array<Calculated::Models::FormulaInput>]
|
171
|
+
|
172
|
+
##Formula Input Object##
|
173
|
+
|
174
|
+
# properties
|
175
|
+
property :id # [String]
|
176
|
+
property :values # [Hash]
|
177
|
+
property :name # [String]
|
178
|
+
property :base_unit # [String]
|
179
|
+
property :active_at # [Time] yes time
|
180
|
+
property :main_source_id # [String]
|
181
|
+
property :group_name # [String]
|
182
|
+
property :input_units # [String]
|
183
|
+
property :label_input_units # [String]
|
184
|
+
property :model_state # [String]
|
185
|
+
|
186
|
+
##Relatable Category API##
|
187
|
+
|
188
|
+
Relatable Categories are created when an generic object has been created; And not before hand; Hence
|
189
|
+
in theory a relatable category is only there when objects are there and not before; Which means
|
190
|
+
that you should never have a problem of a category existing just for the fun of it;
|
191
|
+
|
192
|
+
A relatable category will know all the objects that have its name and related_attribute; In a nut shell you can use relatable
|
193
|
+
category to filter and find related objects easily and effectively;
|
194
|
+
|
195
|
+
Also a relatable category will know of other relatable categories of the created object; so this allows for
|
196
|
+
super easy drop downs to create easy filter; the blog should help explain this further or just send me an email;
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
# Getting related objects from an ARRAY OF Relatable Category IDS USED in intersecting the related objects
|
201
|
+
# therefore its a really cool way to get filtered results
|
202
|
+
#
|
203
|
+
# @param object_template_name ie "car", "material"
|
204
|
+
# @param relatable_category_ids # Array #
|
205
|
+
result = @session.related_objects_from_relatable_categories("material", ["4bf42d8a46a95925b500199a"])
|
206
|
+
result["4bf42d8b46a95925b5001a0c"] # Partial Board
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
# Getting Related Categories from a related category
|
213
|
+
#
|
214
|
+
# @param Relatable Category ID
|
215
|
+
# @param related_attribute
|
216
|
+
result = @session.related_categories_from_relatable_category("4bf42d8a46a95925b500199a", "material_type")
|
217
|
+
results["4bf42d8b46a95925b50019c7"] # hardboard
|
218
|
+
|
219
|
+
|
220
|
+
##Relatable Category Object##
|
221
|
+
|
222
|
+
# properties
|
223
|
+
property :id # [String]
|
224
|
+
property :name # [String]
|
225
|
+
property :related_attribute # [String]
|
226
|
+
property :related_object_name # [String]
|
227
|
+
# @example
|
228
|
+
# "emission_source" => {
|
229
|
+
# {
|
230
|
+
# "4c349f6068fe5434960178c2" => "flaring and venting",
|
231
|
+
# "4c349f6068fe54349601791f" => "fugitives",
|
232
|
+
# }
|
233
|
+
# }
|
234
|
+
property :related_categories # [Hash{name => <Hash{:id => identifier}}]
|
235
|
+
|
236
|
+
|
237
|
+
##Answer API##
|
238
|
+
|
239
|
+
This is the crux of the api; this is where co2, co2e etc results can be found;
|
240
|
+
|
241
|
+
The best and easiest way to found what information you need to get a valid answer is using the browser["http://browser.carboncalculated.com"]
|
242
|
+
Once you have found what parameters you need to send you can then do exactly that;
|
243
|
+
|
244
|
+
A Answer Object will give you vaste information on what it used to get the answer; You will be told the all the objects that were used in the calculation
|
245
|
+
all the used_formula_inputs and the sources that where used to gain the information to make the calculation even possible.
|
246
|
+
|
247
|
+
|
248
|
+
@answer = @session.answer_from_computation("4bab7e64f78b122cdd000005", {"material_category"=>"4bf42d8046a95925b5000efb", "type_of_material"=>"4bf42d8046a95925b5000f40", "material"=>"4bf42d8046a95925b5000f2a", "amount_of_material"=>"10", "formula_input_name"=>"emissions_by_kg"})
|
249
|
+
@answer.calculations["co2"]["value"]
|
250
|
+
|
251
|
+
*Errors
|
252
|
+
|
253
|
+
If you don send the correct parameters in an answer you will be told where you are going wrong
|
254
|
+
|
255
|
+
@answer = @session.answer_from_computation("4bab7e64f78b122cdd000005", {"material_category"=>"4bf42d8046a95925b5000efb", "type_of_material"=>"4bf42d8046a95925b5000f40", "material"=>"4bf42d8046a95925b5000f2a", "formula_input_name"=>"emissions_by_kg"})
|
256
|
+
@answer.valid? # false
|
257
|
+
|
258
|
+
@answer.errors
|
259
|
+
|
260
|
+
"amount_of_material" =>["can't be blank", "is not a number"]
|
261
|
+
|
262
|
+
##Answer Object##
|
263
|
+
|
264
|
+
# answer.calculations["co2"]["value"] == 10
|
265
|
+
# answer.calculations["co2"]["units"] == "kg"
|
266
|
+
#
|
267
|
+
property :calculations # [Hash]
|
268
|
+
property :object_references # [Hash]
|
269
|
+
property :used_global_computations # [Hash]
|
270
|
+
property :calculator_id # [String]
|
271
|
+
property :computation_id # [String]
|
272
|
+
property :answer_set_id # [String]
|
273
|
+
property :source # [Calculated::Models::Source]
|
274
|
+
property :errors # [Hash]
|
275
|
+
|
276
|
+
##Source Object##
|
277
|
+
|
278
|
+
# properties
|
279
|
+
property :id # [String]
|
280
|
+
property :description # [String]
|
281
|
+
property :main_source_ids # [Array<String>]
|
282
|
+
property :external_url # [String]
|
283
|
+
property :wave_id # [String]
|
284
|
+
|
285
|
+
##Bugs and Feedback##
|
286
|
+
|
287
|
+
If you discover any bugs or want to drop a line support@carboncalculated.com
|
288
|
+
|
289
|
+
Copyright (c) 2010 Richard Hooker http://blog.carboncalculated.com
|
290
|
+
See the attached MIT License.
|
data/lib/calculated/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Richard Hooker
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-07-
|
17
|
+
date: 2010-07-21 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/calculated.rb
|
117
117
|
- LICENSE
|
118
118
|
- CHANGELOG.md
|
119
|
+
- README.md
|
119
120
|
- ROADMAP.md
|
120
121
|
has_rdoc: true
|
121
122
|
homepage: http://github.com/hookercookerman/calculated
|