class Yotpo::Client

Public Class Methods

new(url = 'https://api.yotpo.com', parallel_requests, timeout, user_agent) click to toggle source

Creates a new instance of Yotpo::Client

@param url [String] The url to yotpo api (api.yotpo.com) @param parallel_requests [Integer String] The maximum parallel request to do (5)

# File lib/yotpo/client.rb, line 50
def initialize(url = 'https://api.yotpo.com', parallel_requests, timeout, user_agent)
  @url = url
  @parallel_requests = parallel_requests
  @timeout = timeout
  @headers = { yotpo_api_connector: 'Ruby'+Yotpo::VERSION }
  @headers.merge!(user_agent: user_agent) if user_agent
end

Public Instance Methods

delete(url, params) click to toggle source

Does a DELETE request to the url with the params

@param url [String] the relative path in the Yotpo API

# File lib/yotpo/client.rb, line 98
def delete(url, params)
  params = convert_hash_keys(params)
  preform(url, :delete, params: params) do
    return connection.delete(url, params)
  end
end
get(url, params = {}) click to toggle source

Does a GET request to the url with the params

@param url [String] the relative path in the Yotpo API @param params [Hash] the url params that should be passed in the request

# File lib/yotpo/client.rb, line 63
def get(url, params = {})
  params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
  preform(url, :get, params: params) do
    return connection.get(url, params)
  end
end
in_parallel() { || ... } click to toggle source

Does a parallel request to the api for all of the requests in the block

@example block

Yotpo.in_parallel do
  Yotpo.create_review(review_params)
  Yotpo.update_account(account_params)
end
# File lib/yotpo/client.rb, line 113
def in_parallel
  connection.in_parallel do
    yield
  end
end
post(url, params) click to toggle source

Does a POST request to the url with the params

@param url [String] the relative path in the Yotpo API @param params [Hash] the body of the request

# File lib/yotpo/client.rb, line 75
def post(url, params)
  params = convert_hash_keys(params)
  preform(url, :post, params: params) do
    return connection.post(url, params)
  end
end
put(url, params) click to toggle source

Does a PUT request to the url with the params

@param url [String] the relative path in the Yotpo API @param params [Hash] the body of the request

# File lib/yotpo/client.rb, line 87
def put(url, params)
  params = convert_hash_keys(params)
  preform(url, :put, params: params) do
    return connection.put(url, params)
  end
end

Private Instance Methods

connection() click to toggle source

@return an instance of Faraday initialized with all that this gem needs

# File lib/yotpo/client.rb, line 139
def connection
  @connection ||= Faraday.new(url: @url, parallel_manager: Typhoeus::Hydra.new(max_concurrency: @parallel_requests), headers: @headers) do |conn|

    conn.options.timeout = @timeout

    conn.use Yotpo::ResponseParser

    # Set the response to be mashified
    conn.response :mashify

    # Setting request and response to use JSON/XML
    conn.request :oj
    conn.response :oj, :content_type => /\bjson$/

    # Set to use instrumentals to get time logs
    conn.use :instrumentation

    conn.adapter :typhoeus
  end
end
convert_hash_keys(value) click to toggle source
# File lib/yotpo/client.rb, line 160
def convert_hash_keys(value)
  case value
    when Array
      value.map { |v| convert_hash_keys(v) }
    when Hash
      Hash[value.map { |k, v| [k.to_s, convert_hash_keys(v)] }]
    else
      value
  end
end
preform(url, type, params = {}, &block) click to toggle source

Preforms an HTTP request and notifies the ActiveSupport::Notifications

@private @param url [String] the url to which preform the request @param type [String]

# File lib/yotpo/client.rb, line 127
def preform(url, type, params = {}, &block)
  ActiveSupport::Notifications.instrument 'Yotpo', request: type, url: url, params: params do
    if connection.in_parallel?
      block.call
    else
      block.call.body
    end
  end
end