Class: FlightStats::API

Inherits:
Object
  • Object
show all
Extended by:
Net::HTTPAdapter
Defined in:
lib/flightstats/api.rb,
lib/flightstats/api/errors.rb,
lib/flightstats/api/net_http_adapter.rb

Defined Under Namespace

Modules: Net Classes: BadRequest, ClientError, Forbidden, InternalServerError, MethodNotAllowed, NotFound, Redirection, ResponseError, ServerError

Constant Summary collapse

ERRORS =

Error mapping by status code.

Hash.new { |hash, code|
  unless hash.key? code
    case code
      when 400...500 then ClientError
      when 500...600 then ServerError
      else                ResponseError
    end
  end
}.update(
  400 => BadRequest,
  403 => Forbidden,
  404 => NotFound,
  405 => MethodNotAllowed,
  500 => InternalServerError
).freeze
@@base_uri =
"https://api.flightstats.com"

Instance Attribute Summary

Attributes included from Net::HTTPAdapter

#net_http

Class Method Summary collapse

Class Method Details

.accept_languageString?

Returns Accept-Language header value.

Returns:

  • (String, nil)

    Accept-Language header value



16
17
18
# File 'lib/flightstats/api.rb', line 16

def accept_language
  headers['Accept-Language']
end

.accept_language=(language) ⇒ Object

Parameters:

  • language (String)

    Accept-Language header value



21
22
23
# File 'lib/flightstats/api.rb', line 21

def accept_language=(language)
  headers['Accept-Language'] = language
end

.base_uriURI::Generic

Returns:

  • (URI::Generic)


56
57
58
# File 'lib/flightstats/api.rb', line 56

def base_uri
  URI.parse @@base_uri
end

.delete(uri, options = {}) ⇒ Net::HTTPNoContent, Net::HTTPResponse

Returns:

  • (Net::HTTPNoContent, Net::HTTPResponse)

Raises:



51
52
53
# File 'lib/flightstats/api.rb', line 51

def delete uri, options = {}
  request :delete, uri, options
end

.get(uri, params = {}, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



33
34
35
# File 'lib/flightstats/api.rb', line 33

def get uri, params = {}, options = {}
  request :get, uri, { :params => params }.merge(options)
end

.head(uri, params = {}, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



27
28
29
# File 'lib/flightstats/api.rb', line 27

def head uri, params = {}, options = {}
  request :head, uri, { :params => params }.merge(options)
end

.headersHash{String => String}

Additional HTTP headers sent with each API call

Returns:

  • (Hash{String => String})


11
12
13
# File 'lib/flightstats/api.rb', line 11

def headers
  @headers ||= { 'Accept' => accept, 'User-Agent' => user_agent }
end

.post(uri, body = nil, options = {}) ⇒ Net::HTTPCreated, Net::HTTPResponse

Returns:

  • (Net::HTTPCreated, Net::HTTPResponse)

Raises:



39
40
41
# File 'lib/flightstats/api.rb', line 39

def post uri, body = nil, options = {}
  request :post, uri, { :body => body.to_s }.merge(options)
end

.put(uri, body = nil, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



45
46
47
# File 'lib/flightstats/api.rb', line 45

def put uri, body = nil, options = {}
  request :put, uri, { :body => body.to_s }.merge(options)
end

.user_agentString

Returns:

  • (String)


61
62
63
# File 'lib/flightstats/api.rb', line 61

def user_agent
  "FlightStats/#{FlightStats::Version}; #{RUBY_DESCRIPTION}"
end