Class: OneBusAway::Client
- Inherits:
-
Object
- Object
- OneBusAway::Client
- Defined in:
- lib/one_bus_away/client.rb
Overview
Class for establishing a connection to one bus away
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_method ⇒ Object
Returns the value of attribute api_method.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#apply_local_api_key ⇒ Object
Applies ~/.one_bus_away to @api if the file exists otherwise, it fails.
-
#build_path ⇒ Object
Builds the path for utilization in #build_url.
-
#build_query ⇒ Object
Build query for utilization in #build_url.
-
#build_url ⇒ Object
Builds a valid url, then sets this string to @url.
-
#get ⇒ Object
Provided that @url is set, HTTP get @url.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#valid? ⇒ Boolean
Verifies that @api_method and @api_key are set.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 |
# File 'lib/one_bus_away/client.rb', line 10 def initialize( = {}) @api_method = [:api_method] @api_key = [:api_key] || apply_local_api_key @parameters = [:parameters] @base_url = 'api.pugetsound.onebusaway.org' end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/one_bus_away/client.rb', line 7 def api_key @api_key end |
#api_method ⇒ Object
Returns the value of attribute api_method.
7 8 9 |
# File 'lib/one_bus_away/client.rb', line 7 def api_method @api_method end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/one_bus_away/client.rb', line 8 def base_url @base_url end |
#http_response ⇒ Object (readonly)
Returns the value of attribute http_response.
8 9 10 |
# File 'lib/one_bus_away/client.rb', line 8 def http_response @http_response end |
#parameters ⇒ Object
Returns the value of attribute parameters.
7 8 9 |
# File 'lib/one_bus_away/client.rb', line 7 def parameters @parameters end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/one_bus_away/client.rb', line 8 def url @url end |
Instance Method Details
#apply_local_api_key ⇒ Object
Applies ~/.one_bus_away to @api if the file exists otherwise, it fails.
65 66 67 68 69 70 71 72 73 |
# File 'lib/one_bus_away/client.rb', line 65 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 |
#build_path ⇒ Object
Builds the path for utilization in #build_url
49 50 51 52 53 54 |
# File 'lib/one_bus_away/client.rb', line 49 def build_path path = %w(api where) path.concat api_method path = path.join('/') @path = "/#{path}.json" end |
#build_query ⇒ Object
Build query for utilization in #build_url
57 58 59 60 61 |
# File 'lib/one_bus_away/client.rb', line 57 def build_query query = { key: @api_key } query.merge! @parameters if @parameters query.map { |k, v| "#{k}=#{v}" }.join('&') end |
#build_url ⇒ Object
Builds a valid url, then sets this string to @url
40 41 42 43 44 45 46 |
# File 'lib/one_bus_away/client.rb', line 40 def build_url @url = URI::HTTP.build( host: @base_url, path: build_path, query: build_query ).to_s if valid? end |
#get ⇒ Object
Provided that @url is set, HTTP get @url
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/one_bus_away/client.rb', line 27 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 |
#valid? ⇒ Boolean
Verifies that @api_method and @api_key are set
18 19 20 21 22 23 24 |
# File 'lib/one_bus_away/client.rb', line 18 def valid? if api_method && api_key true else false end end |