class Storenvy::Client

Attributes

access_token[RW]

Public Class Methods

new(url = 'https://api.storenvy.com', parallel_requests = 5) click to toggle source

Creates a new instance of Storenvy::Client

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

# File lib/storenvy/client.rb, line 31
def initialize(url = 'https://api.storenvy.com', parallel_requests = 5)
  @url = url
  @parallel_requests = parallel_requests
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 Storenvy API

# File lib/storenvy/client.rb, line 79
def delete(url, params)
  params = convert_hash_keys(params)
  params[:access_token] = @access_token if @access_token
  preform(url, :delete) do
    return connection.delete(url)
  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 Storenvy API @param params [Hash] the url params that should be passed in the request

# File lib/storenvy/client.rb, line 41
def get(url, params = {})
  params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
  params[:access_token] = @access_token if @access_token
  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

Storenvy.in_parallel do
  Storenvy.create_review(review_params)
  Storenvy.update_account(account_params)
end
# File lib/storenvy/client.rb, line 95
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 Storenvy API @param params [Hash] the body of the request

# File lib/storenvy/client.rb, line 54
def post(url, params)
  params = convert_hash_keys(params)
  params[:access_token] = @access_token if @access_token
  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 Storenvy API @param params [Hash] the body of the request

# File lib/storenvy/client.rb, line 67
def put(url, params)
  params = convert_hash_keys(params)
  params[:access_token] = @access_token if @access_token
  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/storenvy/client.rb, line 121
def connection
  @connection ||= Faraday.new(url: @url, parallel_manager: Typhoeus::Hydra.new(max_concurrency: @parallel_requests), headers: {:storenvy_api_connector => 'Ruby'+ Storenvy::VERSION}) do |conn|

    conn.use Storenvy::ResponseParser

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

    # Setting request and response to use JSON using Oj
    conn.response :oj

    # 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/storenvy/client.rb, line 139
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/storenvy/client.rb, line 109
def preform(url, type, params = {}, &block)
  ActiveSupport::Notifications.instrument 'Storenvy', request: type, url: url, params: params do
    if connection.in_parallel?
      block.call
    else
      block.call.body
    end
  end
end