Class: Wordy::Cli

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

Class Method Summary collapse

Class Method Details

.check_for_exceptions(json_data) ⇒ Object



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

def check_for_exceptions(json_data)
  if (!json_data.is_a? Array) && json_data['error'].present?
    raise WordyException, json_data['verbose']
  end
end

.decoded_response_body(response_body) ⇒ Object



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

def decoded_response_body(response_body)
  if !response_body.empty?
    json_data = ActiveSupport::JSON.decode(response_body)
    check_for_exceptions(json_data)
    json_data
  else
    {}
  end
end

.http_get(url, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wordy/cli.rb', line 20

def http_get(url, params)
  path = "#{url.path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&')) unless params.nil?
  
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
  request = Net::HTTP::Get.new(url.request_uri)        
  request.basic_auth Wordy.username, Wordy.api_key
  
  response = http.request(request)
  decoded_response_body(response.body)
end

.http_post(url, params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wordy/cli.rb', line 34

def http_post(url, params)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
  request = Net::HTTP::Post.new(url.request_uri)
  request.set_form_data(params)
  request.basic_auth Wordy.username, Wordy.api_key
  
  response = http.request(request)
  decoded_response_body(response.body)
end