3scale_api 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0760d1fb028c4855c5477a58cc1d4f3ba1bddff3
4
- data.tar.gz: 41562a512efbbd707b4406177eca447204d0a3cb
3
+ metadata.gz: 2d0223786cde69d7a70a886afb91b6f3c3919659
4
+ data.tar.gz: 052c5d209b2d50fed163f952cfce07d9b958d193
5
5
  SHA512:
6
- metadata.gz: 72cdb9d77ed42d42ba8b92884386da152281c4c092354cdc1d390ffdad32ca74077634d5c4773d6bc5e4ce7179d42e1bbc57ca4cf223bac9d34d80372050013b
7
- data.tar.gz: 6cf1433e7e57660c616c29e9771c839f0c406036e7cecb9f305c493117e201b3cc2f79943ae18263b1f4df76953202304e6f7688af797d1b5f4b73d264cb60e5
6
+ metadata.gz: e698537127b7dacd729e4f8e1ea1b59042e6092f0d6dd73e1abf03c9d8d692797ddcc78d1c174b1c3be68e701975b0c5cbd4cf618dc9d6765ec5614f50a9b2d0
7
+ data.tar.gz: 15003f3b05077cf23fa2a039db8e11a4b56075763042c63363063043f845bc813f1eaceebbae09305e445cdc562d30689d69efcbdcffaef382e141dea7df4bcb
data/Gemfile CHANGED
@@ -1,11 +1,11 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- gem 'faraday'
4
- gem 'json'
5
- gem 'nokogiri'
3
+ gem "faraday"
4
+ gem "json"
5
+ gem "nokogiri"
6
6
  gemspec
7
7
  gem "bundler", "~> 1.6"
8
8
  gem "rake", "~> 10.0"
9
9
  gem "webmock"
10
- gem "vcr"
11
- gem 'rspec'
10
+ gem "rspec"
11
+ gem "pry"
@@ -5,11 +5,11 @@ require 'nokogiri'
5
5
  module Threescale
6
6
  class API
7
7
  attr_accessor :provider_key, :url, :path, :conn
8
- def initialize(provider_key = nil, debug = false)
8
+ def initialize(provider_key = nil)
9
9
  if ENV['THREESCALE_URL']
10
10
  @url = ENV['THREESCALE_URL']
11
11
  else
12
- raise Error, "Please set your 3 Scale URL as an environmental variable THREESCALE_URL"
12
+ raise ("Please set your 3 Scale URL as an environmental variable THREESCALE_URL")
13
13
  end
14
14
  if not provider_key
15
15
  if ENV['THREESCALE_PROVIDER_KEY']
@@ -25,9 +25,38 @@ module Threescale
25
25
  end
26
26
  end
27
27
 
28
+ def create_application(account_id, plan_id, name, description = nil)
29
+ response = conn.post "/admin/api/accounts/#{account_id}/applications.xml", {
30
+ :provider_key => @provider_key ,
31
+ :name => name,
32
+ :description => description,
33
+ :plan_id => plan_id}
34
+ return false if response.status != 201
35
+ xml = Nokogiri::XML(response.body)
36
+ result = {
37
+ :app_id => xml.css("application application_id").text ,
38
+ :application_id => xml.css("application id").text,
39
+ :keys => [xml.css("application keys key").text]
40
+ }
41
+ end
42
+
43
+ def delete_application_key(account_id, application_id, key)
44
+ response = @conn.delete "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys/#{key}.xml", {
45
+ :provider_key => @provider_key }
46
+ response.status == 200
47
+ end
48
+
49
+ def generate_application_key(account_id, application_id)
50
+ new_key = SecureRandom.hex(16)
51
+ response = conn.post "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys.xml", {
52
+ :provider_key => @provider_key ,
53
+ :key => new_key }
54
+ response.status == 201
55
+ end
56
+
28
57
  def get_application_keys(account_id, application_id)
29
58
  response = @conn.get "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys.xml", {
30
- :provider_key => @provider_key, }
59
+ :provider_key => @provider_key, }
31
60
  p response.status
32
61
  return [] if response.status != 200
33
62
  xml = Nokogiri::XML(response.body)
@@ -36,6 +65,7 @@ module Threescale
36
65
  key.text
37
66
  end
38
67
  end
