Sha256: 9784cb16c0a8df7b6dd29452443fbdc079f298c808eb844f61a389cbf1755f82
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
require 'rest-client' require 'recursive-open-struct' class OneBusAway # Class for establishing a connection to one bus away class Client attr_accessor :api_method, :api_key, :parameters attr_reader :base_url, :url, :http_response def initialize(options = {}) @api_method = options[:api_method] @api_key = options[:api_key] || apply_local_api_key @parameters = options[:parameters] @base_url = 'api.pugetsound.onebusaway.org' end # Verifies that @api_method and @api_key are set def valid? if api_method && api_key true else false end end # Provided that @url is set, HTTP get @url def get if @url response = RestClient.get(@url) json = JSON.parse(response) @http_response = RecursiveOpenStruct.new( json, recurse_over_arrays: true ) else fail 'url is not properly built' end end # Builds a valid url, then sets this string to @url def build_url @url = URI::HTTP.build( host: @base_url, path: build_path, query: build_query ).to_s if valid? end # Builds the path for utilization in #build_url def build_path path = %w(api where) path.concat api_method path = path.join('/') @path = "/#{path}.json" end # Build query for utilization in #build_url def build_query query = { key: @api_key } query.merge! @parameters if @parameters query.map { |k, v| "#{k}=#{v}" }.join('&') end # Applies ~/.one_bus_away to @api if the file exists # otherwise, it fails. def apply_local_api_key if File.exist? ENV['HOME'] + '/.one_bus_away' file = File.read(ENV['HOME'] + '/.one_bus_away') @api_key = file.chomp else fail 'no API key provided. Please ensure you have your api key'\ 'installed in here: ~/.one_bus_away' end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
one_bus_away-0.0.4 | lib/one_bus_away/client.rb |