Class: Caseblocks::Client

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

Overview

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Since:

  • 0.1.0



8
9
10
11
12
13
14
# File 'lib/caseblocks/client.rb', line 8

def initialize(options={})
  @options = options
  @host = URI.parse(@options[:host] || "https://login.caseblocks.com")
  @authentication_token = @options[:authentication_token]
  @logger = @options[:logger] || Logger.new(STDOUT)
  @cache= {}
end

Class Method Details

.defaultObject

Since:

  • 0.1.0



20
21
22
# File 'lib/caseblocks/client.rb', line 20

def self.default
  @default_client
end

.setup(options) ⇒ Object

Since:

  • 0.1.0



16
17
18
# File 'lib/caseblocks/client.rb', line 16

def self.setup(options)
  @default_client = Client.new(options)
end

Instance Method Details

#case_typesObject

Since:

  • 0.1.0



24
25
26
# File 'lib/caseblocks/client.rb', line 24

def case_types
  Caseblocks::CaseType.all(self)
end

#clear_cache(pattern = nil) ⇒ Object

Since:

  • 0.1.0



28
29
30
31
32
33
34
35
36
37
# File 'lib/caseblocks/client.rb', line 28

def clear_cache(pattern=nil)
  if pattern.nil?
    @cache = {}
  else
    # puts "Clearing Cache for #{pattern}"
    @cache = @cache.reject{|k,v| k.to_s.include?(pattern) }
    # ap @cache.keys
  end

end

#post(url, data) ⇒ Object

Since:

  • 0.1.0



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/caseblocks/client.rb', line 66

def post(url, data)
  uri = @host.merge(url)
  uri.query = (uri.query.to_s.split("&") + {}.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{v.to_s}"}).join("&")
  req = Net::HTTP::Post.new(uri)
  req.body = data.to_json
  req.content_type = "application/json"
  @logger.debug "POST  #{uri.to_s}"
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
  end
  if res.code.to_i >= 200 && res.code.to_i < 300
    JSON.parse(res.body)
  elsif res.code.to_i == 422
    raise Caseblocks::Errors::ValidationError.new("Validation Error", JSON.parse(res.body)["errors"], :url => uri.to_s)
  else
    raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
  end
end

#put(url, data) ⇒ Object

Since:

  • 0.1.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/caseblocks/client.rb', line 85

def put(url, data)
  uri = @host.merge(url)
  uri.query = (uri.query.to_s.split("&") + {}.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{v}"}).join("&")
  @logger.debug "PUT   #{uri.to_s}"
  req = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
  req.body = data.to_json
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
  end
  if res.code.to_i >= 200 && res.code.to_i < 300
    JSON.parse(res.body)
  elsif res.code.to_i == 422
    raise Caseblocks::Errors::ValidationError.new("Validation Error", JSON.parse(res.body)["errors"], :url => uri.to_s)
  else
    raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
  end
end

#request(url, query = {}) ⇒ Object

Since:

  • 0.1.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/caseblocks/client.rb', line 39

def request(url, query={})
  uri = @host.merge(url)
  uri.query = (uri.query.to_s.split("&") + query.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{URI.encode(v.to_s)}"}).join("&")
  req = Net::HTTP::Get.new(uri)
  req['Content-Type'] = "application/json"
  
  if @cache[uri.to_s]
    @logger.debug "CACHE #{uri.to_s}"
    return @cache[uri.to_s] 
  end
  @logger.debug "GET   #{uri.to_s}"
  
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
    http.request(req)
  }
  if res.code.to_i >= 200 && res.code.to_i < 300
    @cache[uri.to_s] = JSON.parse(res.body)
    return @cache[uri.to_s]
  else
    if res.code.to_i == 404
      raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
    else
      raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
    end
  end
end