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.
-
.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.
-
.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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/service.rb', line 71 def authenticate(login, password) params = [ Curl::PostField.content("login", login), Curl::PostField.content("password", password) ] response = post(auth_endpoint, params) Flareshow::Util.log_info(response) # 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 end |
.commit(params = {}, files = []) ⇒ Object
commit changes to the server with an http post
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/service.rb', line 106 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/service.rb', line 50 def http_get(url) request = Curl::Easy.new(url + "?key=#{@key}") do |curl| curl.headers = { 'User-Agent' => 'flareshow 0.1' } end request.perform() process_response(request) end |
.logout ⇒ Object
clear the authenticated session
88 89 90 |
# File 'lib/service.rb', line 88 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 |
# 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) process_response(request) end |
.process_response(request) ⇒ Object
get the interesting bits out of the curl response
61 62 63 64 65 66 67 68 |
# File 'lib/service.rb', line 61 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"]) Flareshow::Util.log_info(response["status_code"]) end response end |
.query(params = {}) ⇒ Object
query the server with an http post of the query params
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/service.rb', line 93 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 |