ptilinopus 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ptilinopus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ivan Kabluchkov
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.markdown ADDED
@@ -0,0 +1,57 @@
1
+ # Ptilinopus
2
+
3
+ API Wrapper for [MailerLite](http://mailerlite.com/)
4
+ Inspired by [Gibbon](https://github.com/amro/gibbon/)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ptilinopus'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ptilinopus
19
+
20
+ ## Usage
21
+
22
+ First of all setup your API key:
23
+
24
+ api = Ptilinopus::API.new("api_key")
25
+
26
+ or
27
+
28
+ Ptilinopus::API.api_key = "api_key"
29
+
30
+ If you setup key with the second case you can make API call on the class itself:
31
+
32
+ Ptilinopus::API.call(:post, "subscribers/unsubscribe", {email: "test_email@test.com"})
33
+
34
+
35
+ ### Call API methods
36
+
37
+ Fetch groups:
38
+
39
+ api = Primary::API.new("api_key")
40
+ api.call(:get, "lists")
41
+
42
+ Adding a subscriber:
43
+
44
+ api = Primary::API.new("api_key")
45
+ api.call(:post, "subscribers", {email: 'test_email@test.com', id: 123456})
46
+
47
+ List of all methods you can find here [http://docs.mailerlite.com/]
48
+
49
+ > Note: specify :get and :post types according to API documentaion
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :spec
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,6 @@
1
+ module Ptilinopus
2
+ class MailerliteServerError < StandardError; end
3
+ class MailerliteInvalidMethodError < StandardError; end
4
+ class MailerliteInvalidApiKeyError < StandardError; end
5
+ class MailerliteBadRequestItemError < StandardError; end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Ptilinopus
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ptilinopus.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "httparty"
2
+ require "ptilinopus/version"
3
+ require "ptilinopus/errors"
4
+
5
+ module Ptilinopus
6
+ class API
7
+ include HTTParty
8
+ DEFAULT_HEADER = {"Content-Type" => "application/x-www-form-urlencoded"}
9
+ API_PATH = '/api/v1/'
10
+ attr_accessor :api_key
11
+ default_timeout 10 # HTTParty timeout
12
+ base_uri 'https://app.mailerlite.com'
13
+
14
+ def initialize(api_key = nil)
15
+ @api_key = api_key || self.class.api_key
16
+ end
17
+
18
+ def call(type, method, params = {})
19
+ ensure_api_key(params)
20
+
21
+ params = params.merge({apiKey: @api_key})
22
+ response = self.class.send(type, API_PATH + method, body: params, headers: DEFAULT_HEADER, query_string_normalizer: query_string_normalizer)
23
+
24
+ if response.code != 200
25
+ case response.code
26
+ when 400
27
+ raise MailerliteInvalidMethodError.new
28
+ when 401
29
+ raise MailerliteInvalidApiKeyError.new
30
+ when 404
31
+ raise MailerliteBadRequestItemError.new
32
+ else
33
+ raise MailerliteServerError.new
34
+ end
35
+ end
36
+
37
+ return response.body
38
+ end
39
+
40
+ private
41
+
42
+ # FIXME Some methods of Mailerlite doesn't accept encoded emails
43
+ def query_string_normalizer
44
+ proc { |query|
45
+ query.map do |key, value|
46
+ "#{key}=#{value}"
47
+ end.join('&')
48
+ }
49
+ end
50
+
51
+ def ensure_api_key(params)
52
+ unless @api_key || params[:apiKey]
53
+ raise StandardError, "You must set an api_key prior to making a call"
54
+ end
55
+ end
56
+
57
+ class << self
58
+ attr_accessor :api_key
59
+
60
+ def method_missing(sym, *args, &block)
61
+ new(self.api_key).send(sym, *args, &block)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ptilinopus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ptilinopus"
8
+ spec.version = Ptilinopus::VERSION
9
+ spec.authors = ["Ivan Kabluchkov"]
10
+ spec.email = ["ikabluchkov@gmail.com"]
11
+ spec.description = %q{API wrapper for Mailerlite}
12
+ spec.summary = %q{API wrapper for Mailerlite}
13
+ spec.homepage = "http://github.com/lfidnl/ptilinopus"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "fakeweb"
25
+ spec.add_development_dependency "json"
26
+
27
+ spec.add_dependency('httparty')
28
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ptilinopus do
4
+ context "set api key" do
5
+ before do
6
+ @api_key = "test_key"
7
+ end
8
+
9
+ after do
10
+ Ptilinopus::API.api_key = nil
11
+ end
12
+
13
+ it "in constructor" do
14
+ @ptilinopus = Ptilinopus::API.new(@api_key)
15
+ expect(@ptilinopus.api_key).to eq(@api_key)
16
+ end
17
+
18
+ it "set api" do
19
+ Ptilinopus::API.api_key = @api_key
20
+ @ptilinopus = Ptilinopus::API.new
21
+ expect(@ptilinopus.api_key).to eq(@api_key)
22
+ end
23
+ end
24
+
25
+ context "call" do
26
+ before do
27
+ @ptilinopus = Ptilinopus::API.new("test_api")
28
+ @method = "api_method"
29
+ end
30
+
31
+ it "get method" do
32
+ register_method(:get, @method)
33
+ expect(@ptilinopus.call(:get, @method)).to eq({}.to_s)
34
+ end
35
+
36
+ it "post method" do
37
+ register_method(:post, @method)
38
+ expect(@ptilinopus.call(:post, @method)).to eq({}.to_s)
39
+ end
40
+
41
+ context "raise error" do
42
+ it "if api key is no specified" do
43
+ @ptilinopus.api_key = nil
44
+ expect {@ptilinopus.call(:get, @method)}.to raise_error(StandardError)
45
+ end
46
+
47
+ it "if server return HTTP400" do
48
+ register_method(:get, @method, {}, ["400", "Bad Request"])
49
+ expect {@ptilinopus.call(:get, @method)}.to raise_error(Ptilinopus::MailerliteInvalidMethodError)
50
+ end
51
+
52
+ it "if server return HTTP401" do
53
+ register_method(:get, @method, {}, ["401", "Unauthorized"])
54
+ expect {@ptilinopus.call(:get, @method)}.to raise_error(Ptilinopus::MailerliteInvalidApiKeyError)
55
+ end
56
+
57
+ it "if server return HTTP404" do
58
+ register_method(:get, @method, {}, ["404", "Not found"])
59
+ expect {@ptilinopus.call(:get, @method)}.to raise_error(Ptilinopus::MailerliteBadRequestItemError)
60
+ end
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def register_method(type, method, body = {}, status = ["200", "OK"])
67
+ FakeWeb.register_uri(type, URI.join(Ptilinopus::API.base_uri, Ptilinopus::API::API_PATH, method), body: body.to_json, status: status)
68
+ end
69
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'fakeweb'
4
+
5
+ Bundler.setup
6
+ require 'ptilinopus'
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+
13
+ config.order = 'random'
14
+
15
+ config.before(:all) do
16
+ FakeWeb.allow_net_connect = false
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ptilinopus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Kabluchkov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: fakeweb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: httparty
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: API wrapper for Mailerlite
111
+ email:
112
+ - ikabluchkov@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.markdown
122
+ - Rakefile
123
+ - lib/ptilinopus.rb
124
+ - lib/ptilinopus/errors.rb
125
+ - lib/ptilinopus/version.rb
126
+ - ptilinopus.gemspec
127
+ - spec/ptilinopus/ptilinopus_spec.rb
128
+ - spec/spec_helper.rb
129
+ homepage: http://github.com/lfidnl/ptilinopus
130
+ licenses:
131
+ - MIT
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ segments:
143
+ - 0
144
+ hash: 2632116737961080912
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ segments:
152
+ - 0
153
+ hash: 2632116737961080912
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.24
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: API wrapper for Mailerlite
160
+ test_files:
161
+ - spec/ptilinopus/ptilinopus_spec.rb
162
+ - spec/spec_helper.rb