Method: Parse::Client#initialize
- Defined in:
- lib/parse/client.rb
#initialize(opts = {}) ⇒ Client
Create a new client connected to the Parse Server REST API endpoint.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/parse/client.rb', line 243 def initialize(opts = {}) @server_url = opts[:server_url] || ENV["PARSE_SERVER_URL"] || Parse::Protocol::SERVER_URL @application_id = opts[:application_id] || opts[:app_id] || ENV["PARSE_SERVER_APPLICATION_ID"] || ENV['PARSE_APP_ID'] @api_key = opts[:api_key] || opts[:rest_api_key] || ENV["PARSE_SERVER_REST_API_KEY"] || ENV["PARSE_API_KEY"] @master_key = opts[:master_key] || ENV['PARSE_SERVER_MASTER_KEY'] || ENV["PARSE_MASTER_KEY"] opts[:adapter] ||= Faraday.default_adapter opts[:expires] ||= 3 if @server_url.nil? || @application_id.nil? || ( @api_key.nil? && @master_key.nil? ) raise Parse::Error::ConnectionError, "Please call Parse.setup(server_url:, application_id:, api_key:) to setup a client" end @server_url += '/' unless @server_url.ends_with?('/') #Configure Faraday opts[:faraday] ||= {} opts[:faraday].merge!(:url => @server_url) @conn = Faraday.new(opts[:faraday]) do |conn| #conn.request :json conn.response :logger if opts[:logging] # This middleware handles sending the proper authentication headers to Parse # on each request. # this is the required authentication middleware. Should be the first thing # so that other middlewares have access to the env that is being set by # this middleware. First added is first to brocess. conn.use Parse::Middleware::Authentication, application_id: @application_id, master_key: @master_key, api_key: @api_key # This middleware turns the result from Parse into a Parse::Response object # and making sure request that are going out, follow the proper MIME format. # We place it after the Authentication middleware in case we need to use then # authentication information when building request and responses. conn.use Parse::Middleware::BodyBuilder if opts[:logging].present? && opts[:logging] == :debug Parse::Middleware::BodyBuilder.logging = true end if opts[:cache].present? && opts[:expires].to_i > 0 # advanced: provide a REDIS url, we'll configure a Moneta Redis store. if opts[:cache].is_a?(String) && opts[:cache].starts_with?("redis://") begin opts[:cache] = Moneta.new(:Redis, url: opts[:cache]) rescue LoadError puts "[Parse::Middleware::Caching] Did you forget to load the redis gem (Gemfile)?" raise end end unless [:key?, :[], :delete, :store].all? { |method| opts[:cache].respond_to?(method) } raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta store" end self.cache = opts[:cache] conn.use Parse::Middleware::Caching, self.cache, {expires: opts[:expires].to_i } end yield(conn) if block_given? conn.adapter opts[:adapter] end Parse::Client.clients[:default] ||= self end |