68
+
39
69
  def get_application_list(account_id)
40
70
  results = Array.new
41
71
  response = @conn.get "/admin/api/accounts/#{account_id}/applications.xml", {:provider_key => @provider_key, }
@@ -47,40 +77,14 @@ module Threescale
47
77
  key.text
48
78
  end
49
79
  results.push(
50
- {:keys => keys,
51
- :id => application.css('id').text,
52
- :name => application.css('name').text,
53
- :application_id => application.css('application_id').text,
54
- :plan_type => application.css('plan name').text})
80
+ {:keys => keys,
81
+ :id => application.css('id').text,
82
+ :name => application.css('name').text,
83
+ :application_id => application.css('application_id').text,
84
+ :plan_type => application.css('plan name').text})
55
85
  end
56
86
  results
57
87
  end
58
- def delete_application_key(account_id, application_id, key)
59
- response = @conn.delete "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys/#{key}.xml", {
60
- :provider_key => @provider_key }
61
- response.status == 200
62
- end
63
88
 
64
- def generate_application_key(account_id, application_id)
65
- new_key = SecureRandom.hex(16)
66
- response = conn.post "/admin/api/accounts/#{account_id}/applications/#{application_id}/keys.xml", {
67
- :provider_key => @provider_key ,
68
- :key => new_key }
69
- response.status == 201
70
- end
71
- def create_application(account_id, plan_id, name, description = nil)
72
- response = conn.post "/admin/api/accounts/#{account_id}/applications.xml", {
73
- :provider_key => @provider_key ,
74
- :name => name,
75
- :description => description,
76
- :plan_id => plan_id}
77
- return false if response.status != 201
78
- xml = Nokogiri::XML(response.body)
79
- result = {
80
- :app_id => xml.css("application application_id").text ,
81
- :application_id => xml.css("application id").text,
82
- :keys => [xml.css("application keys key").text]
83
- }
84
- end
85
89
  end
86
90
  end
@@ -1,3 +1,3 @@
1
1
  module Threescale
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -5,28 +5,120 @@ describe "3scaleApi" do
5
5
  ENV["THREESCALE_URL"] = nil
6
6
  ENV["THREESCALE_PROVIDER_KEY"] = nil
7
7
  end
8
- it "should raise an error when there is no env variables and you instantiate" do
9
- lambda do
10
- Threescale::API.new
11
- end.should raise_error
8
+ describe "Instantiate 3scale object" do
9
+ it "should raise an error when there is no env variables and you instantiate" do
10
+ lambda do
11
+ Threescale::API.new
12
+ end.should raise_error
13
+ end
14
+ it "should raise an error when there is no env variables and you instantiate obj" do
15
+ lambda do
16
+ ENV["THREESCALE_URL"] = "test-url"
17
+ Threescale::API.new
18
+ end.should raise_error
19
+ end
20
+ it "should not raise an error when there is one env variables and you instantiate" do
21
+ lambda do
22
+ ENV["THREESCALE_URL"] = "test-url"
23
+ Threescale::API.new "provider-key"
24
+ end.should_not raise_error
25
+ end
26
+ it "should not raise an error when both env variables and you instantiate" do
27
+ lambda do
28
+ ENV["THREESCALE_URL"] = "http://test-url"
29
+ ENV["THREESCALE_PROVIDER_KEY"] = "provider-key"
30
+ Threescale::API.new
31
+ end.should_not raise_error
32
+ end
12
33
  end
