class Seoshop::Client
Constants
- SERVER_EU1_LIVE
- SERVER_US1_LIVE
Attributes
Public Class Methods
Creates a new instance of Seoshop::Client
@param api_key
[String] The App api key @param api_secret
[String] The App secret @param access_token
[String] The shop access token @param shop_language
[String] The shop language @param cluster_id [String] The shop cluster id @param parallel_requests [Integer String] The maximum parallel request to do (5)
# File lib/seoshop-api/client.rb, line 48 def initialize(api_key, api_secret, access_token, shop_language, cluster_id = 'eu1', parallel_requests = 5) @api_key = api_key @api_secret = api_secret @access_token = access_token @shop_language = shop_language @url = get_url(cluster_id) @parallel_requests = parallel_requests end
Public Instance Methods
Does a DELETE request to the url with the params
@param url [String] the relative path in the Seoshop
API
# File lib/seoshop-api/client.rb, line 108 def delete(url) preform(url, :delete) do return connection.delete(url) end end
Does a GET request to the url with the params
@param url [String] the relative path in the Seoshop
API @param params [Hash] the url params that should be passed in the request
# File lib/seoshop-api/client.rb, line 73 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
# File lib/seoshop-api/client.rb, line 57 def get_url(cluster_id) case cluster_id when 'eu1' SERVER_EU1_LIVE when 'us1' SERVER_US1_LIVE else SERVER_EU1_LIVE end end
Does a parallel request to the api for all of the requests in the block
@example block
Seoshop.in_parallel do Seoshop.create_review(review_params) Seoshop.update_account(account_params) end
# File lib/seoshop-api/client.rb, line 122 def in_parallel connection.in_parallel do yield end end
Does a POST request to the url with the params
@param url [String] the relative path in the Seoshop
API @param params [Hash] the body of the request
# File lib/seoshop-api/client.rb, line 85 def post(url, params) params = convert_hash_keys(params) preform(url, :post, params: params) do return connection.post(url, params) end end
Does a PUT request to the url with the params
@param url [String] the relative path in the Seoshop
API @param params [Hash] the body of the request
# File lib/seoshop-api/client.rb, line 97 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
@return an instance of Faraday initialized with all that this gem needs
# File lib/seoshop-api/client.rb, line 148 def connection @connection ||= Faraday.new(url: @url, parallel_manager: Typhoeus::Hydra.new(max_concurrency: @parallel_requests), headers: {:seoshop_api_connector => 'Ruby'+ Seoshop::VERSION}) do |conn| conn.basic_auth(@api_key, Digest::MD5.hexdigest(@access_token + @api_secret)) conn.use Seoshop::ResponseParser # Set the response to be rashified conn.response :mashify # Setting request and response to use JSON/XML conn.response :oj # Set to use instrumentals to get time logs conn.use :instrumentation conn.adapter :typhoeus end end
# File lib/seoshop-api/client.rb, line 168 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
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/seoshop-api/client.rb, line 136 def preform(url, type, params = {}, &block) ActiveSupport::Notifications.instrument 'Seoshop', request: type, url: url, params: params do if connection.in_parallel? block.call else block.call.body end end end