class Spinacz::Client

Constants

TEST_URL
URL

Attributes

email[RW]
password[RW]
test[RW]
token[RW]

Public Class Methods

new(credentials = {}) click to toggle source
# File lib/spinacz/client.rb, line 8
def initialize(credentials = {})
  @email = credentials[:email] || ENV['SPINACZ_EMAIL']
  @email = @email.strip if @email
  @password = credentials[:password] || ENV['SPINACZ_PASSWORD']
  @password = @password.strip if @password
  @token = credentials[:token] || ENV['SPINACZ_TOKEN']
  @token = @token.strip if @token
  @test = credentials[:test]
end

Public Instance Methods

add_package_DHL(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 22
def add_package_DHL(package = {})
  check_credentials
  call_api(:post, 'addPackageDHL', package)
end
add_package_DPD(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 27
def add_package_DPD(package = {})
  check_credentials
  call_api(:post, 'addPackageDPD', package)
end
add_package_FEDEX(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 32
def add_package_FEDEX(package = {})
  check_credentials
  call_api(:post, 'addPackageFEDEX', package)
end
add_package_GLS(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 37
def add_package_GLS(package = {})
  check_credentials
  call_api(:post, 'addPackageGLS', package)
end
add_package_INPOST(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 42
def add_package_INPOST(package = {})
  check_credentials
  call_api(:post, 'addPackageINPOST', package)
end
add_package_KEX(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 47
def add_package_KEX(package = {})
  check_credentials
  call_api(:post, 'addPackageKEX', package)
end
add_package_POCZTA(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 52
def add_package_POCZTA(package = {})
  check_credentials
  call_api(:post, 'addPackagePOCZTA', package)
end
add_package_UPS(package = {}) click to toggle source
# File lib/spinacz/client.rb, line 57
def add_package_UPS(package = {})
  check_credentials
  call_api(:post, 'addPackageUPS', package)
end
cancel(query = {}) click to toggle source
# File lib/spinacz/client.rb, line 62
def cancel(query = {})
  check_credentials
  call_api(:get, 'cancel', query)
end
get_packages_templates() click to toggle source
# File lib/spinacz/client.rb, line 67
def get_packages_templates
  check_credentials
  call_api(:get, 'getPackagesTemplates')
end
get_paczkomaty(query = {}) click to toggle source
# File lib/spinacz/client.rb, line 72
def get_paczkomaty(query = {})
  call_api(:get, 'getPaczkomaty', query)
end
get_placowki(query = {}) click to toggle source
# File lib/spinacz/client.rb, line 76
def get_placowki(query = {})
  call_api(:get, 'getPlacowki', query)
end
get_send_points() click to toggle source
# File lib/spinacz/client.rb, line 80
def get_send_points
  check_credentials
  call_api(:get, 'getSendPoints')
end
login() click to toggle source
# File lib/spinacz/client.rb, line 18
def login
  check_credentials
end
pickup(query = {}) click to toggle source
# File lib/spinacz/client.rb, line 85
def pickup(query = {})
  check_credentials
  call_api(:get, 'pickup', query)
end

Protected Instance Methods

call_api(call_type, get_data, json_data = nil) click to toggle source
# File lib/spinacz/client.rb, line 91
def call_api(call_type, get_data, json_data = nil)
  json_data_converted = json_data ? json_data.to_json : ''
  endpoint = "/api/v1/#{get_data}"
  url = @test ? TEST_URL : URL

  conn = Faraday.new(url: url) do |faraday|
    faraday.request  :url_encoded
    # faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end

  response = conn.send(call_type) do |req|
    if call_type == :get 
      req.url endpoint, json_data
    else
      req.url endpoint
      req.body = json_data_converted
    end

    # req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = @token if @token.present?
  end

  body = JSON.parse(response.body)

  if body['error'].nil?
    body['success']
  else
    raise Spinacz::ParametersInvalidError, "Wrong parameters provided. Full error response: #{body['error']}"
  end
end
check_credentials() click to toggle source
# File lib/spinacz/client.rb, line 123
def check_credentials
  unless (@password.present? and @email.present?) or @token.present?
    raise Spinacz::CredentialsMissingError, 'Email with password or token are missing.'
  end

  unless @token.present?
    authorization = call_api(:get, 'login', {email: @email, password: Digest::SHA256.hexdigest(@password)})

    if authorization['hash'].present?
      @token = authorization['hash'] 
    else
      raise Spinacz::CredentialsInvalidError, 'email or password is invalid'
    end
  end
end