13
- it "should raise an error when there is no env variables and you instantiate obj" do
14
- lambda do
15
- ENV["THREESCALE_URL"] = "test-url"
16
- Threescale::API.new
17
- end.should raise_error
18
- end
19
- it "should not raise an error when there is one env variables and you instantiate" do
20
- lambda do
21
- ENV["THREESCALE_URL"] = "test-url"
22
- Threescale::API.new "provider-key"
23
- end.should_not raise_error
24
- end
25
- it "should not raise an error when both env variables and you instantiate" do
26
- lambda do
27
- ENV["THREESCALE_URL"] = "test-url"
28
- ENV["THREESCALE_PROVIDER_KEY"] = "provider-key"
29
- Threescale::API.new
30
- end.should_not raise_error
34
+ describe "API methods" do
35
+ before(:all) do
36
+ ENV["THREESCALE_URL"] = "http://test-url.test"
37
+ @threescale = Threescale::API.new 'provider-key'
38
+ end
39
+ describe "get_application_keys" do
40
+ it "should call /admin/api/accounts/{account_id}/applications/{application_id}/keys.xml" do
41
+ stub_request(
42
+ :get,
43
+ "http://test-url.test/admin/api/accounts/account-id/applications/application-id/keys.xml?provider_key=provider-key").
44
+ with(
45
+ :headers => {
46
+ 'Accept'=>'*/*',
47
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
48
+ 'User-Agent'=>'Faraday v0.9.1'}).
49
+ to_return(:status => 200, :body => "", :headers => {})
50
+ @threescale.get_application_keys 'account-id', 'application-id'
51
+ end
52
+ end
53
+ describe "get_application_list" do
54
+ it "should call /admin/api/accounts/{account_id}/applications.xml" do
55
+ stub_request(
56
+ :get,
57
+ "http://test-url.test/admin/api/accounts/account-id/applications.xml?provider_key=provider-key").
58
+ with(
59
+ :headers => {
60
+ 'Accept'=>'*/*',
61
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
62
+ 'User-Agent'=>'Faraday v0.9.1'}).
63
+ to_return(:status => 200, :body => "", :headers => {})
64
+
65
+ @threescale.get_application_list 'account-id'
66
+ end
67
+ end
68
+ describe "delete_application_key" do
69
+ it "should call /admin/api/accounts/{account_id}/applications/{application_id}/keys/{key}.xml" do
70
+ stub_request(
71
+ :delete,
72
+ "http://test-url.test/admin/api/accounts/account-id/applications/application-id/keys/key.xml?provider_key=provider-key").
73
+ with(
74
+ :headers => {
75
+ 'Accept'=>'*/*',
76
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
77
+ 'User-Agent'=>'Faraday v0.9.1'}).
78
+ to_return(:status => 200, :body => "", :headers => {})
79
+
80
+ @threescale.delete_application_key 'account-id', 'application-id', 'key'
81
+ end
82
+ end
83
+ describe "generate_application_key" do
84
+ it "should call /admin/api/accounts/{account_id}/applications/{application_id}/keys.xml" do
85
+ stub_request(
86
+ :post,
87
+ "http://test-url.test/admin/api/accounts/account-id/applications/application-id/keys.xml").
88
+ with(
89
+ :body => {
90
+ "key"=>/[0-9a-f]{32}/,
91
+ "provider_key"=>"provider-key"},
92
+ :headers => {
93
+ 'Accept'=>'*/*',
94
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
95
+ 'Content-Type'=>'application/x-www-form-urlencoded',
96
+ 'User-Agent'=>'Faraday v0.9.1'})
97
+
98
+
99
+ @threescale.generate_application_key 'account-id', 'application-id'
100
+ end
101
+ end
102
+ describe "create_application" do
103
+ it "should call /admin/api/accounts/{account_id}/applications.xml" do
104
+ stub_request(
105
+ :post,
106
+ "http://test-url.test/admin/api/accounts/account-id/applications.xml").
107
+ with(
108
+ :body => {
109
+ "description"=>"description",
110
+ "name"=>"name",
111
+ "plan_id"=>"plan-id",
112
+ "provider_key"=>"provider-key"},
113
+ :headers => {
114
+ 'Accept'=>'*/*',
115
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
116
+ 'Content-Type'=>'application/x-www-form-urlencoded',
117
+ 'User-Agent'=>'Faraday v0.9.1'}).
118
+ to_return(:status => 200, :body => "", :headers => {})
119
+
120
+ @threescale.create_application 'account-id', 'plan-id', 'name', 'description'
121
+ end
122
+ end
31
123
  end
32
124
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
- # our gem
3
+ require 'webmock/rspec'
4
+ require 'webmock/rspec/matchers'
5
+ require 'webmock/rspec/matchers/request_pattern_matcher'
6
+ require 'webmock/rspec/matchers/webmock_matcher'
4
7
  require '3scale_api'
5
8
 
6
- RSpec.configure do |config|
7
-
8
- end
9
+ WebMock.disable_net_connect!(allow_localhost: true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 3scale_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robbie Holmes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem is to be used to interact with 3Scale's API.
14
14
  email: