Sha256: c1966b1bdd5416f88b72b73ca6714fccd26ca751d6af2fc8e2f1a10b75c0fdcb

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

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
        faraday.adapter  Faraday.default_adapter
      end

    end

    def add_group(group_name)
      entry_point = "/api/group/#{@op_key}"

      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}"
      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}"

      res = @@conn.post do |req|
        req.url entry_point
        req.headers['Content-Type'] = 'application/json'
        req.body = "{\"url\":\"#{url}\"}"
      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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
impas-client-0.0.5 lib/impas-client.rb
impas-client-0.0.4 lib/impas-client.rb
impas-client-0.0.3 lib/impas-client.rb