Class: Caseblocks::Client
- Inherits:
-
Object
- Object
- Caseblocks::Client
- Defined in:
- lib/caseblocks/client.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #case_types ⇒ Object
- #clear_cache(pattern = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(url, data) ⇒ Object
- #put(url, data) ⇒ Object
- #request(url, query = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 |
# File 'lib/caseblocks/client.rb', line 8 def initialize(={}) @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
.default ⇒ Object
20 21 22 |
# File 'lib/caseblocks/client.rb', line 20 def self.default @default_client end |
Instance Method Details
#case_types ⇒ Object
24 25 26 |
# File 'lib/caseblocks/client.rb', line 24 def case_types Caseblocks::CaseType.all(self) end |
#clear_cache(pattern = nil) ⇒ Object
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
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
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
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 |