Class: DopisOnlineClient::Request

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/dopis_online_client/request.rb

Constant Summary collapse

DEFAULTS =

common

{:postage_type => 195, # common
            :coupon_type => 0, # do not print
            :print_type => 0, # one-sided print
            :sender_type => 2, # from 1st page of the document
            :recipient_type => 2,
            :format => :xml
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf_file_path, params = {}) ⇒ Request

Public: Initializes library

pdf_file_path - string with the path to the pdf file, that will be send params - hash with the additional parameters



37
38
39
40
41
42
# File 'lib/dopis_online_client/request.rb', line 37

def initialize(pdf_file_path, params = {})
  params.merge!(DEFAULTS)
  @options = OpenStruct.new(params)
  @pdf_file_path = pdf_file_path
  self.class.base_uri DopisOnlineClient.base_uri
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/dopis_online_client/request.rb', line 19

def options
  @options
end

#pdf_file_pathObject (readonly)

Returns the value of attribute pdf_file_path.



19
20
21
# File 'lib/dopis_online_client/request.rb', line 19

def pdf_file_path
  @pdf_file_path
end

Class Method Details

.send(pdf_file_path, params = {}) ⇒ Object

Public: Sends the request to the service

pdf_file_path - string with the path to the pdf file, that will be send params - hash with the additional parameters

Returns new DopisOnlineClient::Response



28
29
30
# File 'lib/dopis_online_client/request.rb', line 28

def self.send(pdf_file_path, params = {})
  @request = new(pdf_file_path, params).deliver
end

Instance Method Details

#bodyObject

Internal: returns hash with request body

Returns Hash with request body



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
# File 'lib/dopis_online_client/request.rb', line 74

def body
  xml = Builder::XmlMarkup.new( :indent => 2 )
  xml.instruct! :xml, :encoding => "UTF-8"
  xml.dataroot do |dataroot|
    dataroot.typvyplatneho options.postage_type
    dataroot.typtisku options.print_type
    dataroot.tiskpoukazky options.coupon_type
    dataroot.typods options.sender_type
    dataroot.typadr options.recipient_type
    dataroot.odsid nil
    dataroot.odsobrazek nil
    dataroot.odsfirma nil
    dataroot.odsosoba nil
    dataroot.odsulice nil
    dataroot.odscp nil
    dataroot.odsco nil
    dataroot.odsobec nil
    dataroot.odspsc nil
    dataroot.adrosloveni nil
    dataroot.adrfirma nil
    dataroot.adrosoba nil
    dataroot.adrulice nil
    dataroot.adrcp nil
    dataroot.adrco nil
    dataroot.adrobec nil
    dataroot.adrpsc nil
    dataroot.adriso nil
    dataroot.soubory  do |soubory|
      soubory.soubor(:mimeType => "", :name => @pdf_file_path.split("/").last) do |soubor|
        soubor.dataSoubor Base64.encode64(File.read(@pdf_file_path))
      end
    end
  end

  xml_file = Tempfile.new(["DopisOnlineNew_1_1", ".xml"])
  xml_file.write xml.target!
  xml_file.flush
  xml_file.rewind

  {
    :user => DopisOnlineClient.username,
    :password => DopisOnlineClient.password,
    :soubor => xml_file
  }
end

#deliverObject

Public: Delivers payload to service

Returns new DopisOnlineClient::Response



48
49
50
51
52
# File 'lib/dopis_online_client/request.rb', line 48

def deliver
  response = self.class.post '/dopisonline/donApi.php', :body => body
  parsed_response = parse_response(response.body)
  DopisOnlineClient::Response.new(parsed_response, response.body, response.code)
end

#parse_response(body) ⇒ Object

Internal: parses the body of the response

body - string with service response

Returns nil if the body is not present



60
61
62
63
64
65
66
67
68
# File 'lib/dopis_online_client/request.rb', line 60

def parse_response(body)
  return nil if body.nil? or body.empty?
  case options.format
  when :xml
    MultiXml.parse(body)
  else
    body
  end
end