pechkinrb 0.0.2

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: cd3a89fe6b95d51923b3f65d041b7afe41218bf2
4
+ data.tar.gz: 1eb794dec52e6516e6c40333114bbf28bf6d096b
5
+ SHA512:
6
+ metadata.gz: c7f8ad2a4c1c40c6853eab8b4fe590421ba7e1d5b5a8471fdda97fd0c47d588e47523e5ac5781c3e82349f0bea18840b34d297698c5941eb59605c1e900eeb66
7
+ data.tar.gz: efd2cab51e9ef1c39972fff1baddc33e807d969097fc883a3b0f8bcd93a2525aaac3b049c22b57921d37b03455fef739ec50b7632af982f10d3ecc712f7b0eb9
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pechkinrb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Alexandr Prokopenko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Pechkinrb
2
+
3
+ This gem provides you with easy Ruby interface for pechkin-mail.ru mail service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pechkinrb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pechkinrb
18
+
19
+ ## Usage
20
+
21
+ require "pechkinrb"
22
+ conn = Pechkin::Connection.new('login', 'password')
23
+ conn.lists # => Array of Pechkin::List instances
24
+
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/[my-github-username]/pechkinrb/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,4 @@
1
+ module Pechkin
2
+ class ApiException < StandardError
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ module Pechkin
2
+ # Mailing list representation class
3
+ class List < Model
4
+ attr_reader :raw
5
+
6
+ FIELDS = [
7
+ :id,
8
+ :state,
9
+ :count_active,
10
+ :count_all,
11
+ :name,
12
+ :description,
13
+ :company,
14
+ :abuse_name,
15
+ :phone,
16
+ :address,
17
+ :city,
18
+ :url
19
+ ]
20
+
21
+ FIELDS.each do |f|
22
+ attr_reader f
23
+ end
24
+
25
+ # Initialize new list instance
26
+ #
27
+ # @param connection [Pachkin::Connection] Active connection
28
+ # @param doc [Hash] API object data
29
+ def initialize(connection, doc)
30
+ super(connection, doc)
31
+ FIELDS.each do |field|
32
+ instance_variable_set :"@#{field}", doc[field.to_s]
33
+ end
34
+ end
35
+
36
+ # Invokes 'lists.get_members' API method to retreive list members
37
+ #
38
+ # @param params [Hash] Params to be passed
39
+ # @return [Array] Array of Pechkin::Members instances
40
+ def get_members(params = {})
41
+ connection.call_method('lists.get_members', params.merge(id_params)).map {|member| Pechkin::Member.new(connection, member)}
42
+ end
43
+
44
+ private
45
+
46
+ def id_params
47
+ {list_id: id}
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,62 @@
1
+ module Pechkin
2
+ class Member < Model
3
+ attr_reader :raw
4
+
5
+ FIELDS = [
6
+ :id,
7
+ :list_id,
8
+ :email,
9
+ :state,
10
+ :merge_1,
11
+ :merge_2,
12
+ :merge_3,
13
+ :merge_4,
14
+ :merge_5,
15
+ :optin_time,
16
+ :unsub_time,
17
+ :lastedit_time
18
+ ]
19
+
20
+ FIELDS.each do |f|
21
+ attr_reader f
22
+ end
23
+
24
+ # Initialize new member instance
25
+ #
26
+ # @param connection [Pachkin::Connection] Active connection
27
+ # @param doc [Hash] API object data
28
+ def initialize(connection, doc)
29
+ super(connection, doc)
30
+ FIELDS.each do |field|
31
+ instance_variable_set :"@#{field}", doc[field.to_s]
32
+ end
33
+ end
34
+
35
+ # Invokes 'lists.delete_member' API method
36
+ #
37
+ def delete_member
38
+ connection.call_method('lists.delete_member', id_params)
39
+ end
40
+
41
+ # Invokes 'lists.update_member' API method
42
+ #
43
+ # @param params [Hash] Params to be passed
44
+ def update_member(params)
45
+ connection.call_method('lists.update_member', params.merge(id_params))
46
+ end
47
+
48
+ # Invokes 'lists.get_lists' API method to retrieve member list
49
+ #
50
+ # @return params [Pechkin::List] List instance, accosiated with member
51
+ def list
52
+ connection.get_list(list_id)
53
+ end
54
+
55
+ private
56
+
57
+ def id_params
58
+ {member_id: id}
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ module Pechkin
2
+ # Basic class for API model objects
3
+ class Model
4
+
5
+ # API object data
6
+ attr_reader :raw
7
+
8
+ # Initialize new model instance
9
+ #
10
+ # @param connection [Pachkin::Connection] Active connection
11
+ # @param doc [Hash] API object data
12
+ def initialize(connection, doc)
13
+ @connection = connection
14
+ @raw = doc
15
+ end
16
+
17
+ protected
18
+
19
+ def connection
20
+ @connection
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,71 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Pechkin
5
+ class Connection
6
+
7
+ API_URL = 'https://api.pechkin-mail.ru'
8
+
9
+ # Initializes new connection instance
10
+ #
11
+ # @param username [String] Service username
12
+ # @param password [String] Password
13
+ def initialize(username, password)
14
+ @username, @password = username, password
15
+ end
16
+
17
+ # Memoized Faraday connection factory
18
+ #
19
+ # @return Faraday connection instance
20
+ def connection
21
+ @conn ||= Faraday.new(:url => 'https://api.pechkin-mail.ru') do |faraday|
22
+ faraday.request :url_encoded
23
+ faraday.response :json
24
+ faraday.adapter Faraday.default_adapter
25
+ end
26
+ end
27
+
28
+ # Invokes API method by name
29
+ #
30
+ # @param method [String] Method name, corresponding API reference https://pechkin-mail.ru/api/
31
+ # @param params [Hash] Params to be passed
32
+ def call_method(method, params = {})
33
+ response = connection.post '/', {method: method}.merge(credentials).merge(params)
34
+ err_code = response.body["response"]["msg"]["err_code"]
35
+ unless err_code == 0
36
+ raise Pechkin::ApiException.new(response.body["response"]["msg"]["text"])
37
+ end
38
+ response.body["response"]["data"]
39
+ end
40
+
41
+ # Invokes 'lists.get' API method
42
+ #
43
+ # @param params [Hash] Params to be passed
44
+ # @return [Array] Array of Pechkin::List instances
45
+ def lists(params = {})
46
+ call_method('lists.get', params).map {|list_raw| Pechkin::List.new(self, list_raw)}
47
+ end
48
+
49
+ # Invokes 'lists.get' API method to retrieve single List object
50
+ #
51
+ # @param id [Fixnum] List id
52
+ # @return [Pechkin::List] List instance
53
+ def get_list(id)
54
+ lists(list_id: id)[0]
55
+ end
56
+
57
+ # Invokes 'lists.get_member' API method
58
+ #
59
+ # @param email [String] Email for search
60
+ # @return [Array] Array of Pechkin::Member instances
61
+ def get_member(email)
62
+ call_method('lists.get_member', {email: email}).map {|member| Pechkin::Member.new(connection, member)}
63
+ end
64
+
65
+ private
66
+
67
+ def credentials
68
+ {username: @username, password: @password}
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Pechkinrb
2
+ VERSION = "0.0.2"
3
+ end
data/lib/pechkinrb.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "pechkinrb/version"
2
+
3
+ require "pechkinrb/api_exception"
4
+ require "pechkinrb/model"
5
+ require "pechkinrb/member"
6
+ require "pechkinrb/list"
7
+ require "pechkinrb/pechkin"
data/pechkinrb.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pechkinrb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pechkinrb"
8
+ spec.version = Pechkinrb::VERSION
9
+ spec.authors = ["Alexandr Prokopenko"]
10
+ spec.email = ["prokopenko@igc.ru"]
11
+ spec.summary = %q{Interface for pechkin-mail.ru service API}
12
+ spec.description = %q{Interface for pechkin-mail.ru service API}
13
+ spec.homepage = "https://github.com/crsde/PechkinRb"
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency 'faraday'
26
+ spec.add_runtime_dependency 'faraday_middleware'
27
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pechkin::Connection do
4
+ let(:connection) { Pechkin::Connection.new('1', '2') }
5
+
6
+ before(:each) do
7
+ stub_connection = Faraday.new do |builder|
8
+ # builder.response :logger
9
+ builder.response :json
10
+
11
+ builder.adapter :test do |stubs|
12
+ stubs.post("/", {username: '1', password: '2', method: 'get_good', param: 'value'} ) do
13
+ [200, {}, {
14
+ 'response' =>
15
+ {
16
+ 'msg' => { 'text' => "OK", 'err_code' => 0 },
17
+ 'data' => { 'ok' => "ok" }
18
+ }
19
+ }]
20
+ end
21
+
22
+ stubs.post("/", {username: '1', password: '2', method: 'get_bad', param: 'value'} ) do
23
+ [200, {}, {
24
+ 'response' =>
25
+ {
26
+ 'msg' => { 'text' => "Error", 'err_code' => 1 },
27
+ 'data' => { 'ok' => "ok" }
28
+ }
29
+ }]
30
+ end
31
+ end
32
+ end
33
+
34
+ expect(connection).to receive(:connection).and_return(stub_connection)
35
+ end
36
+
37
+ describe "#call_method" do
38
+ it "returns response data" do
39
+ expect(connection.call_method('get_good', {param: 'value'})).to eq('ok' => "ok")
40
+ end
41
+
42
+ it "raises error when bad response received" do
43
+ expect { connection.call_method('get_bad', {param: 'value'}) }.to raise_error(Pechkin::ApiException)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'pechkinrb'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pechkinrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alexandr Prokopenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-29 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Interface for pechkin-mail.ru service API
84
+ email:
85
+ - prokopenko@igc.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/pechkinrb.rb
96
+ - lib/pechkinrb/api_exception.rb
97
+ - lib/pechkinrb/list.rb
98
+ - lib/pechkinrb/member.rb
99
+ - lib/pechkinrb/model.rb
100
+ - lib/pechkinrb/pechkin.rb
101
+ - lib/pechkinrb/version.rb
102
+ - pechkinrb.gemspec
103
+ - spec/connection_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/crsde/PechkinRb
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Interface for pechkin-mail.ru service API
129
+ test_files:
130
+ - spec/connection_spec.rb
131
+ - spec/spec_helper.rb
132
+ has_rdoc: