Sha256: 63887d70309035d936f35b121ffdd8290c5a22823009405451032b6bc3759fcc

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

require 'httmultiparty'

module Siteleaf
  class Client
    def self.auth(email, password)
      begin
        request = HTTParty.post(Siteleaf.api_url('auth'), {
          :basic_auth => {:username => email, :password => password}
        })
        return request.parsed_response # parse JSON
      rescue => e
        return 'error' => e.message # error
      end
    end

    def self.get(path, params = nil)
      self.execute(:get, path, params)
    end

    def self.post(path, params)
      self.execute(:post, path, params)
    end

    def self.put(path, params)
     self.execute(:put, path, params)
    end

    def self.delete(path, params = nil)
      self.execute(:delete, path, params)
    end

    def self.execute(method, path, params = nil)
      Siteleaf.load_settings if !Siteleaf.api_key
      begin
        request = HTTMultiParty.send(method, Siteleaf.api_url(path), {
          :query => params,
          :basic_auth => {:username => Siteleaf.api_key, :password => Siteleaf.api_secret},
          :timeout => 300
        })
        if request.respond_to?('parsed_response')
          return request.parsed_response # parse JSON
        else
          return request # raw
        end
      rescue => e
        return 'error' => e.message # error
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
siteleaf-1.0.0 lib/siteleaf/client.rb
siteleaf-0.9.23 lib/siteleaf/client.rb
siteleaf-0.9.22 lib/siteleaf/client.rb
siteleaf-0.9.21 lib/siteleaf/client.rb
siteleaf-0.9.20 lib/siteleaf/client.rb
siteleaf-0.9.19 lib/siteleaf/client.rb
siteleaf-0.9.18 lib/siteleaf/client.rb
siteleaf-0.9.17 lib/siteleaf/client.rb
siteleaf-0.9.16 lib/siteleaf/client.rb