class Stella::API

Attributes

account[R]
httparty_opts[R]
key[R]
response[R]

Public Class Methods

new(account=nil, key=nil, httparty_opts={}) click to toggle source
# File lib/stella.rb, line 211
def initialize account=nil, key=nil, httparty_opts={}
  self.class.base_uri ENV['STELLA_HOST'] || 'https://www.blamestella.com/api/v2'
  @httparty_opts = httparty_opts
  @account = account || ENV['STELLA_ACCOUNT']
  @key = key || ENV['STELLA_KEY']
  unless @account.to_s.empty? || @key.to_s.empty?
    httparty_opts[:basic_auth] ||= { :username => @account, :password => @key }
  end
end

Public Instance Methods

get(path, params=nil) click to toggle source
# File lib/stella.rb, line 220
def get path, params=nil
  opts = httparty_opts
  opts[:query] = params || {}
  execute_request :get, path, opts
end
post(path, params=nil) click to toggle source
# File lib/stella.rb, line 225
def post path, params=nil
  opts = httparty_opts
  opts[:body] = params || {}
  execute_request :post, path, opts
end
site_uri(path) click to toggle source
# File lib/stella.rb, line 230
def site_uri path
  uri = Addressable::URI.parse self.class.base_uri
  uri.path = uri_path(path)
  uri.to_s
end

Private Instance Methods

execute_request(meth, path, opts) click to toggle source
# File lib/stella.rb, line 241
def execute_request meth, path, opts
  path = uri_path [path]
  @response = self.class.send meth, path, opts
  indifferent_params @response.parsed_response
end
indifferent_hash() click to toggle source

Creates a Hash with indifferent access.

# File lib/stella.rb, line 265
def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
indifferent_params(params) click to toggle source

Enable string or symbol key access to the nested params hash.

# File lib/stella.rb, line 247
def indifferent_params(params)
  if params.is_a?(Hash)
    params = indifferent_hash.merge(params)
    params.each do |key, value|
      next unless value.is_a?(Hash) || value.is_a?(Array)
      params[key] = indifferent_params(value)
    end
  elsif params.is_a?(Array)
    params.collect! do |value|
      if value.is_a?(Hash) || value.is_a?(Array)
        indifferent_params(value)
      else
        value
      end
    end
  end
end
uri_path(*args) click to toggle source
# File lib/stella.rb, line 236
def uri_path *args
  args.unshift ''  # force leading slash
  path = args.flatten.join('/')
  path.gsub '//', '/'
end