Class: Cheddargetter::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cheddargetter/client.rb

Constant Summary collapse

OptionModes =
[:customer, :data, :customer_and_data, :no_options]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/cheddargetter/client.rb', line 10

def initialize(options = {})
  self.username      = options[:username] || options["username"]
  self.password      = options[:password] || options["password"]
  self.product_code  = options[:product_code] || options["product_code"]

  raise(ArgumentError, "No username specified")     unless username
  raise(ArgumentError, "No password specified")     unless password
  raise(ArgumentError, "No product_code specified") unless product_code
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/cheddargetter/client.rb', line 20

def method_missing(method, *args, &block)
  parts = method.to_s.split('_')
  response = post('/' + [parts.slice!(0, 2).join('/'), parts].flatten.join('-'), args)

  if response.parsed_response["error"] && response.parsed_response["error"]["__content__"] && response.parsed_response["error"]["__content__"] == 'Resource not found'
    raise NoMethodError.new(method.to_s)
  else
    ::Cheddargetter::Response.new(response)
  end
end

Instance Attribute Details

#options_modeObject

Returns the value of attribute options_mode.



6
7
8
# File 'lib/cheddargetter/client.rb', line 6

def options_mode
  @options_mode
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/cheddargetter/client.rb', line 6

def password
  @password
end

#product_codeObject

Returns the value of attribute product_code.



6
7
8
# File 'lib/cheddargetter/client.rb', line 6

def product_code
  @product_code
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/cheddargetter/client.rb', line 6

def username
  @username
end

Instance Method Details

#customers_delete_allObject



41
42
43
44
45
46
47
48
# File 'lib/cheddargetter/client.rb', line 41

def customers_delete_all
  #whacky url
  response = self.class.post(
    "https://cheddargetter.com/xml/customers/delete-all/confirm/#{Time.now.to_i}/productCode/#{product_code}", 
    :basic_auth => {:username => username, :password => password}
  )
  ::Cheddargetter::Response.new(response)
end

#post(url, options) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/cheddargetter/client.rb', line 31

def post(url, options)
  set_options_mode(options)

  response = self.class.post(
    full_url(url, options), 
    :body => filter_customer_code_key(options), 
    :basic_auth => {:username => username, :password => password}
  )
end