Class: Collmex::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/collmex/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



31
32
33
34
35
36
37
38
39
40
# File 'lib/collmex/request.rb', line 31

def initialize
  @commands     = []
  @raw_response = {}

  if Collmex.username.nil? || Collmex.password.nil? || Collmex.customer_id.nil?
    raise "No credentials for collmex given"
  else
    add_command Collmex::Api::Login.new({username: Collmex.username, password: Collmex.password})
  end
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



6
7
8
# File 'lib/collmex/request.rb', line 6

def commands
  @commands
end

#debugObject

Returns the value of attribute debug.



7
8
9
# File 'lib/collmex/request.rb', line 7

def debug
  @debug
end

#httpObject

Returns the value of attribute http.



6
7
8
# File 'lib/collmex/request.rb', line 6

def http
  @http
end

Class Method Details

.classify(term) ⇒ Object



17
18
19
# File 'lib/collmex/request.rb', line 17

def self.classify(term)
  term.to_s.split("_").collect(&:capitalize).join
end

.header_attributesObject



51
52
53
# File 'lib/collmex/request.rb', line 51

def self.header_attributes
  {"Content-Type" => "text/csv"}
end

.run(&block) ⇒ Object



9
10
11
12
13
14
# File 'lib/collmex/request.rb', line 9

def self.run(&block)
  Request.new.tap do |request|
    request.instance_eval &block if block_given?
    request.execute
  end
end

.uriObject



47
48
49
# File 'lib/collmex/request.rb', line 47

def self.uri
  URI.parse "https://www.collmex.de/cgi-bin/cgi.exe\?#{Collmex.customer_id},0,data_exchange"
end

Instance Method Details

#add_command(cmd) ⇒ Object



42
43
44
45
# File 'lib/collmex/request.rb', line 42

def add_command(cmd)
  @commands << cmd
  cmd
end

#enqueue(command, args = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/collmex/request.rb', line 21

def enqueue(command, args = {})
  if command.is_a? Symbol
    add_command Collmex::Api::const_get(self.class.classify(command)).new(args)
  elsif Collmex::Api.is_a_collmex_api_line_obj?(command)
    add_command command
  else
    return false
  end
end

#executeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/collmex/request.rb', line 72

def execute
  @http = Net::HTTP.new(Collmex::Request.uri.host, Collmex::Request.uri.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  # http://www.collmex.de/faq.html#zeichensatz_import
  encoded_body = payload.encode("ISO8859-1", undef: :replace) # Do not blow up on undefined characters in ISO8859-1
  response = @http.request_post(Collmex::Request.uri.request_uri, encoded_body, Collmex::Request.header_attributes)
  response.body.force_encoding("ISO8859-1") if response.body.encoding.to_s == "ASCII-8BIT"

  @raw_response[:string] = response.body.encode("UTF-8")

  begin
    @raw_response[:array]  = CSV.parse(@raw_response[:string], Collmex.csv_opts)
  rescue => e
    STDERR.puts "CSV.parse failed with string: #{@raw_response[:string]}" if self.debug
    raise e
  end

  parse_response
end

#parse_responseObject



59
60
61
62
# File 'lib/collmex/request.rb', line 59

def parse_response
  @response = @raw_response[:array].map { |l| Collmex::Api.parse_line(l) }
  @response
end

#payloadObject



55
56
57
# File 'lib/collmex/request.rb', line 55

def payload
  @commands.map { |c| c.to_csv }.join
end

#raw_responseObject



64
65
66
# File 'lib/collmex/request.rb', line 64

def raw_response
  @raw_response
end

#responseObject



68
69
70
# File 'lib/collmex/request.rb', line 68

def response
  @response
end