Class: TflApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tfl_api_client/client.rb,
lib/tfl_api_client/mode.rb,
lib/tfl_api_client/cycle.rb,
lib/tfl_api_client/cabwise.rb,
lib/tfl_api_client/journey.rb,
lib/tfl_api_client/bike_point.rb,
lib/tfl_api_client/air_quality.rb,
lib/tfl_api_client/accident_stats.rb

Overview

This is the client class that allows direct access to the subclasses and to the TFL API. The class contains methods that perform GET and POST requests to the API.

Defined Under Namespace

Classes: AccidentStats, AirQuality, BikePoint, Cabwise, Cycle, Journey, Mode

Constant Summary collapse

VALID_PARAMS =

Parameters that are permitted as options while initializing the client

%w( app_id app_key host logger log_level log_location ).freeze
VERB_MAP =

HTTP verbs supported by the Client

{
  get: Net::HTTP::Get
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TflApi::Client

Initialize a Client object with TFL API credentials

Parameters:

  • args (Hash)

    Arguments to connect to TFL API

Options Hash (args):

  • :app_id (String)

    the application id generated by registering an app with TFL

  • :app_key (String)

    the application key generated by registering an app with TFL

  • :host (String)

    the API’s host url - defaults to api.tfl.gov.uk

Raises:

  • (ArgumentError)

    when required options are not provided.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tfl_api_client/client.rb', line 62

def initialize(args)
  args.each do |key, value|
    if value && VALID_PARAMS.include?(key.to_s)
      instance_variable_set("@#{key.to_sym}", value)
    end
  end if args.is_a? Hash

  # Ensure the Application ID and Key is given
  raise ArgumentError, "Application ID (app_id) is required to interact with TFL's APIs" unless app_id
  raise ArgumentError, "Application Key (app_key) is required to interact with TFL's APIs" unless app_key

  # Set client defaults
  @host ||= 'https://api.tfl.gov.uk'
  @host = URI.parse(@host)

  # Create a global Net:HTTP instance
  @http = Net::HTTP.new(@host.host, @host.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  # Logging
  if @logger
    raise ArgumentError, 'logger parameter must be a Logger object' unless @logger.is_a?(Logger)
    raise ArgumentError, 'log_level cannot be set if using custom logger' if @log_level
    raise ArgumentError, 'log_location cannot be set if using custom logger' if @log_location
  else
    @log_level = Logger::INFO unless @log_level
    @log_location = STDOUT unless @log_location
    @logger = Logger.new(@log_location)
    @logger.level = @log_level
    @logger.datetime_format = '%F T%T%z'
    @logger.formatter = proc do |severity, datetime, _progname, msg|
      "[%s] %-6s %s \r\n" %  [datetime, severity, msg]
    end
  end
end

Instance Attribute Details

#app_idObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def app_id
  @app_id
end

#app_keyObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def app_key
  @app_key
end

#hostObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def host
  @host
end

#log_levelObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def log_level
  @log_level
end

#log_locationObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def log_location
  @log_location
end

#loggerObject (readonly)

Client accessors



48
49
50
# File 'lib/tfl_api_client/client.rb', line 48

def logger
  @logger
end

Instance Method Details

#accident_statsTflApi::Client::AccidentStats

Creates an instance to the AccidentStats class by passing a reference to self

Returns:



103
104
105
# File 'lib/tfl_api_client/client.rb', line 103

def accident_stats
  TflApi::Client::AccidentStats.new(self)
end

#air_qualityTflApi::Client::AirQuality

Creates an instance to the AirQuality class by passing a reference to self

Returns:



111
112
113
# File 'lib/tfl_api_client/client.rb', line 111

def air_quality
  TflApi::Client::AirQuality.new(self)
end

#bike_pointTflApi::Client::BikePoint

Creates an instance to the BikePoint class by passing a reference to self

Returns:



119
120
121
# File 'lib/tfl_api_client/client.rb', line 119

def bike_point
  TflApi::Client::BikePoint.new(self)
end

#cabwiseTflApi::Client::Cabwise

Creates an instance to the Cabwise class by passing a reference to self

Returns:



135
136
137
# File 'lib/tfl_api_client/client.rb', line 135

def cabwise
  TflApi::Client::Cabwise.new(self)
end

#cycleTflApi::Client::Cycle

Creates an instance to the Cycle class by passing a reference to self

Returns:



127
128
129
# File 'lib/tfl_api_client/client.rb', line 127

def cycle
  TflApi::Client::Cycle.new(self)
end

#get(path, query = {}) ⇒ hash

Performs a HTTP GET request to the api, based upon the given URI resource and any additional HTTP query parameters. This method will automatically inject the mandatory application id and application key HTTP query parameters.

Returns:

  • (hash)

    HTTP response as a hash



162
163
164
# File 'lib/tfl_api_client/client.rb', line 162

def get(path, query={})
  request_json :get, path, query
end

#inspectString

Overrides the inspect method to prevent the TFL Application ID and Key credentials being shown when the ‘inspect` method is called. The method will only print the important variables.

Returns:

  • (String)

    String representation of the current object



172
173
174
175
176
177
# File 'lib/tfl_api_client/client.rb', line 172

def inspect
  "#<#{self.class.name}:0x#{(self.__id__ * 2).to_s(16)} " +
      "@host=#{host.to_s}, " +
      "@log_level=#{log_level}, " +
      "@log_location=#{log_location.inspect}>"
end

#journeyTflApi::Client::Journey

Creates an instance to the Journey class by passing a reference to self

Returns:



143
144
145
# File 'lib/tfl_api_client/client.rb', line 143

def journey
  TflApi::Client::Journey.new(self)
end

#modeTflApi::Client::Mode

Creates an instance to the Mode class by passing a reference to self

Returns:



151
152
153
# File 'lib/tfl_api_client/client.rb', line 151

def mode
  TflApi::Client::Mode.new(self)
end