magnews-ruby 0.1.0

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: 468f2da87827c283d63555722640ab4c5d64c240
4
+ data.tar.gz: 85deedf30c5df8a5afcae9338c06025d92d7a05c
5
+ SHA512:
6
+ metadata.gz: ef082b576bd83dde5253e58b846323de1115253416c4e8b258017cd04fc122f6081702be7ef885f3f406fb04e6be8a25dfded0aa1f5f0b66a042b874a8c38cf5
7
+ data.tar.gz: 01c3f4f5855ec185680f9dc93208f997084991d3f51f91a398356b265c48aa8f1f33e3e805234590a0e7f2c46673a5ce3d0e13dd6481bdfbfab5ac11b634a79d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magnews.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Fabrizio Monti
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Magnews
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/magnews`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'magnews'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install magnews
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/magnews. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "magnews"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require "rest-client"
2
+ require "active_support"
3
+ require "active_support/core_ext"
4
+ require "json"
5
+ require "logger"
6
+
7
+ require "magnews/version"
8
+ require "magnews/errors"
9
+ require "magnews/util/configuration"
10
+ require "magnews/util/header_helper"
11
+ require "magnews/util/url_helper"
12
+ require "magnews/util/logger_helper"
13
+ require "magnews/response"
14
+ require "magnews/contact"
15
+
16
+ module Magnews
17
+ extend SingleForwardable
18
+
19
+ def_delegators :configuration, *Util::Configuration::DEFAULTS.keys
20
+
21
+ def self.configure(&block)
22
+ yield configuration
23
+ end
24
+
25
+ def self.configuration
26
+ @configuration ||= Util::Configuration.new
27
+ end
28
+ private_class_method :configuration
29
+ end
@@ -0,0 +1,42 @@
1
+ module Magnews
2
+ class Contact < OpenStruct
3
+ extend Magnews::HeaderHelper
4
+ extend Magnews::UrlHelper
5
+ extend Magnews::LoggerHelper
6
+
7
+ class << self
8
+ def create!(values, options={})
9
+ normalize_values!(values)
10
+ payload = { options: options.reverse_merge({ iddatabase: Magnews.iddatabase }), values: values }.to_json
11
+ RestClient.post(url_for("contacts/subscribe"), payload, common_headers) do |response, request, result, &block|
12
+ logger.info { response.body }
13
+
14
+ if (200..207).include? response.code
15
+ respond_to_200(response)
16
+ elsif Magnews::EXCEPTIONS_MAP[response.code].present?
17
+ raise Magnews::EXCEPTIONS_MAP[response.code].new(response, response.code)
18
+ else
19
+ response.return!(request, result, &block)
20
+ end
21
+ end
22
+ end
23
+
24
+ def respond_to_200(response)
25
+ body = JSON.parse(response.body).deep_symbolize_keys!
26
+ if body[:ok]
27
+ return true
28
+ else
29
+ response.code = 422
30
+ raise Magnews::EXCEPTIONS_MAP[response.code].new(response, response.code)
31
+ end
32
+ end
33
+
34
+ def normalize_values!(values)
35
+ values.each do |key, value|
36
+ values[key] = value.join(",") if value.is_a? Array
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,19 @@
1
+ module Magnews
2
+
3
+ EXCEPTIONS_MAP = {}
4
+
5
+ class MissingAuthToken < StandardError; end
6
+
7
+ RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, rc_klass|
8
+ klass = Class.new(rc_klass)
9
+ const_set rc_klass.name.demodulize, klass
10
+ EXCEPTIONS_MAP[code] = klass
11
+ end
12
+
13
+ UnprocessableEntity.class_eval do
14
+
15
+ def errors
16
+ JSON.parse(http_body).deep_symbolize_keys[:errors]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ RestClient::AbstractResponse.module_eval do
2
+ attr_writer :code
3
+ end
@@ -0,0 +1,30 @@
1
+ module Magnews
2
+ module Util
3
+ class Configuration
4
+
5
+ DEFAULTS = {
6
+ api_version: "v19",
7
+ endpoint: "https://ws-mn1.mag-news.it/ws/rest/api",
8
+ iddatabase: 1,
9
+ logger: Logger.new(STDOUT),
10
+ auth_token: nil
11
+ }
12
+
13
+ DEFAULTS.each_key do |attribute|
14
+ attr_accessor attribute
15
+ end
16
+
17
+ def initialize(opts={})
18
+ DEFAULTS.each do |attribute, value|
19
+ send("#{attribute}=".to_sym, opts.fetch(attribute, value))
20
+ end
21
+ end
22
+
23
+ def logger=(logger)
24
+ @logger = logger
25
+ RestClient.log = @logger
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Magnews
2
+ module HeaderHelper
3
+ def auth_header
4
+ raise Magnews::MissingAuthToken if Magnews.auth_token.blank?
5
+ { Authorization: "Bearer #{Magnews.auth_token}" }
6
+ end
7
+
8
+ def common_headers
9
+ auth_header.merge({ content_type: :json, accept: :json })
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Magnews
2
+ module LoggerHelper
3
+ def logger
4
+ Magnews.logger
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Magnews
2
+ module UrlHelper
3
+ def url_for(resource)
4
+ "#{Magnews.endpoint}/#{Magnews.api_version}/#{resource}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Magnews
2
+ VERSION = "0.1.0"
3
+ end
data/magnews.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magnews/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "magnews-ruby"
8
+ spec.version = Magnews::VERSION
9
+ spec.authors = ["Filippo Gangi Dino", "Fabrizio Monti"]
10
+ spec.email = ["filippo.gangidino@welaika.com", "fabrizio.monti@welaika.com"]
11
+
12
+ spec.summary = %q{Unofficial Magnews REST API ruby interface }
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/welaika/magnews-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rest-client", "~> 1.8.0"
23
+ spec.add_dependency "activesupport", "< 4.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.3.0"
28
+ spec.add_development_dependency "shoulda-matchers", "~> 2.8.0"
29
+ spec.add_development_dependency "pry-byebug", "~> 3.1.0"
30
+ spec.add_development_dependency "webmock", "~> 1.21.0"
31
+ spec.add_development_dependency "simplecov", "~> 0.10.0"
32
+ spec.add_development_dependency "priscilla", "~> 1.0.3"
33
+ end
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magnews-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Filippo Gangi Dino
8
+ - Fabrizio Monti
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 1.8.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 1.8.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '4.2'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '4.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.10'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.10'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 3.3.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 3.3.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: shoulda-matchers
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 2.8.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 2.8.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry-byebug
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 3.1.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 3.1.0
112
+ - !ruby/object:Gem::Dependency
113
+ name: webmock
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: 1.21.0
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: 1.21.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: 0.10.0
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.10.0
140
+ - !ruby/object:Gem::Dependency
141
+ name: priscilla
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: 1.0.3
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: 1.0.3
154
+ description: ''
155
+ email:
156
+ - filippo.gangidino@welaika.com
157
+ - fabrizio.monti@welaika.com
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - ".gitignore"
163
+ - ".rspec"
164
+ - ".travis.yml"
165
+ - CODE_OF_CONDUCT.md
166
+ - Gemfile
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/rake
172
+ - bin/rspec
173
+ - bin/setup
174
+ - lib/magnews-ruby.rb
175
+ - lib/magnews/contact.rb
176
+ - lib/magnews/errors.rb
177
+ - lib/magnews/response.rb
178
+ - lib/magnews/util/configuration.rb
179
+ - lib/magnews/util/header_helper.rb
180
+ - lib/magnews/util/logger_helper.rb
181
+ - lib/magnews/util/url_helper.rb
182
+ - lib/magnews/version.rb
183
+ - magnews.gemspec
184
+ homepage: https://github.com/welaika/magnews-ruby
185
+ licenses:
186
+ - MIT
187
+ metadata: {}
188
+ post_install_message:
189
+ rdoc_options: []
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ requirements: []
203
+ rubyforge_project:
204
+ rubygems_version: 2.4.6
205
+ signing_key:
206
+ specification_version: 4
207
+ summary: Unofficial Magnews REST API ruby interface
208
+ test_files: []