Sha256: 719aa906639f6c7c1236d92bda5450f9e93cb2c609b5a5c6e2cf09be006677df

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

require 'rest-client'
require 'json'
require 'hashie'

module SimpleMDM
  class Base < Hashie::Mash

    def self.build(hash = nil)
      if hash
        attrs = {}

        if hash['id']
          attrs[:id] = hash['id']
        end

        if hash['attributes']
          attrs.merge!(hash['attributes'])
        end

        if hash['relationships']
          if hash['relationships']['device_group']
            attrs['device_group_id'] = hash['relationships']['device_group']['data']['id']
          end

          if hash['relationships']['device_groups']
            attrs['device_group_ids'] = hash['relationships']['device_groups']['data'].collect { |o| o['id'] }
          end

          if hash['relationships']['devices']
            attrs['device_ids'] = hash['relationships']['devices']['data'].collect { |o| o['id'] }
          end

          if hash['relationships']['apps']
            attrs['app_ids'] = hash['relationships']['apps']['data'].collect { |o| o['id'] }
          end
        end

        new attrs
      end
    end

    private

    def new?
      self.id.nil?
    end

    def self.fetch(method, verb = :get, params = {})
      headers = { 'SIMPLEMDM-CLIENT-VERSION' => SimpleMDM::VERSION }
      url  = SimpleMDM.api_url + method
      if [:get, :delete].include? verb
        resp = RestClient.send(verb, url, headers)
      else
        resp = RestClient.send(verb, url, params, headers)
      end

      begin
        hash = JSON.parse(resp)
      rescue JSON::ParserError
        hash = nil
      end

      code = resp.code

      return hash, code
    end

    def fetch(method, verb = :get, params = {})
      self.class.fetch(method, verb, params)
    end

  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
fastlane-plugin-simplemdm-0.1.0 lib/simplemdm/base.rb
simplemdm-1.3.0 lib/simplemdm/base.rb
simplemdm-1.2.0 lib/simplemdm/base.rb
simplemdm-1.1.3 lib/simplemdm/base.rb
simplemdm-1.1.2 lib/simplemdm/base.rb
simplemdm-1.0.0 lib/simplemdm/base.rb
simplemdm-0.2.0 lib/simplemdm/base.rb
simplemdm-0.1.1 lib/simplemdm/base.rb