Class: TflApi::Client
- Inherits:
-
Object
- Object
- TflApi::Client
- 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
-
#app_id ⇒ Object
readonly
Client accessors.
-
#app_key ⇒ Object
readonly
Client accessors.
-
#host ⇒ Object
readonly
Client accessors.
-
#log_level ⇒ Object
readonly
Client accessors.
-
#log_location ⇒ Object
readonly
Client accessors.
-
#logger ⇒ Object
readonly
Client accessors.
Instance Method Summary collapse
-
#accident_stats ⇒ TflApi::Client::AccidentStats
Creates an instance to the AccidentStats class by passing a reference to self.
-
#air_quality ⇒ TflApi::Client::AirQuality
Creates an instance to the AirQuality class by passing a reference to self.
-
#bike_point ⇒ TflApi::Client::BikePoint
Creates an instance to the BikePoint class by passing a reference to self.
-
#cabwise ⇒ TflApi::Client::Cabwise
Creates an instance to the Cabwise class by passing a reference to self.
-
#cycle ⇒ TflApi::Client::Cycle
Creates an instance to the Cycle class by passing a reference to self.
-
#get(path, query = {}) ⇒ hash
Performs a HTTP GET request to the api, based upon the given URI resource and any additional HTTP query parameters.
-
#initialize(args) ⇒ TflApi::Client
constructor
Initialize a Client object with TFL API credentials.
-
#inspect ⇒ String
Overrides the inspect method to prevent the TFL Application ID and Key credentials being shown when the ‘inspect` method is called.
-
#journey ⇒ TflApi::Client::Journey
Creates an instance to the Journey class by passing a reference to self.
-
#mode ⇒ TflApi::Client::Mode
Creates an instance to the Mode class by passing a reference to self.
Constructor Details
#initialize(args) ⇒ TflApi::Client
Initialize a Client object with TFL API credentials
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_id ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def app_id @app_id end |
#app_key ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def app_key @app_key end |
#host ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def host @host end |
#log_level ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def log_level @log_level end |
#log_location ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def log_location @log_location end |
#logger ⇒ Object (readonly)
Client accessors
48 49 50 |
# File 'lib/tfl_api_client/client.rb', line 48 def logger @logger end |
Instance Method Details
#accident_stats ⇒ TflApi::Client::AccidentStats
Creates an instance to the AccidentStats class by passing a reference to self
103 104 105 |
# File 'lib/tfl_api_client/client.rb', line 103 def accident_stats TflApi::Client::AccidentStats.new(self) end |
#air_quality ⇒ TflApi::Client::AirQuality
Creates an instance to the AirQuality class by passing a reference to self
111 112 113 |
# File 'lib/tfl_api_client/client.rb', line 111 def air_quality TflApi::Client::AirQuality.new(self) end |
#bike_point ⇒ TflApi::Client::BikePoint
Creates an instance to the BikePoint class by passing a reference to self
119 120 121 |
# File 'lib/tfl_api_client/client.rb', line 119 def bike_point TflApi::Client::BikePoint.new(self) end |
#cabwise ⇒ TflApi::Client::Cabwise
Creates an instance to the Cabwise class by passing a reference to self
135 136 137 |
# File 'lib/tfl_api_client/client.rb', line 135 def cabwise TflApi::Client::Cabwise.new(self) end |
#cycle ⇒ TflApi::Client::Cycle
Creates an instance to the Cycle class by passing a reference to self
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.
162 163 164 |
# File 'lib/tfl_api_client/client.rb', line 162 def get(path, query={}) request_json :get, path, query end |
#inspect ⇒ String
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.
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 |
#journey ⇒ TflApi::Client::Journey
Creates an instance to the Journey class by passing a reference to self
143 144 145 |
# File 'lib/tfl_api_client/client.rb', line 143 def journey TflApi::Client::Journey.new(self) end |
#mode ⇒ TflApi::Client::Mode
Creates an instance to the Mode class by passing a reference to self
151 152 153 |
# File 'lib/tfl_api_client/client.rb', line 151 def mode TflApi::Client::Mode.new(self) end |