class Ulule::Client

Attributes

key[RW]
username[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ulule/client.rb, line 7
def initialize(options = {})

  @key = options.fetch(:key) { ENV.fetch('ULULE_API_KEY') }
  @username = options.fetch(:username) { ENV.fetch('ULULE_USERNAME') }
  @host = options.fetch(:host) { 'https://api.ulule.com/v1/' }
  @projects = 'projects'
  @comments = 'comments'
  @supporters = 'supporters'
  @news = 'news'
  @users = 'users'

end

Public Instance Methods

me() click to toggle source
# File lib/ulule/client.rb, line 20
def me
  get("#{@users}/#{@username}")
end
my_projects(options = {}) click to toggle source
# File lib/ulule/client.rb, line 24
def my_projects(options = {})
  get("#{@users}/#{@username}/#{@projects}", options)
end
news(id) click to toggle source
# File lib/ulule/client.rb, line 44
def news id
  get("#{@news}/#{id}")
end
news_comments(id, options = {}) click to toggle source
# File lib/ulule/client.rb, line 48
def news_comments(id, options = {})
  get("#{@news}/#{id}/#{@comments}", options)
end
project(slug) click to toggle source
# File lib/ulule/client.rb, line 32
def project slug
  get("#{@projects}/#{slug}")
end
project_comments(slug, options = {}) click to toggle source
# File lib/ulule/client.rb, line 36
def project_comments(slug, options = {})
  get("#{@projects}/#{slug}/#{@comments}", options)
end
project_news(slug) click to toggle source
# File lib/ulule/client.rb, line 40
def project_news slug
  get("#{@projects}/#{slug}/#{@news}")
end
projects(user, options = {}) click to toggle source
# File lib/ulule/client.rb, line 28
def projects(user, options = {})
  get("#{@users}/#{user}/#{@projects}", options)
end
supporters(slug) click to toggle source
# File lib/ulule/client.rb, line 52
def supporters slug
  get ("#{@projects}/#{slug}/#{@supporters}")
end

Private Instance Methods

build_params(params) click to toggle source
# File lib/ulule/client.rb, line 76
def build_params params
  str = nil
  params.each_key do |key|
    str = "#{str}#{CGI::escape(key.to_s)}=#{CGI::escape(params[key])}&" unless params[key].empty?
  end
   str.nil? ? '' : "?#{str[0...-1]}"
end
get(path, params = {}) click to toggle source
# File lib/ulule/client.rb, line 58
def get(path, params = {})
  uri = URI "#{@host}#{path}#{build_params params}"
  req = Net::HTTP::Get.new uri
  req['Authorization'] = "ApiKey #{username}:#{key}"
  parse Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
end
parse(http_response) click to toggle source
# File lib/ulule/client.rb, line 65
def parse http_response
  case http_response
    when Net::HTTPSuccess
      JSON.parse http_response.body
    when Net::HTTPUnauthorized
      raise AuthenticationError
    else
      raise Error, "Unexpected HTTP response, code #{http_response.code}"
  end
end