Class: OneBusAway::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/one_bus_away/client.rb

Overview

Class for establishing a connection to one bus away

Instance Attribute Summary collapse

Instance Method Summary collapse

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(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

Instance Attribute Details

#api_keyObject

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_methodObject

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_urlObject (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_responseObject (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

#parametersObject

Returns the value of attribute parameters.



7
8
9
# File 'lib/one_bus_away/client.rb', line 7

def parameters
  @parameters
end

#urlObject (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_keyObject

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_pathObject

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_queryObject

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_urlObject

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

#getObject

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

Returns:

  • (Boolean)


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