cdn77 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0bfd78dd2a34718e5ff5b883edc2fc40ae64aa49
4
+ data.tar.gz: c3b54b15c9afa921baf64d440fb9c1778f8f46e8
5
+ SHA512:
6
+ metadata.gz: 8cf0f6146103bdab9715d723769f6ac9ff6d6d38854a67f37e2d266b96962b047a60bfb0a2795f59e374f5b21e88b4f89e55a9c7c71fa31152c1be0b4f2f75c0
7
+ data.tar.gz: 6bb8ccc37bc6ec4908d41ab4bd34b534c7571521dc7fca10eda3bedf0e5fbbdd89654cb60ca73da352eabcee71b093339ff7bdfd0e0b54b589bf938e452adb38
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+
8
+ gemfile:
9
+ - Gemfile
10
+
11
+ script: "bundle exec rspec"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ТоМесто
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,83 @@
1
+ # cdn77
2
+
3
+ Wrapper for CDN77 API that allows you to do a wide range of commands and tasks from an external script or server.
4
+
5
+ ## Installation
6
+
7
+ This gem works with Rails 3.2 onwards. To make it available just add following lines into your Gemfile:
8
+
9
+ ```ruby
10
+ gem "cdn77"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ gem install xxx
23
+ ```
24
+ And require in irb:
25
+
26
+ ```ruby
27
+ require "cdn77"
28
+ ```
29
+
30
+ ## Using
31
+
32
+ To perform any requests use simple wrapper:
33
+
34
+ ```ruby
35
+ config = Cdn77::Configuration.new(:login => "someone@example.com", :password => "secret")
36
+ cdn = Cdn77.cdn(config)
37
+ cdn.get("account", "details")
38
+ # {:status => "ok", :description => "Request was successful.", :account => ...}
39
+ ```
40
+
41
+ If you want to setup configuration only once you should take a look on `Cdn77.configure`. For example, it is a good idea to create initializer with following code in Rails:
42
+
43
+ ```ruby
44
+ # config/initializers/cdn77.rb
45
+
46
+ Cdn77.configure do |config|
47
+ config.login = "someone@example.com"
48
+ config.password = "secret"
49
+ end
50
+ ```
51
+
52
+ Now just call API's method in any part of your application:
53
+
54
+ ```ruby
55
+ Cdn77.cdn.get("account", "details")
56
+ # {:status => "ok", :description => "Request was successful.", :account => ...}
57
+ ```
58
+
59
+ Of course, you can override global configuration if needed. For example:
60
+
61
+ ```ruby
62
+ Cdn77.configure do |config|
63
+ config.login = "someone@example.com"
64
+ config.password = "secret"
65
+ end
66
+ Cdn77.cdn.get("account", "details", :login => "someoneelse@example.com", :password => 'elsesecret')
67
+ ```
68
+
69
+ To make requests using HTTP POST call `post` method:
70
+
71
+ ```ruby
72
+ Cdn77.cdn.post("account", "edit", :full_name => "Martin Eden")
73
+ ```
74
+
75
+ And have fun!
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it ( https://github.com/ToMesto/cdn77/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cdn77/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cdn77"
8
+ spec.version = Cdn77::VERSION
9
+ spec.authors = ["Sergey Tsvetkov"]
10
+ spec.email = ["sergey.a.tsvetkov@gmail.com"]
11
+ spec.summary = %q{Wrapper for CDN77 API}
12
+ spec.description = %q{CDN77 API allows you to do a wide range of commands and tasks from an external script or server.}
13
+ spec.homepage = "https://github.com/ToMesto/cdn77"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "webmock", "~> 1.20"
25
+ end
@@ -0,0 +1,109 @@
1
+ require "cdn77/version"
2
+ require "uri"
3
+ require "net/http"
4
+ require "net/https"
5
+ require "json"
6
+
7
+ module Cdn77
8
+ autoload :Account, "cdn77/account"
9
+ autoload :Configuration, "cdn77/configuration"
10
+
11
+ def self.cdn(configuration = nil)
12
+ CDN.new(configuration)
13
+ end
14
+
15
+ class MethodCallError < StandardError; end;
16
+
17
+ class CDN
18
+ include Cdn77::Account
19
+
20
+ def initialize(configuration = nil)
21
+ @configuration = configuration
22
+ end
23
+
24
+ def configuration
25
+ @configuration || Cdn77.configuration
26
+ end
27
+
28
+ def login
29
+ configuration.login
30
+ end
31
+
32
+ def password
33
+ configuration.password
34
+ end
35
+
36
+ def headers
37
+ {
38
+ "Accept" => "application/json"
39
+ }
40
+ end
41
+
42
+ def post(scope, method, params = {}, &block)
43
+ raise_if_invalid(scope, method)
44
+ uri = URI(url(scope, method))
45
+ http = Net::HTTP.new(uri.host,uri.port)
46
+ http.use_ssl = true
47
+ params = URI.encode_www_form(with_creditinals(params))
48
+ handle_response(http.post(uri.request_uri, params, headers), &block)
49
+ end
50
+
51
+ def get(scope, method, params = {}, &block)
52
+ raise_if_invalid(scope, method)
53
+ uri = URI(url(scope, method, with_creditinals(params)))
54
+ http = Net::HTTP.new(uri.host,uri.port)
55
+ http.use_ssl = true
56
+ handle_response(http.get(uri.request_uri, headers), &block)
57
+ end
58
+
59
+ def url(scope, method, params = {})
60
+ raise_if_invalid(scope, method)
61
+ url = configuration.endpoint + "/#{scope}/#{method}"
62
+ url += "?#{URI.encode_www_form(params)}" if params && params.any?
63
+ url
64
+ end
65
+
66
+ private
67
+
68
+ def handle_response(response)
69
+ raise MethodCallError, response.body unless response.is_a?(Net::HTTPSuccess)
70
+ body = response.body
71
+ raise MethodCallError, "Response could not be empty" if body.nil? || body.empty?
72
+ json = JSON.parse(body, :symbolize_names => true)
73
+ if json[:status] == "ok"
74
+ block_given? ? yield(json) : json
75
+ else
76
+ message = [ json[:description].to_s ]
77
+ message += json[:errors].map { |field, description| "#{field}: #{description}" } if json[:errors]
78
+ raise MethodCallError, message.join(". ")
79
+ end
80
+ end
81
+
82
+ def with_creditinals(params = {})
83
+ params ||= {}
84
+ params[:passwd] = params.delete(:password) if params[:password] && !params[:passwd]
85
+ params[:login] ||= login
86
+ params[:passwd] ||= password
87
+ params
88
+ end
89
+
90
+ def raise_if_invalid(scope, method)
91
+ raise ArgumentError, "Scope could not be empty" if scope.nil? || scope.empty?
92
+ raise ArgumentError, "Method could not be empty" if method.nil? || method.empty?
93
+ raise ArgumentError, "Configuration endpoint was not specified" if configuration.nil? || configuration.endpoint.nil? || configuration.endpoint.empty?
94
+ end
95
+ end
96
+
97
+ class << self
98
+ attr_accessor :configuration
99
+ end
100
+
101
+ def self.reset_configuration
102
+ self.configuration = nil
103
+ end
104
+
105
+ def self.configure
106
+ self.configuration ||= Configuration.new
107
+ yield(configuration)
108
+ end
109
+ end
@@ -0,0 +1,6 @@
1
+ module Cdn77
2
+ module Account
3
+ def details
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module Cdn77
2
+ class Configuration
3
+ DEFAULT_ENDPOINT = "https://client.cdn77.com/api/v2.0"
4
+
5
+ attr_accessor :login
6
+ attr_accessor :password
7
+ attr_accessor :endpoint
8
+
9
+ def initialize(params = nil)
10
+ params ||= {}
11
+ self.login = params[:login]
12
+ self.password = params[:password]
13
+ self.endpoint = params[:endpoint] || DEFAULT_ENDPOINT
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Cdn77
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cdn77::Account do
4
+ end
@@ -0,0 +1,135 @@
1
+ require "spec_helper"
2
+
3
+ shared_examples "an url builder" do |method_name|
4
+ it "should raise ArgumentError when scope is nil" do
5
+ expect{ cdn.send(method_name, nil, "details") }.to raise_error(ArgumentError, "Scope could not be empty")
6
+ end
7
+
8
+ it "should raise ArgumentError when method is nil" do
9
+ expect{ cdn.send(method_name, "account", nil) }.to raise_error(ArgumentError, "Method could not be empty")
10
+ end
11
+
12
+ it "should raise ArgumentError when configuration in not specified" do
13
+ allow(Cdn77).to receive(:configuration).and_return(nil)
14
+ expect{ cdn.send(method_name, "account", "details") }.to raise_error(ArgumentError, "Configuration endpoint was not specified")
15
+ end
16
+ end
17
+
18
+ shared_examples "a request sender" do |method, url|
19
+ let (:successful_response_body) do
20
+ {
21
+ :status => "ok",
22
+ :description => "Request was successful."
23
+ }
24
+ end
25
+
26
+ let (:wrong_credentials_response_body) do
27
+ {
28
+ :status => "error",
29
+ :description => "Authentication failed. Please, login again or contact our support."
30
+ }
31
+ end
32
+
33
+ let (:wrong_parameters_response_body) do
34
+ {
35
+ :status => "error",
36
+ :description => "Request was not successful.",
37
+ :errors => {
38
+ :email => "email is required.",
39
+ :password => "password is required."
40
+ }
41
+ }
42
+ end
43
+
44
+ it "should raise MethodCallError when response code is not 200 OK" do
45
+ stub_request(method, url).to_return(:status => 500, :body => successful_response_body.to_json)
46
+ expect{ cdn.send(method, "account", "details") }.to raise_error(Cdn77::MethodCallError)
47
+ end
48
+
49
+ it "should raise MethodCallError when response body is empty" do
50
+ stub_request(method, url).to_return(:status => 500, :body => '')
51
+ expect{ cdn.send(method, "account", "details") }.to raise_error(Cdn77::MethodCallError)
52
+ end
53
+
54
+ it "should raise MethodCallError when response status is not ok" do
55
+ stub_request(method, url).to_return(:status => 200, :body => wrong_parameters_response_body.to_json)
56
+ expect{ cdn.send(method, "account", "details") }.to raise_error(Cdn77::MethodCallError)
57
+ end
58
+
59
+ it "should raise MethodCallError when wrong creditinals reported" do
60
+ stub_request(method, url).to_return(:status => 200, :body => wrong_credentials_response_body.to_json)
61
+ expect{ cdn.send(method, "account", "details") }.to raise_error(Cdn77::MethodCallError, wrong_credentials_response_body[:description])
62
+ end
63
+
64
+ it "should return response body as a hash if no block given" do
65
+ stub_request(method, url).to_return(:status => 200, :body => successful_response_body.to_json)
66
+ expect(cdn.send(method, "account", "details")).to eq(successful_response_body)
67
+ end
68
+
69
+ it "should pass response body as a hash into given block" do
70
+ stub_request(method, url).to_return(:status => 200, :body => successful_response_body.to_json)
71
+ expect{ |block| cdn.send(method, "account", "details", &block) }.to yield_with_args(successful_response_body)
72
+ end
73
+
74
+ it "should send request with login and password in parameters" do
75
+ stub_request(method, url).to_return(:status => 200, :body => successful_response_body.to_json)
76
+ cdn.send(method, "account", "details")
77
+ expect(WebMock).to have_requested(method, url)
78
+ end
79
+ end
80
+
81
+ describe Cdn77::CDN do
82
+ before do
83
+ Cdn77.configure do |config|
84
+ config.login = "ivan@examle.com"
85
+ config.password = "secret"
86
+ end
87
+ end
88
+
89
+ let (:cdn) { Cdn77.cdn }
90
+
91
+ describe "#configuration" do
92
+ it "should return global configuration" do
93
+ expect(Cdn77::configuration.login).to eq("ivan@examle.com")
94
+ expect(Cdn77::configuration.password).to eq("secret")
95
+ end
96
+ end
97
+
98
+ describe "#cdn" do
99
+ it "should use specific configuration if it was provided" do
100
+ config = Cdn77::Configuration.new(:login => "vasya@example.com", :password => "secretsecret")
101
+ cdn = Cdn77.cdn(config)
102
+ expect(cdn.configuration).to eq(config)
103
+ expect(cdn.login).to eq("vasya@example.com")
104
+ expect(cdn.password).to eq("secretsecret")
105
+ end
106
+ end
107
+
108
+ describe "#url" do
109
+ it { is_expected.to respond_to(:url) }
110
+
111
+ it_behaves_like "an url builder", :url
112
+
113
+ it "should return correct url with scope and method" do
114
+ expect(cdn.url("account", "details")).to eq("https://client.cdn77.com/api/v2.0/account/details")
115
+ end
116
+
117
+ it "should add given params to url" do
118
+ expect(cdn.url("account", "details", :test => "test")).to eq("https://client.cdn77.com/api/v2.0/account/details?test=test")
119
+ end
120
+ end
121
+
122
+ describe "#post" do
123
+ it { is_expected.to respond_to(:post) }
124
+
125
+ it_behaves_like "an url builder", :post
126
+ it_behaves_like "a request sender", :post, "https://client.cdn77.com/api/v2.0/account/details"
127
+ end
128
+
129
+ describe "#get" do
130
+ it { is_expected.to respond_to(:get) }
131
+
132
+ it_behaves_like "an url builder", :get
133
+ it_behaves_like "a request sender", :get, "https://client.cdn77.com/api/v2.0/account/details?login=ivan@examle.com&passwd=secret"
134
+ end
135
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Cdn77::Configuration do
4
+ it { is_expected.to respond_to(:login, :password, :endpoint) }
5
+
6
+ it "should return default endpoint if another was not given" do
7
+ expect(Cdn77::Configuration.new.endpoint).to eq(Cdn77::Configuration::DEFAULT_ENDPOINT)
8
+ end
9
+
10
+ it "should return provided endpoint" do
11
+ expect(Cdn77::Configuration.new(:endpoint => "https://exmaple.com").endpoint).to eq("https://exmaple.com")
12
+ end
13
+
14
+ it "should return provided login and password" do
15
+ config = Cdn77::Configuration.new(:login => "ivan@example.com", :password => "secret")
16
+ expect(config.login).to eq("ivan@example.com")
17
+ expect(config.password).to eq("secret")
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+
3
+ Bundler.setup
4
+
5
+ require 'webmock/rspec'
6
+ require 'cdn77'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_doubled_constant_names = true
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cdn77
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Tsvetkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.20'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.20'
69
+ description: CDN77 API allows you to do a wide range of commands and tasks from an
70
+ external script or server.
71
+ email:
72
+ - sergey.a.tsvetkov@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - cdn77.gemspec
85
+ - lib/cdn77.rb
86
+ - lib/cdn77/account.rb
87
+ - lib/cdn77/configuration.rb
88
+ - lib/cdn77/version.rb
89
+ - spec/account_spec.rb
90
+ - spec/cdn_spec.rb
91
+ - spec/configuration_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/ToMesto/cdn77
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Wrapper for CDN77 API
117
+ test_files:
118
+ - spec/account_spec.rb
119
+ - spec/cdn_spec.rb
120
+ - spec/configuration_spec.rb
121
+ - spec/spec_helper.rb