Module: Cimis

Defined in:
lib/cimis.rb,
lib/cimis/station.rb,
lib/cimis/version.rb,
lib/cimis/data_point.rb,
lib/cimis/station_data.rb

Defined Under Namespace

Classes: DataPoint, MissingAttributeError, Station, StationData

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_keyObject

Returns the value of attribute app_key.



13
14
15
# File 'lib/cimis.rb', line 13

def app_key
  @app_key
end

Class Method Details

.camel_case_lower(str) ⇒ Object



45
46
47
48
49
# File 'lib/cimis.rb', line 45

def camel_case_lower(str)
  str.split('_').inject([]) do |buffer,e| 
    buffer.push(buffer.empty? ? e : e.capitalize)
  end.join
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Cimis)

    the object that the method was called on

Raises:



15
16
17
18
# File 'lib/cimis.rb', line 15

def configure
  yield self
  raise MissingAttributeError, "You must include the app key" unless app_key
end

.connectionObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/cimis.rb', line 20

def connection
  @connect ||= Faraday.new do |f|
    f.adapter :net_http
    f.url_prefix = "http://et.water.ca.gov/api"
    f.headers["User-Agent"] = "Cimis Ruby v#{Cimis::VERSION}"
    f.headers["Content-Type"] = "application/json"
    f.headers["Accept"] = "*/*"
    f.response :json, content_type: /\bjson$/
  end
end

.data(options = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cimis.rb', line 31

def data(options = {})
  options.merge!({app_key: app_key})
  response = connection.get("data?#{to_query(options)}")
  response.body["Data"]["Providers"].first["Records"].map do |record|
    StationData.new(record)
  end
end

.symbolize_keys(params) ⇒ Object



61
62
63
64
65
# File 'lib/cimis.rb', line 61

def symbolize_keys(params)
  {}.tap do |hsh|
    params.keys.each { |k| hsh[underscore(k).to_sym] = params[k] }
  end
end

.to_query(params = {}) ⇒ Object



39
40
41
42
43
# File 'lib/cimis.rb', line 39

def to_query(params = {})
  params.collect do |key, value|
    "#{camel_case_lower(key.to_s)}=#{value}"
  end.sort * "&" 
end

.underscore(str) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/cimis.rb', line 51

def underscore(str)
  word = str.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end