Module: Carbon
- Defined in:
- lib/carbon.rb,
lib/carbon/query.rb,
lib/carbon/shell.rb,
lib/carbon/version.rb,
lib/carbon/registry.rb,
lib/carbon/query_pool.rb,
lib/carbon/shell/emitter.rb
Defined Under Namespace
Modules: ClassMethods Classes: QueryPool, Registry
Constant Summary
- DOMAIN =
'http://impact.brighterplanet.com'.freeze
- CONCURRENCY =
16
- VERSION =
"3.0.1"
- @@key =
nil
- @@domain =
nil
Class Method Summary collapse
-
.domain ⇒ String
Where we send queries.
-
.domain=(domain) ⇒ nil
Set an alternate API endpoint.
-
.key ⇒ String
Get the key you've set.
-
.key=(key) ⇒ nil
Set the Brighter Planet API key that you can get from keys.brighterplanet.com.
-
.query(*args) ⇒ Object
Get impact estimates from Brighter Planet CM1; low-level method that does not require you to define ClassMethods#emit_as blocks; just pass emitter/param or objects that respond to #as_impact_query.
Instance Method Summary collapse
-
#as_impact_query(extra_params = {}) ⇒ Array
A query like what you could pass into Carbon.query.
-
#impact(extra_params = {}) ⇒ Hashie::Mash
Get an impact estimate from Brighter Planet CM1; high-level convenience method that requires a ClassMethods#emit_as block.
Class Method Details
.domain ⇒ String
Where we send queries.
49 50 51 |
# File 'lib/carbon.rb', line 49 def Carbon.domain @@domain || DOMAIN end |
.domain=(domain) ⇒ nil
Set an alternate API endpoint. You probably shouldn't do this.
41 42 43 44 |
# File 'lib/carbon.rb', line 41 def Carbon.domain=(domain) @@domain = domain nil end |
.key ⇒ String
Get the key you've set.
32 33 34 |
# File 'lib/carbon.rb', line 32 def Carbon.key @@key end |
.key=(key) ⇒ nil
Set the Brighter Planet API key that you can get from keys.brighterplanet.com
24 25 26 27 |
# File 'lib/carbon.rb', line 24 def Carbon.key=(key) @@key = key nil end |
.query(emitter, params) ⇒ Hashie::Mash .query(obj) ⇒ Hashie::Mash .query(array) ⇒ Hash{Object => Hashie::Mash}
We make up to 16 requests concurrently (hardcoded, per the Brighter Planet Terms of Service) and it can be more than 90% faster than running queries serially!
[emitter, params] is called a “plain query.”
Get impact estimates from Brighter Planet CM1; low-level method that does not require you to define Carbon::ClassMethods#emit_as blocks; just pass emitter/param or objects that respond to #as_impact_query.
Return values are Hashie::Mash objects because they are a simple way to access a deeply nested response.
Here's a map of what's included in a response:
certification
characteristics.{}.description
characteristics.{}.object
compliance.[]
decisions.{}.description
decisions.{}.methodology
decisions.{}.object
emitter
equivalents.{}
errors.[]
methodology
scope
timeframe.endDate
timeframe.startDate
137 138 139 140 |
# File 'lib/carbon.rb', line 137 def Carbon.query(*args) raise ::ArgumentError, "Don't pass a block directly - instead use Carbon.query(array).each (for example)." if block_given? Query.perform(*args) end |
Instance Method Details
#as_impact_query(extra_params = {}) ⇒ Array
A query like what you could pass into Carbon.query.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/carbon.rb', line 208 def as_impact_query(extra_params = {}) registration = Registry.instance[self.class.name] params = registration.characteristics.inject({}) do |memo, (method_id, )| k = .has_key?(:as) ? [:as] : method_id if .has_key?(:key) k = "#{k}[#{[:key]}]" end v = if .has_key?(:blk) [:blk].call self else send method_id end if v.present? memo[k] = v end memo end params.merge! extra_params if Carbon.key and not params.has_key?(:key) params[:key] = Carbon.key end [ registration.emitter, params ] end |
#impact(extra_params = {}) ⇒ Hashie::Mash
Get an impact estimate from Brighter Planet CM1; high-level convenience method that requires a Carbon::ClassMethods#emit_as block.
You get this when you include Carbon in a class.
See query for an explanation of the return value, a Hashie::Mash.
258 259 260 261 |
# File 'lib/carbon.rb', line 258 def impact(extra_params = {}) plain_query = as_impact_query extra_params Carbon.query(*plain_query) end |