Class: Tradenity::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tradenity/http/client.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



13
14
15
16
# File 'lib/tradenity/http/client.rb', line 13

def initialize()
  @key = Tradenity.api_key
  @current_session = nil
end

Class Method Details

.get_instanceObject



19
20
21
22
23
24
# File 'lib/tradenity/http/client.rb', line 19

def self.get_instance
  unless @@instance
    @@instance = HttpClient.new
  end
  @@instance
end

Instance Method Details

#auth_tokenObject



30
31
32
33
34
35
36
# File 'lib/tradenity/http/client.rb', line 30

def auth_token
  if @current_session.has_key? :auth_token
    @current_session[:auth_token]
  else
    nil
  end
end

#current_session(session) ⇒ Object



26
27
28
# File 'lib/tradenity/http/client.rb', line 26

def current_session(session)
  @current_session = session
end

#delete(url) ⇒ Object



88
89
90
# File 'lib/tradenity/http/client.rb', line 88

def delete( url)
  http_op('DELETE', url, {})
end

#get(url, data = nil) ⇒ Object



76
77
78
# File 'lib/tradenity/http/client.rb', line 76

def get(url, data=nil)
  http_op('GET', url, data)
end

#http_op(op, url, data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tradenity/http/client.rb', line 38

def http_op(op, url, data)
  headers = {}
  auth = nil
  if auth_token == nil
    auth =  {:user=> @key, :password=> ''}
  else
    headers[AUTH_TOKEN_HEADER_NAME] = auth_token
  end
  begin
    case op
      when 'GET'
        response = Unirest.get(url, headers: headers, auth: auth, parameters: data)
      when 'POST'
        response = Unirest.post(url, headers: headers, auth: auth, parameters: data)
      when 'PUT'
        response = Unirest.put(url, headers: headers, auth: auth, parameters: data)
      when 'DELETE'
        response = Unirest.delete(url, headers: headers, auth: auth, parameters: data)
      else
        raise 'You must provide a valid Http method.'
    end
  rescue
    raise ClientErrorException.new
  end

  if response.headers.has_key? AUTH_TOKEN_HEADER_SYMBOL

    @current_session[:auth_token] = response.headers[AUTH_TOKEN_HEADER_SYMBOL]
  end

  if response.code >= 400
    translate_exception(response)
  end

  response.body
end

#post(url, data = nil) ⇒ Object



80
81
82
# File 'lib/tradenity/http/client.rb', line 80

def post(url, data=nil)
  http_op('POST', url, data)
end

#put(url, data = nil) ⇒ Object



84
85
86
# File 'lib/tradenity/http/client.rb', line 84

def put(url, data=nil)
  http_op('PUT', url, data)
end

#translate_exception(response) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tradenity/http/client.rb', line 92

def translate_exception(response)
  error = ErrorMessage.new(response.body)
  case response.code
    when 500
      raise ServerErrorException.new('API server error.')
    when 401
      raise AuthenticationException.new(error)
    when 403
      raise AuthorizationException.new(error)
    when 404
      raise EntityNotFoundException.new(error)
    when 400
      case error.errorCode
        when DATA_VALIDATION_ERROR_CODE
          raise DataValidationException.new(error)
        when INVALID_PAYMENT_ERROR_CODE, GATEWAY_ERROR_ERROR_CODE, REFUND_ERROR_ERROR_CODE
          raise PaymentErrorException.new(error)
        when EXISTING_USERNAME_ERROR_CODE, EXISTING_EMAIL_ERROR_CODE
          raise CustomerCreationException.new(error)
        when CART_INVALID_ITEM_ERROR_CODE
          raise ShoppingCartException.new(error)
        when INVENTORY_INVALID_PRODUCT_ERROR_CODE, INVENTORY_NOT_AVAILABLE_PRODUCT_ERROR_CODE
          raise InventoryErrorException.new(error)
        else
          raise RequestErrorException.new(error)
      end
  end
end