Class: Pandexio::HttpClient

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

Constant Summary collapse

HOSTED_HOST =
"hosted.pandexio.com"
PLATFORM_HOST =
"platform.pandexio.com"

Instance Method Summary collapse

Instance Method Details

#get_document(document_id, cover_size, signing_options) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/http_client.rb', line 111

def get_document(document_id, cover_size, signing_options)

    raise ArgumentError, "Signing options is nil." if signing_options.nil?

    id = document_id.to_s

    raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
    raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'

    path = "/v2/documents/#{id}"

    normalized_request = Pandexio::Request.new(
        :method => "GET",
        :path => path,
        :query_parameters => { "coverSize" => cover_size },
        :headers => { "Host" => PLATFORM_HOST },
        :payload => '')

    signer = Pandexio::Signer.new()

    authorized_request = signer.sign(normalized_request, signing_options)

    headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
    query_string = build_query_string(authorized_request.query_parameters)

    http = Net::HTTP.new(PLATFORM_HOST, 443)
    http.use_ssl = true
    http.open_timeout = 60
    http.read_timeout = 120

    req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
    res = http.start {|http| http.request(req)}

    case res.code
        when "200"
            data = JSON.parse(res.body)
            document = Pandexio::Document.new(
                :document_id => data['documentId'],
                :name => data['name'],
                :page_count => data['pageCount'],
                :content_type => data['contentType'],
                :content_length => data['contentLength'],
                :cover => data['cover'])
        when "404"
            return nil
        else
            raise StandardError, "Failed to get document."
    end

end

#get_document_cover(document_id, cover_size, signing_options) ⇒ Object

Raises:

  • (ArgumentError)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/http_client.rb', line 162

def get_document_cover(document_id, cover_size, signing_options)

    raise ArgumentError, "Signing options is nil." if signing_options.nil?

    id = document_id.to_s

    raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
    raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'

    path = "/v2/documents/#{id}/cover"

    normalized_request = Pandexio::Request.new(
        :method => "GET",
        :path => path,
        :query_parameters => { "coverSize" => cover_size },
        :headers => { "Host" => PLATFORM_HOST },
        :payload => '')

    signer = Pandexio::Signer.new()

    authorized_request = signer.sign(normalized_request, signing_options)

    headers = build_headers(authorized_request.headers, 'image/png')
    query_string = build_query_string(authorized_request.query_parameters)

    http = Net::HTTP.new(PLATFORM_HOST, 443)
    http.use_ssl = true
    http.open_timeout = 60
    http.read_timeout = 120

    req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
    res = http.start {|http| http.request(req)}

    case res.code
        when "200"
            return res.body
        when "404"
            return nil
        else
            raise StandardError, "Failed to get document cover."
    end

end

#get_document_cover_url(document_id, cover_size, signing_options) ⇒ Object

Raises:

  • (ArgumentError)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/http_client.rb', line 206

def get_document_cover_url(document_id, cover_size, signing_options)

    id = document_id.to_s

    raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
    raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'

    raise ArgumentError, "Signing options is nil." if signing_options.nil?
    raise ArgumentError, "Signing mechanism must be QueryString." unless signing_options.mechanism == Pandexio::SigningMechanisms::QUERY_STRING

    path = "/v2/documents/#{id}/cover"

    normalized_request = Pandexio::Request.new(
        :method => "GET",
        :path => path,
        :query_parameters => { "coverSize" => cover_size },
        :headers => { "Host" => PLATFORM_HOST },
        :payload => '')

    signer = Pandexio::Signer.new()

    authorized_request = signer.sign(normalized_request, signing_options)

    query_string = build_query_string(authorized_request.query_parameters)

    return "https://#{PLATFORM_HOST}#{path}?#{query_string}"

end

#get_document_snip_count(document_id, signing_options) ⇒ Object

Raises:

  • (ArgumentError)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/http_client.rb', line 235

def get_document_snip_count(document_id, signing_options)

    raise ArgumentError, "Signing options is nil." if signing_options.nil?

    id = document_id.to_s

    raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?

    path = "/v2/documents/#{id}/snips/count"

    normalized_request = Pandexio::Request.new(
        :method => "GET",
        :path => path,
        :query_parameters => {},
        :headers => { "Host" => PLATFORM_HOST },
        :payload => '')

    signer = Pandexio::Signer.new()

    authorized_request = signer.sign(normalized_request, signing_options)

    headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
    query_string = build_query_string(authorized_request.query_parameters)

    http = Net::HTTP.new(PLATFORM_HOST, 443)
    http.use_ssl = true

    req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
    res = http.start {|http| http.request(req)}

    case res.code
        when "200"
            return res.body.to_i
        else
            raise StandardError, "Failed to get document snip count."
    end

end

#load_document(source_document, signing_options) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/http_client.rb', line 50

def load_document(source_document, signing_options)

    raise ArgumentError, "Document source is nil." if source_document.nil?
    raise ArgumentError, "Signing options is nil." if signing_options.nil?

    id = source_document.id.to_s
    name = source_document.name.to_s
    content = source_document.content.to_s
    location = source_document.location.to_s

    raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !source_document.id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
    raise ArgumentError, "Document name is not a string or is nil or empty." if !source_document.name.is_a?(String) || name.nil? || name.empty? || !name.strip!.nil?
    raise ArgumentError, "Document content is not a string or is nil or empty." if !source_document.content.is_a?(String) || content.nil? || content.empty? || !content.strip!.nil?
    raise ArgumentError, "Document location must be AWS, Azure, Internet, or Inline." unless location == "AWS" || location == "Azure" || location == "Internet" || location == "Inline"

    path = "/api/documents/#{id}"
    payload = {
        "name" => name,
        "content" => content,
        "location" => location
    }.to_json

    normalized_request = Pandexio::Request.new(
        :method => "PUT",
        :path => path,
        :query_parameters => { },
        :headers => { "Host" => HOSTED_HOST },
        :payload => payload)

    signer = Pandexio::Signer.new()

    authorized_request = signer.sign(normalized_request, signing_options)

    headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
    query_string = build_query_string(authorized_request.query_parameters)

    http = Net::HTTP.new(HOSTED_HOST, 443)
    http.use_ssl = true
    http.open_timeout = 60
    http.read_timeout = 600

    req = Net::HTTP::Put.new("#{path}?#{query_string}", initheader = headers)
    req.body = payload
    res = http.start {|http| http.request(req)}

    case res.code
        when "202"
            data = JSON.parse(res.body)
            document = Pandexio::Document.new(
                :document_id => data['documentId'],
                :name => data['name'],
                :page_count => data['pageCount'],
                :content_type => data['contentType'],
                :content_length => data['contentLength'],
                :cover => data['cover'])
        else
            raise StandardError, "Failed to load document."
    end

end