class DataForSEO::Client

Attributes

api_version[RW]
auth[RW]
base_uri[RW]

Public Class Methods

new(username, password, sandbox=false, api_version='v3') click to toggle source
# File lib/data_for_seo/client.rb, line 10
def initialize(username, password, sandbox=false, api_version='v3')
    if sandbox
        @base_uri = 'https://sandbox.dataforseo.com'
    else
        @base_uri = 'https://api.dataforseo.com'
    end

    @auth = {username: username, password: password}
    @api_version = api_version 
end

Public Instance Methods

get(path, options={}) click to toggle source
# File lib/data_for_seo/client.rb, line 37
def get(path, options={})
    target_url = [@base_uri, @api_version, path].join('/')
    options = merge_request_options(options)
    request = self.class.get(target_url, options)
    json = parse_response(request)
    return json, request
end
merge_request_options(options) click to toggle source
# File lib/data_for_seo/client.rb, line 21
def merge_request_options(options)
    options.merge!({ basic_auth: @auth })
    options.merge!({headers: { 'Content-Type' => 'application/json' }})
    options 
end
parse_response(request) click to toggle source
# File lib/data_for_seo/client.rb, line 27
def parse_response(request)
    begin 
        json = JSON.parse(request.response.body)
    rescue => e 
        warn ['Parse response JSON error', e.class, e.message].join(' ')
        json = {}
    end
    json
end
post(path, body={}, options = {}) click to toggle source
# File lib/data_for_seo/client.rb, line 45
def post(path, body={}, options = {})
    target_url = [@base_uri, @api_version, path].join('/')
    options[:body] = body.to_json
    options = merge_request_options(options)
    request = self.class.post(target_url, options)
    json = parse_response(request)
    return json, request
end