Module: GreenButtonData::Fetchable::ClassMethods

Includes:
Relations, Utilities
Defined in:
lib/green-button-data/fetchable.rb

Instance Method Summary collapse

Methods included from Utilities

#attributes_to_hash, #class_from_name, #epoch_to_time, #first_sunday_of, #last_weekday_of, #normalize_epoch, #nth_weekday_of, #parse_datetime, #weekday_offset

Methods included from Relations

#construct_related_urls

Instance Method Details

#all(url = nil, options = nil) ⇒ Object

Returns all entries

Arguments

  • url (OPTIONAL) - URL to fetch from. If not supplied, it defaults to

    the URLs in configuration.
    
  • options - Options hash



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/green-button-data/fetchable.rb', line 21

def all(url = nil, options = nil)
  if url.is_a?(Hash)
    # Assume it is an options Hash
    options = url
    url = nil
  end

  url ||= url_path_prefix url_options(options)

  @url = url
  @options = options
  return populate_models(fetch(url, options))
end

#feedObject



113
114
115
116
117
118
119
# File 'lib/green-button-data/fetchable.rb', line 113

def feed
  if !options[:reload] && @feed
    @feed
  else
    @feed = fetch url, options
  end
end

#fetch(url = nil, options = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/green-button-data/fetchable.rb', line 83

def fetch(url = nil, options = nil)
  url or raise ArgumentError.new "url is required to fetch data"

  connection_options = {}

  options ||= {}
  connection_options[:ssl] = options[:ssl] if options[:ssl]

  conn = Faraday.new connection_options do |connection|
    connection.response :logger
    connection.adapter Faraday.default_adapter
  end
  conn.authorization :Bearer, options[:token] if options[:token]

  response = conn.get do |req|
    req.url url
    req.params = build_query_params options
  end

  if response.status == 200
    GreenButtonData::Feed.parse response.body
  elsif response.status == 401
    raise "Unauthorized API call; check authorization token and try again"
  elsif response.status == 500
    raise "500 Server Error:\n#{response.body}"
  else
    raise "Status: #{response.status}"
  end
end

#find(id = nil, options = nil) ⇒ Object

Finds an entry that matches the id in the URL of the form: services.greenbuttondata.org/DataCustodian/espi/1_1/resource/model_name/id

Arguments

  • id - ID of the entry content. URL matching a single entry content

    can be supplied in place of ID.
    
  • url (OPTIONAL) - If specified, this URL is used to fetch data in

    place of global configuration.
    
  • options - Options hash

Examples

The following fetches data identically.

ReadingType.find(1, token: “12345678-1024-2048-abcdef001234”) # use global config ReadingType.find(“services.greenbuttondata.org/DataCustodian/espi/1_1/resource/ReadingType/1”, token: “12345678-1024-2048-abcdef001234”) # override global config and use specific url



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/green-button-data/fetchable.rb', line 66

def find(id = nil, options = nil)
  # If +id+ argument is a URL, set the url
  url = if id =~ /\A#{URI::regexp}\z/
    id
  else
    path_prefix = url_path_prefix url_options(options)

    URI.join(path_prefix, "#{id}/").to_s
  end

  return populate_models(fetch(url, options)).first
end

#first(url = nil, options = nil) ⇒ Object

Returns the first item in the ModelCollection from all entries returned by the API endpoint

Arguments

  • url (OPTIONAL) - URL to fetch from. If not supplied, it defaults to

    the URLs in configuration.
    
  • options - Options hash



44
45
46
# File 'lib/green-button-data/fetchable.rb', line 44

def first(url = nil, options = nil)
  return all(url, options).first
end

#last(url = nil, options = nil) ⇒ Object



79
80
81
# File 'lib/green-button-data/fetchable.rb', line 79

def last(url = nil, options = nil)
  return all(url, options).last
end

#optionsObject



129
130
131
# File 'lib/green-button-data/fetchable.rb', line 129

def options
  @options
end

#recordsObject



121
122
123
124
125
126
127
# File 'lib/green-button-data/fetchable.rb', line 121

def records
  if !options[:reload] && @records
    @records
  else
    @records = populate_models(feed)
  end
end

#urlObject



133
134
135
# File 'lib/green-button-data/fetchable.rb', line 133

def url
  @url
end