Initial working version of the BurstNet test API.

This commit is contained in:
dlage committed 2013-06-02 16:06:53 +01:00
commit 74d5b88ecd
34 files changed
+611

No files matched your search

+161
View File
@@ -0,0 +1,161 @@
require 'rubygems'
require 'httparty'
require 'json'
=begin BurstNet API methods
GET order
GET order/:id
GET order/active/
GET order/cancelled/
GET order/fraud/
GET order/pending/
GET product
GET product/:id
GET product/backup/
GET product/backup/:id
GET product/cp/
GET product/cp/:id
GET product/os/
GET product/os/:id
GET product/site/
GET product/site/:id
GET service
GET service/:id
GET service/active/
GET service/cancelled/
GET service/fraud/
GET service/pending/
GET service/suspended/
GET service/terminated/
POST order
POST service/:id
PUT service/:id
=end
module BurstNet
class API
include HTTParty
base_uri 'https://rsapi.burst.net'
format :json
headers 'Accept' => 'application/json', 'Content-Type' => 'application/json'
def self.configuration
@configuration ||= {}
end
def self.configure
yield(configuration) if block_given?
end
def initialize
self.class.default_options.merge!(self.class.configuration)
end
def get(path, options = {})
response = self.class.get(path, options)
if response.success?
response.parsed_response
else
raise response.response
end
end
def post(path, options = {})
response = self.class.post(path, options).parsed_response
if response.success?
response.parsed_response
else
raise response.response
end
end
def put(path, options = {})
response = self.class.put(path, options).parsed_response
if response.success?
response.parsed_response
else
raise response.response
end
end
def delete(path, options = {})
response = self.class.delete(path, options).parsed_response
if response.success?
response.parsed_response
else
raise response.response
end
end
def get_products
self.get('/product')
end
def get_product(id)
self.get("/product/#{id}")
end
def get_product_backup(id)
self.get("/product/backup/#{id}")
end
def get_product_cp(id)
self.get("/product/cp/#{id}")
end
def get_product_os(id)
self.get("/product/os/#{id}")
end
def get_product_site(id)
self.get("/product/site/#{id}")
end
def get_services
self.get('/service')
end
def get_service(id)
self.get("/service/#{id}")
end
def terminate_service(id)
self.delete("/service/#{id}")
end
def change_service(id, params)
options = {:body => params}
self.post(id, options)
end
def update_service(id, params)
options = {:body => params}
self.put(id, options)
end
def get_services_by_status(status)
possible_status = %w(active cancelled fraud pending suspended terminated)
raise('Unknown service status') unless possible_status.include?(status)
self.get("/service/#{status}")
end
def get_orders
self.get('/order')
end
def get_orders_by_status(status)
possible_status = %w(active cancelled fraud pending)
raise('Unknown order status') unless possible_status.include?(status)
self.get("/order/#{status}")
end
def get_order(id)
self.get("/order/#{id}")
end
def create_order(params)
options = {:body => params}
self.post(id, options)
end
end
end
+7
View File
@@ -0,0 +1,7 @@
module BurstNet
class Model
def self.burstnet
@burstnet ||= BurstNet::API.new
end
end
end
+15
View File
@@ -0,0 +1,15 @@
class BurstNetParser < Faraday::Response::Middleware
def on_complete(env)
json = MultiJson.load(env[:body], symbolize_keys: true)
json.each do |k, v|
if k == :product
json = v[0]
break
end
end
puts json
env[:body] = {
data: json,
}
end
end
@@ -0,0 +1,31 @@
module SpreeBurstnet
module Generators
class InstallGenerator < Rails::Generators::Base
class_option :auto_run_migrations, :type => :boolean, :default => false
def add_javascripts
append_file 'app/assets/javascripts/store/all.js', "//= require store/spree_burstnet\n"
append_file 'app/assets/javascripts/admin/all.js', "//= require admin/spree_burstnet\n"
end
def add_stylesheets
inject_into_file 'app/assets/stylesheets/store/all.css', " *= require store/spree_burstnet\n", :before => /\*\//, :verbose => true
inject_into_file 'app/assets/stylesheets/admin/all.css', " *= require admin/spree_burstnet\n", :before => /\*\//, :verbose => true
end
def add_migrations
run 'bundle exec rake railties:install:migrations FROM=spree_burstnet'
end
def run_migrations
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
if run_migrations
run 'bundle exec rake db:migrate'
else
puts 'Skipping rake db:migrate, don\'t forget to run it!'
end
end
end
end
end
+3
View File
@@ -0,0 +1,3 @@
require 'spree_core'
require 'spree_burstnet/engine'
require 'httparty'
+22
View File
@@ -0,0 +1,22 @@
module SpreeBurstnet
class Engine < Rails::Engine
require 'spree/core'
isolate_namespace Spree
engine_name 'spree_burstnet'
config.autoload_paths += %W(#{config.root}/lib)
# use rspec for tests
config.generators do |g|
g.test_framework :rspec
end
def self.activate
Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
config.to_prepare &method(:activate).to_proc
end
end
+6
View File
@@ -0,0 +1,6 @@
FactoryGirl.define do
# Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
#
# Example adding this to your spec_helper will load these Factories for use:
# require 'spree_burstnet/factories'
end