ok_linker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb9cf79a26e604c19c75c3409fb9adfbad3d2934
4
+ data.tar.gz: e967b848f07355fc4e297c9f7af5f09947427e76
5
+ SHA512:
6
+ metadata.gz: 241d6903dcacf3c14a02cd8f1801a49ef70b440ae332cb834e37b530b40f902c1026efc24d29840e66f63470d601d7339a178affee46f8e74013b9dee1be3b80
7
+ data.tar.gz: 66ec91ff6d0024d649ca1a524c27e836615f02ca0e100c99195b3d515650b14da7a952b0a7358cec9bcf95b7acb95ea16ba1e8d9180d5648cdb69013db2bfb54
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .idea/
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Ilya Bodrov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # OkLinker
2
+ [![Dependency Status](https://gemnasium.com/bodrovis/ok_linker.svg)](https://gemnasium.com/bodrovis/ok_linker)
3
+
4
+ Ruby gem to work with Odnoklassniki's URL shortening service [okey.link](http://okey.link).
5
+
6
+ ## Installation
7
+
8
+ Via RubyGems:
9
+
10
+ $ gem install ok_linker
11
+
12
+ Or add it to your `Gemfile`:
13
+
14
+ gem 'ok_linker'
15
+
16
+ ## Usage
17
+
18
+ ### Prerequisites
19
+
20
+ First of all, you will require access to [okey.link](https://okey.link). Fill in this [submission form](https://okey.link/cabinet/registration)
21
+ to gain access.
22
+
23
+ After logging in, copy your API access token from the [API section](https://okey.link/cabinet/api). This token
24
+ has unlimited lifetime and has to be stored securely.
25
+
26
+ ### Configuration
27
+
28
+ OkLinker does not require much configuration. The only thing that you need to provide is your access token.
29
+ You can either use an initializer (for example, `ok_linker.rb`):
30
+
31
+ OkLinker.configure do |c|
32
+ c.access_token = 'Your token'
33
+ end
34
+
35
+ or provide it when instantiating a new client:
36
+
37
+ client = OkLinker::Client.new(access_token: 'Your token')
38
+
39
+ ### Calling methods
40
+
41
+ You can read more about available methods [here](https://okey.link/cabinet/api).
42
+
43
+ OkLinker provides the following conventional methods:
44
+
45
+ ```ruby
46
+ shorten(url) # Shorten a given URL
47
+
48
+ hide(url) # Hide a URL that was previously shorten
49
+
50
+ clicks(url) # Get clicks statistics for the given URL
51
+
52
+ count_urls(params) # Get a list of shortened URLS.
53
+ # Allowed options:
54
+ # * :epp - integer, optional. Elements per page. Default is 20.
55
+ # * :page - integer, optional. Page to fetch. Default is 1.
56
+ ```
57
+
58
+ Also, you can call raw methods:
59
+
60
+ ```ruby
61
+ get(method, params = {}, &block)
62
+
63
+ post(method, params = {}, &block)
64
+ ```
65
+
66
+ For example,
67
+
68
+ client.get('get-urls', page: 2)
69
+
70
+ These methods always return JSON or one of the errors listed [here](https://github.com/bodrovis/ok_linker/blob/master/lib/ok_linker/error.rb#L18).
71
+
72
+ ## Todo
73
+
74
+ * Write tests
75
+
76
+ ## License
77
+
78
+ Licensed under the [MIT License](https://github.com/bodrovis/ok_linker/blob/master/LICENSE).
79
+
80
+ Copyright (c) 2015 [Ilya Bodrov](http://radiant-wind.com)
data/lib/ok_linker.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'pry'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'multi_json'
5
+
6
+ require_relative 'ok_linker/connection'
7
+ require_relative 'ok_linker/request'
8
+ require_relative 'ok_linker/version'
9
+ require_relative 'ok_linker/error'
10
+ require_relative 'ok_linker/config'
11
+ require_relative 'ok_linker/client'
12
+
13
+ module OkLinker
14
+ class << self
15
+ attr_accessor :config
16
+
17
+ def new(options = {})
18
+ OkLinker::Client.new(options)
19
+ end
20
+
21
+ def configure
22
+ @config = OkLinker::Config.new
23
+ yield @config
24
+ @config
25
+ end
26
+
27
+ def options
28
+ (@config && @config.options) || {}
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ module OkLinker
2
+ class Client
3
+ def initialize(attrs = {})
4
+ attrs = OkLinker.options.merge(attrs)
5
+ Config::VALID_OPTIONS_KEYS.each do |key|
6
+ instance_variable_set("@#{key}".to_sym, attrs[key])
7
+ end
8
+ end
9
+
10
+ def shorten(url)
11
+ post('make-short-url', url: url.to_s)
12
+ end
13
+
14
+ def hide(url)
15
+ post('hide-url', url: url.to_s)
16
+ end
17
+
18
+ def clicks(url)
19
+ get('get-url-clicks', url: url.to_s)
20
+ end
21
+
22
+ def count_urls(params = {})
23
+ get('get-urls', params)
24
+ end
25
+
26
+ def get(method, params = {}, &block)
27
+ request_method(:get, method, params, block)
28
+ end
29
+
30
+ def post(method, params = {}, &block)
31
+ request_method(:post, method, params, block)
32
+ end
33
+
34
+ private
35
+
36
+ def request_method(http_method, method, params, block)
37
+ response = request.send(http_method, method, params)
38
+ response = block.call response if block
39
+ response
40
+ end
41
+
42
+ def request
43
+ @request ||= Request.new(credentials)
44
+ end
45
+
46
+ def credentials
47
+ {
48
+ access_token: @access_token
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ module OkLinker
2
+ class Config
3
+ VALID_OPTIONS_KEYS = [:access_token].freeze
4
+
5
+ attr_accessor *VALID_OPTIONS_KEYS
6
+
7
+ def self.configure
8
+ config = self.new
9
+ yield config
10
+ config
11
+ end
12
+
13
+ def initialize(options={})
14
+ @access_token = options[:access_token] || options['access_token']
15
+ end
16
+
17
+ def options
18
+ options = {}
19
+ VALID_OPTIONS_KEYS.each{ |name| options[name] = send(name) }
20
+ options
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module OkLinker
2
+ module Connection
3
+ API_HOST = 'https://okey.link/api/v1'
4
+
5
+ def connection(token = '')
6
+ options = {
7
+ :headers => {
8
+ :accept => 'application/json',
9
+ :user_agent => "ok_linker ruby gem/#{OkLinker::VERSION}",
10
+ :'Access-Token' => token
11
+ },
12
+ :url => "#{API_HOST}/"
13
+ }
14
+
15
+ client = Faraday.default_adapter
16
+
17
+ Faraday.new(options) do |conn|
18
+ conn.request :multipart
19
+ conn.request :url_encoded
20
+ conn.adapter client
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module OkLinker
2
+ class Error < StandardError
3
+ ClientError = Class.new(self)
4
+ ServerError = Class.new(self)
5
+
6
+ # HTTP status code 400 - invalid params
7
+ BadRequest = Class.new(ClientError)
8
+
9
+ # HTTP status code 401 - not authorized
10
+ Unauthorized = Class.new(ClientError)
11
+
12
+ # HTTP status code 403 - invalid token
13
+ Forbidden = Class.new(ClientError)
14
+
15
+ # HTTP status code 501 - not implemented
16
+ NotImplemented = Class.new(ServerError)
17
+
18
+ ERRORS = {
19
+ 200 => OkLinker::Error::ClientError,
20
+ 400 => OkLinker::Error::BadRequest,
21
+ 401 => OkLinker::Error::Unauthorized,
22
+ 403 => OkLinker::Error::Forbidden,
23
+ 501 => OkLinker::Error::NotImplemented
24
+ }
25
+
26
+ class << self
27
+ # Create a new error from an HTTP response
28
+ def from_response(body)
29
+ new(body.to_s)
30
+ end
31
+ end
32
+
33
+ # Initializes a new Error object
34
+ def initialize(message = '')
35
+ super(message)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ module OkLinker
2
+ class Request
3
+ include OkLinker::Connection
4
+ attr_reader :access_token
5
+
6
+ def initialize(credentials)
7
+ @access_token = credentials[:access_token]
8
+ end
9
+
10
+ def get(path, params = {})
11
+ respond perform_request(:get, path, params)
12
+ end
13
+
14
+ def post(path, params = {})
15
+ respond perform_request(:post, path, params)
16
+ end
17
+
18
+ private
19
+
20
+ def perform_request(method, path, params)
21
+ connection(access_token).send(method) do |req|
22
+ req.url path
23
+ if method == :get
24
+ req.params = params
25
+ else
26
+ req.body = params
27
+ end
28
+ end
29
+ end
30
+
31
+ def respond(response)
32
+ begin
33
+ MultiJson.load(response.body)
34
+ rescue MultiJson::ParseError
35
+ return_error(response.status, response.body)
36
+ end
37
+ end
38
+
39
+ def return_error(code, body)
40
+ fail error(code, body)
41
+ end
42
+
43
+ def error(code, body)
44
+ unless [200, 201].include?(code)
45
+ klass = OkLinker::Error::ERRORS[code]
46
+ klass.from_response(body)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module OkLinker
2
+ VERSION = '0.0.1'
3
+ end
data/ok_linker.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require File.expand_path("../lib/ok_linker/version", __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ok_linker"
5
+ spec.version = OkLinker::VERSION
6
+ spec.authors = ["Ilya Bodrov"]
7
+ spec.email = ["golosizpru@gmail.com"]
8
+ spec.summary = %q{Ruby gem for okey.link API.}
9
+ spec.description = %q{Ruby gem for Odnoklassniki's URL shortening service okey.link.}
10
+ spec.homepage = "https://github.com/bodrovis/ok_linker"
11
+ spec.license = "MIT"
12
+ spec.platform = Gem::Platform::RUBY
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "faraday", "~> 0.9"
19
+ spec.add_dependency "faraday_middleware", "~> 0.9"
20
+ spec.add_dependency "multi_json", "~> 1.11"
21
+
22
+ spec.add_development_dependency "rake", "~> 10.4"
23
+ spec.add_development_dependency "rspec", "~> 3.4"
24
+ spec.add_development_dependency "pry", "0.10.3"
25
+ spec.add_development_dependency "vcr", "~> 3.0", ">= 3.0.0"
26
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ok_linker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Bodrov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.0.0
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '3.0'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 3.0.0
117
+ description: Ruby gem for Odnoklassniki's URL shortening service okey.link.
118
+ email:
119
+ - golosizpru@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - Gemfile
126
+ - LICENSE
127
+ - README.md
128
+ - lib/ok_linker.rb
129
+ - lib/ok_linker/client.rb
130
+ - lib/ok_linker/config.rb
131
+ - lib/ok_linker/connection.rb
132
+ - lib/ok_linker/error.rb
133
+ - lib/ok_linker/request.rb
134
+ - lib/ok_linker/version.rb
135
+ - ok_linker.gemspec
136
+ homepage: https://github.com/bodrovis/ok_linker
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.5.0
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Ruby gem for okey.link API.
160
+ test_files: []
161
+ has_rdoc: