Class: Issuu::Cli

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

Class Method Summary collapse

Class Method Details

.check_for_exceptions(json_data) ⇒ Object



4
5
6
7
8
# File 'lib/issuu/cli.rb', line 4

def check_for_exceptions(json_data)
  if json_data['rsp']['stat'].eql?("fail")
    IssuuExceptionManager.new(json_data['rsp']["_content"]["error"]).raise_error
  end
end

.decoded_response_body(response_body) ⇒ Object



10
11
12
13
14
# File 'lib/issuu/cli.rb', line 10

def decoded_response_body(response_body)
  json_data = ActiveSupport::JSON.decode(response_body)
  check_for_exceptions(json_data)
  json_data
end

.http_get(url, params) ⇒ Object



16
17
18
19
20
# File 'lib/issuu/cli.rb', line 16

def http_get(url, params)
  path = "#{url.path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&')) unless params.nil?
  request = Net::HTTP.get(url.host, path)
  decoded_response_body(request)
end

.http_multipart_post(url, file, params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/issuu/cli.rb', line 27

def http_multipart_post(url, file, params)
  petition = Net::HTTP::Post::Multipart.new(
    url.path + '?',
    params.merge({:file => file})
  )
  Net::HTTP.start(url.host, url.port) do |http|
    request = http.request(petition)
    decoded_response_body(request.body)
  end
end

.http_post(url, params) ⇒ Object



22
23
24
25
# File 'lib/issuu/cli.rb', line 22

def http_post(url, params)
  request = Net::HTTP.post_form(url+ '?', params)
  decoded_response_body(request.body)
end