Module: GreenButtonData::Fetchable::ClassMethods
Instance Method Summary collapse
-
#all(url = nil, options = nil) ⇒ Object
Returns all entries.
- #feed ⇒ Object
- #fetch(url = nil, options = nil) ⇒ Object
-
#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.
-
#first(url = nil, options = nil) ⇒ Object
Returns the first item in the ModelCollection from all entries returned by the API endpoint.
- #last(url = nil, options = nil) ⇒ Object
- #options ⇒ Object
- #records ⇒ Object
- #url ⇒ Object
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
Instance Method Details
#all(url = nil, options = nil) ⇒ Object
Returns all entries
Arguments
-
url(OPTIONAL) - URL to fetch from. If not supplied, it defaults tothe 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, = nil) if url.is_a?(Hash) # Assume it is an options Hash = url url = nil end url ||= url_path_prefix () @url = url = return populate_models(fetch(url, )) end |
#feed ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/green-button-data/fetchable.rb', line 113 def feed if ![:reload] && @feed @feed else @feed = fetch url, 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, = nil) url or raise ArgumentError.new "url is required to fetch data" = {} ||= {} [:ssl] = [:ssl] if [:ssl] conn = Faraday.new do |connection| connection.response :logger connection.adapter Faraday.default_adapter end conn. :Bearer, [:token] if [:token] response = conn.get do |req| req.url url req.params = build_query_params 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 contentcan be supplied in place of ID. -
url(OPTIONAL) - If specified, this URL is used to fetch data inplace 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, = nil) # If +id+ argument is a URL, set the url url = if id =~ /\A#{URI::regexp}\z/ id else path_prefix = url_path_prefix () URI.join(path_prefix, "#{id}/").to_s end return populate_models(fetch(url, )).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 tothe URLs in configuration. -
options- Options hash
44 45 46 |
# File 'lib/green-button-data/fetchable.rb', line 44 def first(url = nil, = nil) return all(url, ).first end |
#last(url = nil, options = nil) ⇒ Object
79 80 81 |
# File 'lib/green-button-data/fetchable.rb', line 79 def last(url = nil, = nil) return all(url, ).last end |
#options ⇒ Object
129 130 131 |
# File 'lib/green-button-data/fetchable.rb', line 129 def end |
#records ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/green-button-data/fetchable.rb', line 121 def records if ![:reload] && @records @records else @records = populate_models(feed) end end |
#url ⇒ Object
133 134 135 |
# File 'lib/green-button-data/fetchable.rb', line 133 def url @url end |