lib/stella.rb in stella-2.1.1.002 vs lib/stella.rb in stella-2.1.2.001
- old
+ new
@@ -12,11 +12,11 @@
%w{tryouts benelux storable gibbler familia bluth}.each do |dir|
$:.unshift File.join(STELLA_LIB_HOME, '..', '..', dir, 'lib')
end
require 'yajl'
-require 'erb'
+require 'httparty'
require 'storable'
require 'benelux'
require 'gibbler/aliases'
require 'stella/core_ext'
require 'familia'
@@ -38,24 +38,10 @@
@version ||= YAML.load_file(File.join(STELLA_LIB_HOME, '..', 'VERSION.yml'))
end
end
end
-class MatchData
- include Gibbler::String
-end
-
-module Addressable
- class URI
- include Gibbler::String
- end
-end
-
-class OpenStruct
- include Gibbler::Object
-end
-
#
# Any object that wants to be serialized to JSON
# ought to inherit from this class.
#
# NOTE: you cannot define Storable fields here.
@@ -215,22 +201,75 @@
end
end
end
-class Stella::Template
- include Gibbler::String
- attr_reader :src
- def initialize(src)
- src = src.to_s
- @src, @template = src, ERB.new(src)
+class Stella
+ class API
+ include HTTParty
+ ssl_ca_file Stella::Client::SSL_CERT_PATH
+ format :json
+ attr_reader :httparty_opts, :response, :account, :key
+ 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
+ def get path, params=nil
+ opts = httparty_opts
+ opts[:query] = params || {}
+ execute_request :get, path, opts
+ end
+ def post path, params=nil
+ opts = httparty_opts
+ opts[:body] = params || {}
+ execute_request :post, path, opts
+ end
+ def site_uri path
+ uri = Addressable::URI.parse self.class.base_uri
+ uri.path = uri_path(path)
+ uri.to_s
+ end
+ private
+ def uri_path *args
+ args.unshift '' # force leading slash
+ path = args.flatten.join('/')
+ path.gsub '//', '/'
+ end
+ def execute_request meth, path, opts
+ path = uri_path [path]
+ @response = self.class.send meth, path, opts
+ indifferent_params @response.parsed_response
+ end
+ # Enable string or symbol key access to the nested params hash.
+ 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
+ # Creates a Hash with indifferent access.
+ def indifferent_hash
+ Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
+ end
+
+ class Unauthorized < RuntimeError
+ end
end
- def result(binding)
- @template.result(binding)
- end
- def self.from_file(path)
- new File.read(path)
- end
- def to_s() src end
end