lib/impas-client.rb in impas-client-0.0.2 vs lib/impas-client.rb in impas-client-0.0.3

- old
+ new

@@ -1,45 +1,76 @@ require "impas-client/version" require 'faraday' +require 'json' module Impas class Client attr_accessor :api_url, :op_key def initialize(args) @api_url = (args[:api_url].nil?) ? API_URL : args[:api_url] @op_key = args[:op_key] @@conn = Faraday.new(:url => @api_url) do |faraday| - faraday.request :url_encoded # form-encode POST params - faraday.response :logger # log requests to STDOUT - faraday.adapter Faraday.default_adapter # make requests with Net::HTTP + faraday.request :url_encoded + faraday.adapter Faraday.default_adapter end + end def add_group(group_name) entry_point = "/api/group/#{@op_key}" - @@conn.post do |req| + res = @@conn.post do |req| req.url entry_point req.headers['Content-Type'] = 'application/json' req.body = "{\"name\":\"#{group_name}\"}" end + + if res.status == 200 + desc = JSON.parse(res.body) + + if desc["result"] != "ok" + raise StandardError.new("Process error. message:#{desc['explain']}") + end + else + raise StandardError.new("HTTP status:#{res.status}") + end + + true end def groups entry_point = "/api/group/#{@op_key}" - @@conn.get entry_point + res = @@conn.get entry_point + + if res.status != 200 + raise StandardError.new("HTTP status:#{res.status}") + end + + desc = JSON.parse(res.body) + desc["description"]["groups"] end def add_url(grp_key, url) entry_point = "/api/registration/#{grp_key}" - @@conn.post do |req| + res = @@conn.post do |req| req.url entry_point req.headers['Content-Type'] = 'application/json' req.body = "{\"url\":\"#{url}\"}" end - end + if res.status == 200 + desc = JSON.parse(res.body) + + if desc["result"] != "ok" + raise StandardError.new("Process error. message:#{desc['explain']}") + end + else + raise StandardError.new("HTTP status:#{res.status}") + end + + true + end end end