class TheCity::API::Client
Constants
- ENDPOINT
Attributes
Public Instance Methods
# 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
Perform an HTTP DELETE request
# File lib/the_city/api/client.rb, line 77 def delete(path, params={}) request(:delete, path, params) end
Perform an HTTP GET request
# File lib/the_city/api/client.rb, line 82 def get(path, params={}) request(:get, path, params) end
@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
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
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
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
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
# 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
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