Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic structure and ApiKey implemented

  • Loading branch information...
commit 92f08b22d91a56e6d551668374e2e6b5c54fbae4 0 parents
@mrsimo mrsimo authored
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
9 Gemfile
@@ -0,0 +1,9 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in amiando.gemspec
+gemspec
+
+unless ENV["TRAVIS"]
+ gem 'ruby-debug19', :platforms => :ruby_19
+ gem 'ruby-debug', :platforms => :mri_18
+end
12 Rakefile
@@ -0,0 +1,12 @@
+require 'bundler/gem_tasks'
+require 'rake'
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+ t.pattern = "test/**/*_test.rb"
+ t.verbose = true
+end
+Rake::Task['test'].comment = "Run all tests"
27 amiando.gemspec
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "amiando/version"
+
+Gem::Specification.new do |s|
+ s.name = "amiando"
+ s.version = Amiando::VERSION
+ s.authors = ["Albert Llop"]
+ s.email = ["mrsimo@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "amiando"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency 'yajl-ruby', '0.8.2'
+ s.add_dependency 'typhoeus', '0.2.4'
+
+ s.add_development_dependency 'minitest'
+ s.add_development_dependency 'webmock'
+ s.add_development_dependency 'rake'
+end
51 lib/amiando.rb
@@ -0,0 +1,51 @@
+require "amiando/version"
+require "typhoeus"
+require "yajl"
+
+module Amiando
+ autoload :Request, 'amiando/request'
+ autoload :Resource, 'amiando/resource'
+ autoload :ApiKey, 'amiando/api_key'
+
+ module Error
+ class ServiceDown < Exception; end
+ class ApiKeyNeeded < Exception; end
+ class NotAuthorized < Exception; end
+ class NotFound < Exception; end
+ class NotImplemented < Exception; end
+ end
+
+ class << self
+ attr_accessor :api_key
+
+ URL = 'https://amiando.com'
+ TEST_URL = 'https://test.amiando.com'
+
+ def production!
+ @production = true
+ end
+
+ def development!
+ @production = false
+ end
+
+ def base_url
+ @production ? URL : TEST_URL
+ end
+
+ def requests
+ @requests ||= []
+ end
+
+ def run
+ requests.each{ |request| hydra.queue(request) }
+ hydra.run
+ ensure
+ @requests = []
+ end
+
+ def hydra
+ @hydra ||= Typhoeus::Hydra.new
+ end
+ end
+end
42 lib/amiando/api_key.rb
@@ -0,0 +1,42 @@
+module Amiando
+
+ ##
+ # http://developers.amiando.com/index.php/REST_API_ApiKey
+ class ApiKey < Resource
+
+ ##
+ # Creates an {ApiKey}.
+ #
+ # @param [Hash] possible attributes that can be set on creation.
+ def self.create(attributes)
+ raise ArgumentError.new('ApiKey name field is mandatory') unless attributes[:name]
+
+ object = new
+ request = post object, '/api/apiKey/create', attributes
+
+ object
+ end
+
+ ##
+ # Updates an {ApiKey}.
+ #
+ # @param [Hash] possible attributes that can be updated.
+ def self.update(id, attributes)
+ object = new
+ request = post object, "/api/apiKey/#{id}", attributes
+
+ object
+ end
+
+ def populate(response_body)
+ @attributes = {}
+ if response_body['apiKey']
+ response_body['apiKey'].each do |key,val|
+ @attributes[key.to_sym] = val
+ end
+ end
+
+ @success = response_body['success']
+ end
+ end
+end
19 lib/amiando/request.rb
@@ -0,0 +1,19 @@
+module Amiando
+ class Request < Typhoeus::Request
+ attr_reader :object
+
+ def initialize(object, verb, path, params = {})
+ @object = object
+ super(build_url(path), :method => verb, :params => params, :verbose => 1)
+ end
+
+ private
+
+ def build_url(url)
+ url = URI.join(::Amiando.base_url, url).to_s
+ url = "#{url}?format=json&version=1"
+ url = "#{url}&apikey=#{Amiando.api_key}" if Amiando.api_key
+ url
+ end
+ end
+end
53 lib/amiando/resource.rb
@@ -0,0 +1,53 @@
+module Amiando
+ class Resource
+ attr_accessor :request, :response
+ attr_reader :success
+
+ class << self
+ def post(object, path, params = {})
+ req = Request.new(object, :post, path, params)
+ object.request = req
+
+ req.on_complete do |response|
+
+ # Raise different errors depending on the return codes
+ case response.code
+ when 503
+ raise Error::ServiceDown.new(response.body)
+ when 403
+ raise Error::NotAuthorized.new(response.body)
+ when 404
+ raise Error::NotFound.new(response.body)
+ end
+
+ parsed_body = Yajl::Parser.parse(response.body)
+
+ object.response = response
+ object.populate(parsed_body)
+ end
+
+ Amiando.requests << req
+ end
+ end
+
+ def [](key)
+ @attributes[key.to_sym]
+ end
+
+ def method_missing(method_name, *args, &block)
+ if @attributes.key?(method_name) && args.empty?
+ @attributes[method_name]
+ else
+ super
+ end
+ end
+
+ def id
+ @attributes[:id]
+ end
+
+ def populate
+ raise Error::NotImplemented.new("populate method not implemented for #{self.class}")
+ end
+ end
+end
3  lib/amiando/version.rb
@@ -0,0 +1,3 @@
+module Amiando
+ VERSION = "0.0.1"
+end
13 test/amiando/amiando_test.rb
@@ -0,0 +1,13 @@
+require 'test_helper'
+
+describe Amiando do
+ it 'should return the right url for the test environment' do
+ Amiando.base_url.must_equal 'https://test.amiando.com'
+ end
+
+ it 'should return the right url for the production environment' do
+ Amiando.production!
+ Amiando.base_url.must_equal 'https://amiando.com'
+ Amiando.development!
+ end
+end
66 test/amiando/api_key_test.rb
@@ -0,0 +1,66 @@
+require 'test_helper'
+
+describe Amiando::ApiKey do
+ before do
+ Amiando.api_key = nil
+ HydraCache.prefix = 'ApiKey'
+ end
+
+ describe 'create' do
+ it 'creates an api key' do
+ key = Amiando::ApiKey.create :name => 'wadus'
+ Amiando.run
+
+ key.id.wont_be_nil
+ key.name.must_equal 'wadus'
+ key.identifier.wont_be_nil
+ key.key.wont_be_nil
+ key.enabled.wont_be_nil
+ end
+
+ it 'fails when creating with empty parameters' do
+ lambda { key = Amiando::ApiKey.create({}) }.must_raise ArgumentError
+ end
+
+ end
+
+ describe 'update' do
+ it 'can update its enabled attribute' do
+ key = Amiando::ApiKey.create :name => 'wadus'
+ Amiando.run
+
+ Amiando.api_key = key.key
+
+ result = Amiando::ApiKey.update(key.id, :enabled => false)
+ Amiando.run
+
+ result.success.must_equal true
+ end
+
+ it 'update raises NotAuthorized if apikey doesnt have permission to update the apikey' do
+ key1 = Amiando::ApiKey.create :name => 'wadus'
+ key2 = Amiando::ApiKey.create :name => 'wadus1'
+ Amiando.run
+
+ Amiando.api_key = key1.key
+
+ lambda {
+ result = Amiando::ApiKey.update(key2.id, :enabled => false)
+ Amiando.run
+ }.must_raise Amiando::Error::NotAuthorized
+ end
+
+ it 'update fails if apikey is missing' do
+ key = Amiando::ApiKey.create :name => 'wadus'
+ Amiando.run
+
+ result = Amiando::ApiKey.update(key.id, :enabled => false)
+ Amiando.run
+
+ result.success.must_equal false
+ end
+ end
+
+ # it 'raises an error when updating an api key that doesnt exist' do
+ # end
+end
23 test/amiando/resource_test.rb
@@ -0,0 +1,23 @@
+require 'test_helper'
+
+describe Amiando::Resource do
+ class Wadus < Amiando::Resource
+ def self.create
+ post new, 'somewhere'
+ end
+ end
+
+ it 'raises error when amiando is down' do
+ stub_request(:post, /somewhere/).to_return(:status => 503)
+ lambda {
+ key = Wadus.create
+ Amiando.run
+ }.must_raise Amiando::Error::ServiceDown
+ end
+
+ it 'raises an error if populate method is not implemented' do
+ lambda {
+ Wadus.new.populate
+ }.must_raise Amiando::Error::NotImplemented
+ end
+end
60 test/fixtures/ApiKey/018fa0a2281ec9026bc122bc346366f6.yml
@@ -0,0 +1,60 @@
+--- !ruby/object:Typhoeus::Response
+app_connect_time: 5.6e-05
+body: "{\"success\":true}"
+code: 200
+connect_time: 5.6e-05
+curl_error_message: No error
+curl_return_code: 0
+effective_url: https://test.amiando.com/api/apiKey/179106?format=json&version=1&apikey=NPBLvySxfgggiCrRZ14Es66noIm0MqA8iUViNNtzMlZZlkvoCk
+first_header_line: HTTP/1.1 200 OK
+headers: |
+ HTTP/1.1 200 OK
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie: JSESSIONID=404E5CBE665988250ADCFA426029B28E.web01; Path=/; Secure
+ Set-Cookie: JSESSIONID=404E5CBE665988250ADCFA426029B28E.web01; Path=/
+ accept-charset: utf-8
+ P3P: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Access-Control-Allow-Origin: *
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ Set-Cookie: ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=UzJLMmpDVnRRYXVoWFVnVDoxMzIwMzk5NDEzNjExOjMzM2M3YzcxNmU2NDYxZGE0ZDJjY2JhNmEwNjBkYzM1; Expires=Wed, 19-Oct-2016 09:36:53 GMT; Path=/
+ Accept-Ranges: bytes
+ Cache-Control: max-age=7200
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Vary: User-Agent
+ Transfer-Encoding: chunked
+ Content-Type: application/json;charset=UTF-8
+
+
+headers_hash: !map:Typhoeus::NormalizedHeaderHash
+ Vary: User-Agent
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ P3p: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Transfer-Encoding: chunked
+ Access-Control-Allow-Origin: "*"
+ Content-Type: application/json;charset=UTF-8
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Accept-Ranges: bytes
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie:
+ - JSESSIONID=404E5CBE665988250ADCFA426029B28E.web01; Path=/; Secure
+ - JSESSIONID=404E5CBE665988250ADCFA426029B28E.web01; Path=/
+ - ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=UzJLMmpDVnRRYXVoWFVnVDoxMzIwMzk5NDEzNjExOjMzM2M3YzcxNmU2NDYxZGE0ZDJjY2JhNmEwNjBkYzM1; Expires=Wed, 19-Oct-2016 09:36:53 GMT; Path=/
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Cache-Control: max-age=7200
+ Accept-Charset: utf-8
+http_version:
+mock: false
+name_lookup_time: 5.5e-05
+pretransfer_time: 6.1e-05
+request: |-
+ :method => :post,
+ :url => https://test.amiando.com/api/apiKey/179106?format=json&version=1&apikey=NPBLvySxfgggiCrRZ14Es66noIm0MqA8iUViNNtzMlZZlkvoCk,
+ :params => {:enabled=>false},
+ :headers => {"User-Agent"=>"Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"}
+requested_http_method:
+requested_url:
+start_time:
+start_transfer_time: 0.192455
+status_message: OK
+time: 0.192488
60 test/fixtures/ApiKey/1f62195e173114773b3ec2613582ece9.yml
@@ -0,0 +1,60 @@
+--- !ruby/object:Typhoeus::Response
+app_connect_time: 4.7e-05
+body: "{\"id\":179107,\"success\":true,\"apiKey\":{\"id\":179107,\"enabled\":true,\"name\":\"wadus1\",\"identifier\":\"Tr1G23nJ4Gl4xzSh\",\"key\":\"OVXTWQ7P4CGAEMYfArYwVO1QpKSnoZH3f7RsbrkvgR1p0TuKUE\"}}"
+code: 201
+connect_time: 4.6e-05
+curl_error_message: No error
+curl_return_code: 0
+effective_url: https://test.amiando.com/api/apiKey/create?format=json&version=1
+first_header_line: HTTP/1.1 201 Created
+headers: |
+ HTTP/1.1 201 Created
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie: JSESSIONID=3381C7013BB4836465927E6B17D07F48.web01; Path=/; Secure
+ Set-Cookie: JSESSIONID=3381C7013BB4836465927E6B17D07F48.web01; Path=/
+ accept-charset: utf-8
+ P3P: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Access-Control-Allow-Origin: *
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ Set-Cookie: ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=TGxZM2dZY0ZOaldobU9kUToxMzIwMzk5NDE0MDc2Ojk2NzUxMmJmMzEwNGZkNDI2ZGQ2Yzc2YmJjYTEwZDI3; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Accept-Ranges: bytes
+ Cache-Control: max-age=7200
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Vary: User-Agent
+ Transfer-Encoding: chunked
+ Content-Type: application/json;charset=UTF-8
+
+
+headers_hash: !map:Typhoeus::NormalizedHeaderHash
+ Vary: User-Agent
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ P3p: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Transfer-Encoding: chunked
+ Access-Control-Allow-Origin: "*"
+ Content-Type: application/json;charset=UTF-8
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Accept-Ranges: bytes
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie:
+ - JSESSIONID=3381C7013BB4836465927E6B17D07F48.web01; Path=/; Secure
+ - JSESSIONID=3381C7013BB4836465927E6B17D07F48.web01; Path=/
+ - ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=TGxZM2dZY0ZOaldobU9kUToxMzIwMzk5NDE0MDc2Ojk2NzUxMmJmMzEwNGZkNDI2ZGQ2Yzc2YmJjYTEwZDI3; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Cache-Control: max-age=7200
+ Accept-Charset: utf-8
+http_version:
+mock: false
+name_lookup_time: 4.6e-05
+pretransfer_time: 5.2e-05
+request: |-
+ :method => :post,
+ :url => https://test.amiando.com/api/apiKey/create?format=json&version=1,
+ :params => {:name=>"wadus1"},
+ :headers => {"User-Agent"=>"Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"}
+requested_http_method:
+requested_url:
+start_time:
+start_transfer_time: 0.421535
+status_message: Created
+time: 0.42157
60 test/fixtures/ApiKey/97d82801a10696aaf385bf5873034b60.yml
@@ -0,0 +1,60 @@
+--- !ruby/object:Typhoeus::Response
+app_connect_time: 0.88925
+body: "{\"id\":179106,\"success\":true,\"apiKey\":{\"id\":179106,\"enabled\":true,\"name\":\"wadus\",\"identifier\":\"XLnig0xWKK1DhXcx\",\"key\":\"NPBLvySxfgggiCrRZ14Es66noIm0MqA8iUViNNtzMlZZlkvoCk\"}}"
+code: 201
+connect_time: 0.524973
+curl_error_message: No error
+curl_return_code: 0
+effective_url: https://test.amiando.com/api/apiKey/create?format=json&version=1
+first_header_line: HTTP/1.1 201 Created
+headers: |
+ HTTP/1.1 201 Created
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie: JSESSIONID=88AFC5E6F999B1BEFEF55BE5BC79AE3C.web01; Path=/; Secure
+ Set-Cookie: JSESSIONID=88AFC5E6F999B1BEFEF55BE5BC79AE3C.web01; Path=/
+ accept-charset: utf-8
+ P3P: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Access-Control-Allow-Origin: *
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ Set-Cookie: ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=VEVwdHRNMlpVRWtMUzRVNDoxMzIwMzk5NDEzNDMxOjM2OTJmNTg2YjgxMzU5MjNhZjdiMjJkODZlOWM3MmIy; Expires=Wed, 19-Oct-2016 09:36:53 GMT; Path=/
+ Accept-Ranges: bytes
+ Cache-Control: max-age=7200
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Vary: User-Agent
+ Transfer-Encoding: chunked
+ Content-Type: application/json;charset=UTF-8
+
+
+headers_hash: !map:Typhoeus::NormalizedHeaderHash
+ Vary: User-Agent
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ P3p: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Transfer-Encoding: chunked
+ Access-Control-Allow-Origin: "*"
+ Content-Type: application/json;charset=UTF-8
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Accept-Ranges: bytes
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie:
+ - JSESSIONID=88AFC5E6F999B1BEFEF55BE5BC79AE3C.web01; Path=/; Secure
+ - JSESSIONID=88AFC5E6F999B1BEFEF55BE5BC79AE3C.web01; Path=/
+ - ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=VEVwdHRNMlpVRWtMUzRVNDoxMzIwMzk5NDEzNDMxOjM2OTJmNTg2YjgxMzU5MjNhZjdiMjJkODZlOWM3MmIy; Expires=Wed, 19-Oct-2016 09:36:53 GMT; Path=/
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Cache-Control: max-age=7200
+ Accept-Charset: utf-8
+http_version:
+mock: false
+name_lookup_time: 0.409179
+pretransfer_time: 0.889252
+request: |-
+ :method => :post,
+ :url => https://test.amiando.com/api/apiKey/create?format=json&version=1,
+ :params => {:name=>"wadus"},
+ :headers => {"User-Agent"=>"Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"}
+requested_http_method:
+requested_url:
+start_time:
+start_transfer_time: 1.049302
+status_message: Created
+time: 1.049349
62 test/fixtures/ApiKey/a703d1fcec1576fa8cb4ca1c5f53f545.yml
@@ -0,0 +1,62 @@
+--- !ruby/object:Typhoeus::Response
+app_connect_time: 4.6e-05
+body: "{\"errors\":[\"com.amiando.api.rest.MissingParam.apikey\"],\"success\":false}"
+code: 400
+connect_time: 4.6e-05
+curl_error_message: No error
+curl_return_code: 0
+effective_url: https://test.amiando.com/api/apiKey/179106?format=json&version=1
+first_header_line: HTTP/1.1 400 Bad Request
+headers: |
+ HTTP/1.1 400 Bad Request
+ Date: Fri, 21 Oct 2011 09:33:58 GMT
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie: JSESSIONID=721A263C1C659AA8BA7A3BCE02043732.web01; Path=/; Secure
+ Set-Cookie: JSESSIONID=721A263C1C659AA8BA7A3BCE02043732.web01; Path=/
+ accept-charset: utf-8
+ P3P: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Access-Control-Allow-Origin: *
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ Set-Cookie: ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=UldoTEJ0cGZkaHFERXp0UToxMzIwMzk5NDE0MzUyOjYyMGQxNTFlOWVmNTc3NTcwZmI4ZjU3ODg2N2NhNjE4; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Accept-Ranges: bytes
+ Cache-Control: max-age=7200
+ Expires: Fri, 21 Oct 2011 11:33:58 GMT
+ Vary: User-Agent
+ Connection: close
+ Transfer-Encoding: chunked
+ Content-Type: application/json;charset=UTF-8
+
+
+headers_hash: !map:Typhoeus::NormalizedHeaderHash
+ Vary: User-Agent
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ P3p: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Transfer-Encoding: chunked
+ Access-Control-Allow-Origin: "*"
+ Content-Type: application/json;charset=UTF-8
+ Date: Fri, 21 Oct 2011 09:33:58 GMT
+ Accept-Ranges: bytes
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie:
+ - JSESSIONID=721A263C1C659AA8BA7A3BCE02043732.web01; Path=/; Secure
+ - JSESSIONID=721A263C1C659AA8BA7A3BCE02043732.web01; Path=/
+ - ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=UldoTEJ0cGZkaHFERXp0UToxMzIwMzk5NDE0MzUyOjYyMGQxNTFlOWVmNTc3NTcwZmI4ZjU3ODg2N2NhNjE4; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Expires: Fri, 21 Oct 2011 11:33:58 GMT
+ Cache-Control: max-age=7200
+ Accept-Charset: utf-8
+ Connection: close
+http_version:
+mock: false
+name_lookup_time: 4.6e-05
+pretransfer_time: 5.4e-05
+request: |-
+ :method => :post,
+ :url => https://test.amiando.com/api/apiKey/179106?format=json&version=1,
+ :params => {:enabled=>false},
+ :headers => {"User-Agent"=>"Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"}
+requested_http_method:
+requested_url:
+start_time:
+start_transfer_time: 0.140808
+status_message: Bad Request
+time: 0.140852
60 test/fixtures/ApiKey/fa883db60c0390aa8948098d32497015.yml
@@ -0,0 +1,60 @@
+--- !ruby/object:Typhoeus::Response
+app_connect_time: 5.4e-05
+body: "{\"errors\":[\"com.amiando.api.rest.NotAuthorized\"],\"success\":false}"
+code: 403
+connect_time: 5.4e-05
+curl_error_message: No error
+curl_return_code: 0
+effective_url: https://test.amiando.com/api/apiKey/179107?format=json&version=1&apikey=NPBLvySxfgggiCrRZ14Es66noIm0MqA8iUViNNtzMlZZlkvoCk
+first_header_line: HTTP/1.1 403 Forbidden
+headers: |
+ HTTP/1.1 403 Forbidden
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie: JSESSIONID=2D43C51CCF9E5BF64C6B4E777761B7C3.web01; Path=/; Secure
+ Set-Cookie: JSESSIONID=2D43C51CCF9E5BF64C6B4E777761B7C3.web01; Path=/
+ accept-charset: utf-8
+ P3P: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Access-Control-Allow-Origin: *
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ Set-Cookie: ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=NnNoWnZLNUxJdmp2YnRmMjoxMzIwMzk5NDE0MTk5OjIwOWIxMDc3OTUyYzhmNDk0ZmZhNTdlMmY3MDA4NGRk; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Accept-Ranges: bytes
+ Cache-Control: max-age=7200
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Vary: User-Agent
+ Transfer-Encoding: chunked
+ Content-Type: application/json;charset=UTF-8
+
+
+headers_hash: !map:Typhoeus::NormalizedHeaderHash
+ Vary: User-Agent
+ Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
+ P3p: policyref="/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD CON OUR IND UNI PUR COM CNT"
+ Transfer-Encoding: chunked
+ Access-Control-Allow-Origin: "*"
+ Content-Type: application/json;charset=UTF-8
+ Date: Fri, 21 Oct 2011 09:33:57 GMT
+ Accept-Ranges: bytes
+ Server: Apache/2.0.55 (Red Hat)
+ Set-Cookie:
+ - JSESSIONID=2D43C51CCF9E5BF64C6B4E777761B7C3.web01; Path=/; Secure
+ - JSESSIONID=2D43C51CCF9E5BF64C6B4E777761B7C3.web01; Path=/
+ - ANONYMOUS_HASHED_REMEMBER_ME_COOKIE_KEY=NnNoWnZLNUxJdmp2YnRmMjoxMzIwMzk5NDE0MTk5OjIwOWIxMDc3OTUyYzhmNDk0ZmZhNTdlMmY3MDA4NGRk; Expires=Wed, 19-Oct-2016 09:36:54 GMT; Path=/
+ Expires: Fri, 21 Oct 2011 11:33:57 GMT
+ Cache-Control: max-age=7200
+ Accept-Charset: utf-8
+http_version:
+mock: false
+name_lookup_time: 5.4e-05
+pretransfer_time: 6.1e-05
+request: |-
+ :method => :post,
+ :url => https://test.amiando.com/api/apiKey/179107?format=json&version=1&apikey=NPBLvySxfgggiCrRZ14Es66noIm0MqA8iUViNNtzMlZZlkvoCk,
+ :params => {:enabled=>false},
+ :headers => {"User-Agent"=>"Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"}
+requested_http_method:
+requested_url:
+start_time:
+start_transfer_time: 0.114879
+status_message: Forbidden
+time: 0.114911
58 test/support/hydra_cache.rb
@@ -0,0 +1,58 @@
+module HydraCache
+ extend self
+
+ attr_accessor :prefix
+
+ def setter(request)
+ set_cache request
+ end
+
+ def getter(request)
+ if response = get_cache(request)
+ response.request = request
+ else
+ request.cache_timeout = 1
+ end
+
+ response
+ end
+
+ def get_cache(request)
+ filename = cache_filename(request)
+
+ if File.exist?(filename)
+ YAML.load_file(filename)
+ else
+ false
+ end
+ end
+
+ def set_cache(request)
+ filename = cache_filename(request)
+ response = request.response
+ response.request = request.inspect
+
+ FileUtils.mkdir_p(File.dirname(filename))
+ File.open(filename, 'w') do |f|
+ f << response.to_yaml
+ end
+ end
+
+ def cache_filename(request)
+ uri = URI.parse(request.url)
+ uri.query = uri.query.split('&').sort.join('&')
+
+ body = request.body.to_s.split(/\n/).map do |line|
+ Digest::SHA1.hexdigest(line)
+ end.sort.join
+
+ params = request.params.map do |k,v|
+ Digest::SHA1.hexdigest("#{k},#{v}")
+ end.sort.join
+
+ digest = Digest::MD5.hexdigest("#{uri}|#{body}|#{params}")
+
+ File.join(File.dirname(__FILE__),'..','fixtures', prefix.to_s.gsub(/\W+/,'_'), "#{digest}.yml")
+ end
+
+end
16 test/test_helper.rb
@@ -0,0 +1,16 @@
+require 'bundler/setup'
+require 'minitest/autorun'
+require 'minitest/spec'
+require 'webmock/minitest'
+require 'amiando'
+begin
+ require 'ruby-debug'
+rescue LoadError
+end
+
+require 'support/hydra_cache'
+
+Amiando.hydra.cache_setter &HydraCache.method(:setter)
+Amiando.hydra.cache_getter &HydraCache.method(:getter)
+
+WebMock.allow_net_connect!
Please sign in to comment.
Something went wrong with that request. Please try again.