class Naver::Connection

Public Class Methods

new() click to toggle source

Create a Connection object.

# File lib/naver/connection.rb, line 4
def initialize
  @client_id     = Naver.client_id
  @client_secret = Naver.client_secret
  @api_base_uri  = Configuration::DEFAULT_API_BASE_URI
  @headers = { user_agent: Configuration::DEFAULT_USER_AGENT }
  @connection = Faraday.new(url: @api_base_uri, headers: @headers) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :logger if Naver.debug
    faraday.adapter :net_http
  end
end

Public Instance Methods

delete(path, params = {}) click to toggle source

Perform a DELETE request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.

# File lib/naver/connection.rb, line 41
def delete(path, params = {})
  request(:delete, path, params)
end
get(path, params = {}) click to toggle source

Perform a GET request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.

# File lib/naver/connection.rb, line 20
def get(path, params = {})
  request(:get, path, params)
end
post(path, params = {}) click to toggle source

Perform a POST request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.

# File lib/naver/connection.rb, line 34
def post(path, params = {})
  request(:post, path, params)
end
put(path, params = {}) click to toggle source

Perform a PUT request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.

# File lib/naver/connection.rb, line 27
def put(path, params = {})
  request(:put, path, params)
end

Private Instance Methods

public_auth_header() click to toggle source
# File lib/naver/connection.rb, line 63
def public_auth_header
  {
    "X-Naver-Client-Id" => @client_id,
    "X-Naver-Client-Secret" => @client_secret
  }
end
request(verb, path, params = {}) { |request| ... } click to toggle source
# File lib/naver/connection.rb, line 47
def request(verb, path, params = {})
  raise ArgumentError.new "Invalid http verb #{verb}" unless [:get, :post, :put, :delete].include?(verb)

  response = @connection.run_request(verb, path, params, public_auth_header) do |request|
    request.params.update(params) if verb == :get && params
    yield(request) if block_given?
  end

  unless (200..299).include?(response.status)
    body = JSON.parse(response.body)
    raise Naver::Error.new(body)
  end

  response
end