Class: Flareshow::Service
- Inherits:
-
Object
- Object
- Flareshow::Service
- Defined in:
- lib/service.rb
Overview
provides an interface to the shareflow api
Class Attribute Summary collapse
-
.server ⇒ Object
Returns the value of attribute server.
Class Method Summary collapse
-
.api_endpoint ⇒ Object
return the api endpoint for a given host and domain.
-
.auth_endpoint ⇒ Object
return the authentication endpoint for a given host and domain.
-
.authenticate(login, password) ⇒ Object
authenticate with the server using an http post.
-
.authenticated? ⇒ Boolean
are we authenticated.
-
.commit(params = {}, files = []) ⇒ Object
commit changes to the server with an http post.
-
.configure(subdomain = nil, host = 'biz.zenbe.com') ⇒ Object
setup the service to use a particular host and domain.
-
.http_get(url) ⇒ Object
do a get request.
-
.log_service_error(response) ⇒ Object
log a service error.
-
.logout ⇒ Object
clear the authenticated session.
-
.post(url, params) ⇒ Object
make a post request to an endpoint returns a hash of - status code - headers - body.
-
.process_response(request) ⇒ Object
get the interesting bits out of the curl response.
-
.query(params = {}) ⇒ Object
query the server with an http post of the query params.
-
.server_defined? ⇒ Boolean
has the server been configured?.
Class Attribute Details
.server ⇒ Object
Returns the value of attribute server.
8 9 10 |
# File 'lib/service.rb', line 8 def server @server end |
Class Method Details
.api_endpoint ⇒ Object
return the api endpoint for a given host and domain
22 23 24 |
# File 'lib/service.rb', line 22 def api_endpoint "http://#{server.host}/#{server.domain}/shareflow/api/v2.json" end |
.auth_endpoint ⇒ Object
return the authentication endpoint for a given host and domain
17 18 19 |
# File 'lib/service.rb', line 17 def auth_endpoint "http://#{server.host}/#{server.domain}/shareflow/api/v2/auth.json" end |
.authenticate(login, password) ⇒ Object
authenticate with the server using an http post
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/service.rb', line 98 def authenticate(login, password) params = [ Curl::PostField.content("login", login), Curl::PostField.content("password", password) ] response = post(auth_endpoint, params) # store the auth token returned from the authentication request if response["status_code"] == 200 @key = response["resources"]["data"]["auth_token"] response else raise Flareshow::AuthenticationFailed end rescue Exception => e Flareshow::Util.log_error e. end |
.authenticated? ⇒ Boolean
are we authenticated
121 122 123 |
# File 'lib/service.rb', line 121 def authenticated? !!@key end |
.commit(params = {}, files = []) ⇒ Object
commit changes to the server with an http post
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/service.rb', line 139 def commit(params={}, files=[]) raise Flareshow::AuthenticationRequired unless @key curl_params = [] has_files = false if params["posts"] # add any file parts passed in and assign a part id to the params["posts"] = (params["posts"]).map do |f| if f["files"] f["files"] = (f["files"]).each do |ff| has_files = true curl_params.push(Curl::PostField.file(ff["part_id"], ff["file_path"])) end end f end end params["files"] = [] # add the json request parts curl_params += [ Curl::PostField.content("key", @key, 'application/json'), Curl::PostField.content("data", params.to_json, 'application/json') ] post(api_endpoint, curl_params) end |
.configure(subdomain = nil, host = 'biz.zenbe.com') ⇒ Object
setup the service to use a particular host and domain
11 12 13 14 |
# File 'lib/service.rb', line 11 def configure(subdomain=nil, host='biz.zenbe.com') raise Flareshow::ConfigurationException unless subdomain self.server=Server.new(host, subdomain) end |
.http_get(url) ⇒ Object
do a get request
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/service.rb', line 62 def http_get(url) request = Curl::Easy.new(url + "?key=#{@key}") do |curl| curl.headers = { 'User-Agent' => 'flareshow 0.1' } end request.perform() response = process_response(request) Flareshow::Util.log_error("resource not found") if response["status_code"] == 404 response end |
.log_service_error(response) ⇒ Object
log a service error
88 89 90 91 92 93 94 95 |
# File 'lib/service.rb', line 88 def log_service_error(response) if response["resources"] Flareshow::Util.log_error(response["resources"]["message"]) Flareshow::Util.log_error(response["resources"]["details"]) else Flareshow::Util.log_error(response["body"]) end end |
.logout ⇒ Object
clear the authenticated session
116 117 118 |
# File 'lib/service.rb', line 116 def logout @key = nil end |
.post(url, params) ⇒ Object
make a post request to an endpoint returns a hash of
- status code
- headers
- body
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/service.rb', line 36 def post(url, params) raise Flareshow::ConfigurationException unless server_defined? request = Curl::Easy.new(url) do |curl| curl.headers = { 'Accept' => 'application/json', 'User-Agent' => 'flareshow 0.1' } curl.multipart_form_post=true end request.http_post(*params) response = process_response(request) # log a service exception case response["status_code"] when 400 log_service_error(response) when 500 log_service_error(response) when 403 log_service_error(response) end response end |
.process_response(request) ⇒ Object
get the interesting bits out of the curl response
77 78 79 80 81 82 83 84 85 |
# File 'lib/service.rb', line 77 def process_response(request) response = {"status_code" => request.response_code, "headers" => request.header_str, "body" => request.body_str} if (/json/i).match(request.content_type) response["resources"] = JSON.parse(response["body"]) end Flareshow::Util.log_info(response["status_code"]) Flareshow::Util.log_info(response["body"]) response end |
.query(params = {}) ⇒ Object
query the server with an http post of the query params
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/service.rb', line 126 def query(params={}) raise Flareshow::AuthenticationRequired unless @key # add the json request parts params = [ Curl::PostField.content("key", @key, 'application/json'), Curl::PostField.content("query", params.to_json, 'application/json') ] post(api_endpoint, params) end |
.server_defined? ⇒ Boolean
has the server been configured?
27 28 29 |
# File 'lib/service.rb', line 27 def server_defined? !!server end |