class TheCity::API::Client

Wrapper for the TheCity REST API

@see api.onthecity.com/docs

Constants

ENDPOINT

Attributes

connection_options[W]
middleware[W]

Public Instance Methods

connection_options() click to toggle source
# File lib/the_city/api/client.rb, line 40
def connection_options
  {
    :builder => middleware,
    :headers => {
      :accept => "application/vnd.thecity.v#{version}+json",
      'X-THECITY-SUBDOMAIN' => subdomain,
      'X-THECITY-ACCESS-TOKEN' => access_token,
    },
    :request => {
      :open_timeout => 5,
      :timeout => 60,
    },
  }
end
delete(path, params={}) click to toggle source

Perform an HTTP DELETE request

# File lib/the_city/api/client.rb, line 77
def delete(path, params={})
  request(:delete, path, params)
end
get(path, params={}) click to toggle source

Perform an HTTP GET request

# File lib/the_city/api/client.rb, line 82
def get(path, params={})
  request(:get, path, params)
end
middleware() click to toggle source

@note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one. @see github.com/technoweenie/faraday#advanced-middleware-usage @see mislav.uniqpath.com/2011/07/faraday-advanced-http/ @return [Faraday::Builder]

# File lib/the_city/api/client.rb, line 59
def middleware
  @middleware ||= Faraday::Builder.new do |builder|
    # Convert file uploads to Faraday::UploadIO objects
    builder.use TheCity::API::Request::MultipartWithFile
    # Checks for files in the payload
    builder.use Faraday::Request::Multipart
    # Convert request params to "www-form-urlencoded"
    builder.use Faraday::Request::UrlEncoded
    # Handle error responses
    builder.use TheCity::API::Response::RaiseError
    # Parse JSON response bodies
    builder.use TheCity::API::Response::ParseJson
    # Set Faraday's HTTP adapter
    builder.adapter Faraday.default_adapter
  end
end
post(path, params={}) click to toggle source

Perform an HTTP POST request

# File lib/the_city/api/client.rb, line 87
def post(path, params={})
  signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
  request(:post, path, params, signature_params)
end
put(path, params={}) click to toggle source

Perform an HTTP PUT request

# File lib/the_city/api/client.rb, line 93
def put(path, params={})
  request(:put, path, params)
end

Private Instance Methods

bearer_auth_header() click to toggle source

Generates authentication header for api request

@return [String]

# File lib/the_city/api/client.rb, line 130
def bearer_auth_header
  "Bearer #{access_token}"
end
connection() click to toggle source

Returns a Faraday::Connection object

@return [Faraday::Connection]

# File lib/the_city/api/client.rb, line 123
def connection
  @connection ||= Faraday.new(ENDPOINT, connection_options)
end
request(method, path, params={}, signature_params=params) click to toggle source
# File lib/the_city/api/client.rb, line 112
def request(method, path, params={}, signature_params=params)
  validate_credentials!
  request_setup = request_setup(method, path, params, signature_params)
  connection.send(method.to_sym, path, params, &request_setup).env
rescue Faraday::Error::ClientError, JSON::ParserError
  raise TheCity::Error
end
request_setup(method, path, params, signature_params) click to toggle source

Returns a proc that can be used to setup the Faraday::Request headers

@param method [Symbol] @param path [String] @param params [Hash] @return [Proc]

# File lib/the_city/api/client.rb, line 106
def request_setup(method, path, params, signature_params)
  Proc.new do |request|
    request.headers[:authorization] = bearer_auth_header
  end
end