Sha256: 9b737e89a20ceec2506c43e7071ea0f15a85b39de2994e4380c230eae65042be

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

require 'httparty'

module Siteleaf
  class Client
    def self.auth(email, password)
      request = HTTParty.post(Siteleaf.api_url('auth'), {
        :basic_auth => {:username => email, :password => password},
        :headers => {"User-Agent" => "Siteleaf Gem/#{Siteleaf::VERSION}"}
      })
      response = request.parsed_response
      if response.is_a?(Hash) && error = response['message'] || response['error']
        raise error
      end
      response
    end

    def self.get(path, params = {})
      params['per_page'] = 9999 # todo: paginate
      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
      
      options = {
        :basic_auth => {:username => Siteleaf.api_key, :password => Siteleaf.api_secret},
        :headers => {"User-Agent" => "Siteleaf Gem/#{Siteleaf::VERSION}"},
        :timeout => 300
      }
      
      if method == :get || method == :delete
        options[:query] = params
      elsif params.has_key?('file') || params.has_key?(:file)
        options[:body] = params
      else
        options[:body] = params.to_json
        options[:headers]["Content-Type"] = "application/json"
      end
      
      request = HTTParty.send(method, Siteleaf.api_url(path), options)
      response = request.parsed_response
      if response.is_a?(Hash) && error = response['message'] || response['error']
        raise error
      end
      response
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
siteleaf-2.3.0 lib/siteleaf/client.rb
siteleaf-2.2.1 lib/siteleaf/client.rb
siteleaf-2.2.0 lib/siteleaf/client.rb
siteleaf-2.1.2 lib/siteleaf/client.rb
siteleaf-2.1.1 lib/siteleaf/client.rb
siteleaf-2.1.0 lib/siteleaf/client.rb