api_connector 0.1.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: 979197de3db76b40d6333064617dfbb1eb6abcb1
4
+ data.tar.gz: 1f57a6ec15fcc5310a4543a8f4c3d121917b7f59
5
+ SHA512:
6
+ metadata.gz: e6e5cfb6595ff164aa12d2792ba153cccf93c6ee6a924df663b489ada74b34ef833cec6e2dd1809c985672a3f4c883f9ca6315d3515dfcb664c33e3897ec6b2c
7
+ data.tar.gz: 5640599b700c5a1eae1059a90139e337ce8b6c893a524a4573515aa319155726532a8793776ecf4f387e00bbfe629761133afcc43c7177b9498a856098e482d7
data/.gitignore ADDED
@@ -0,0 +1,39 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
37
+
38
+ .swp
39
+ .swo
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.0.0
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 api_connector.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_connector (0.1.0)
5
+ activesupport (~> 4.2.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.4)
11
+ i18n (~> 0.7)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.3, >= 0.3.4)
15
+ tzinfo (~> 1.1)
16
+ diff-lcs (1.2.5)
17
+ i18n (0.7.0)
18
+ json (1.8.3)
19
+ minitest (5.8.2)
20
+ rake (10.5.0)
21
+ rspec (3.4.0)
22
+ rspec-core (~> 3.4.0)
23
+ rspec-expectations (~> 3.4.0)
24
+ rspec-mocks (~> 3.4.0)
25
+ rspec-core (3.4.4)
26
+ rspec-support (~> 3.4.0)
27
+ rspec-expectations (3.4.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.4.0)
30
+ rspec-mocks (3.4.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.4.0)
33
+ rspec-support (3.4.1)
34
+ thread_safe (0.3.5)
35
+ tzinfo (1.2.2)
36
+ thread_safe (~> 0.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ api_connector!
43
+ bundler (~> 1.10)
44
+ rake (~> 10.0)
45
+ rspec
46
+
47
+ BUNDLED WITH
48
+ 1.10.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Juan Manuel Rodulfo Salcedo
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.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Juan Manuel Rodulfo
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,57 @@
1
+ # ApiConnector
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/api_connector`. 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 'api_connector'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install api_connector
22
+
23
+ ## Usage
24
+
25
+ ###Available commands:
26
+
27
+ ```ruby
28
+ new(opts) - Creates a new Connector. (opts to be defined)
29
+ get(hash)
30
+ post(hash, payload)
31
+ delete(hash)
32
+ ```
33
+ ###Hash structure:
34
+ ==== Attributes
35
+ ** hash[:endpoint] - Url endpoint ex. /product/get (no need to specify the version)
36
+ ** hash[:headers] - Request arguments, (add headers key for extra headers options) ex. hash[:headers] = { 'content-type' => 'xml' }
37
+ ** hash[params] - Request parameters. ex. hash[:params] = { 'param1' => 'XXXXXX' }
38
+
39
+ ###Payload
40
+ JSON-formatted hash.
41
+
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_connector. 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.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+
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
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_connector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api_connector"
8
+ spec.version = ApiConnector::VERSION
9
+ spec.authors = ["Juan Manuel Rodulfo"]
10
+ spec.email = ["jm.rodulfo.salcedo@gmail.com"]
11
+
12
+ spec.summary = "API connectors"
13
+ spec.description = "API Connector main class for wrapping RestClient functionality"
14
+ spec.homepage = "https://github.com/Juanchote/api_connectors"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "http://mygemserver.com"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+
34
+ spec.add_dependency 'activesupport', '~> 4.2.4'
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api_connector"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
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,10 @@
1
+ require "api_connector/version"
2
+ require 'api_connector/api_connector'
3
+ require 'active_support/core_ext/hash/keys'
4
+ require 'logger'
5
+
6
+ module ApiConnector
7
+ def self.new(options={})
8
+ @conn = Connectors::ApiConnector.new(options)
9
+ end
10
+ end
@@ -0,0 +1,144 @@
1
+ # * Parent Connector for APIs
2
+ # * author: juanma <jm.rodulfo.salcedo@gmail.com>
3
+ module Connectors #:nodoc:
4
+ # === Heading
5
+ # This class has all the common logic from <b>api connectors</b>.
6
+ class ApiConnector
7
+ attr_reader :logger, :api_client_id, :api_domain, :api_mall_id,
8
+ :api_domain_format, :api_headers_token, :connection_protocol,
9
+ :headers, :code, :cookies, :cookie_jar, :request, :body
10
+
11
+ def initialize(options = {}) #:notnew:
12
+ options.symbolize_keys!
13
+
14
+ #@logger = Logger.new "#{Rails.root}/log/#{self.class.to_s.demodulize.underscore}.log"
15
+ #@logger.level = Rails.logger.level
16
+
17
+ opts_to_vars(options)
18
+ end
19
+
20
+ # makes a GET request
21
+ #
22
+ # ==== Attributes
23
+ # * +hash+ - Hash of Parameters
24
+ # ** +endpoint+ - Url endpoint ex. /product/get (no need to specify the version)
25
+ # ** +args+ - Request arguments, (add headers key for extra headers options) ex. hash[:headers] = { 'content-type' => 'xml' }
26
+ # ** +params+ - Request parameters. ex. hash[:params] = { 'merchantId' => 'XXXXXX' }
27
+ def get(hash={})
28
+ hash.symbolize_keys!
29
+ call(:get, hash[:endpoint], (hash[:args]||{}), hash[:params]||{})
30
+ end
31
+
32
+ def delete(hash={})
33
+ hash.symbolize_keys!
34
+ call(:delete, hash[:endpoint], (hash[:args]||{}), hash[:params]||{})
35
+ end
36
+
37
+ # makes a POST request
38
+ #
39
+ # ==== Attributes
40
+ # * +hash+ - Hash of parameters
41
+ # ** +endpoint+ - Url endpoint ex. /product/createOrUpdate
42
+ # ** +args+ - Request arguments, (add headers key for extra headers options) ex. hash[:headers] = { 'content-type' => 'xml' }
43
+ # * +payload+ - Data for the request ex. { merchantId: 'asdasdsadas', products: [{ ... },{ ...}...]}
44
+ def post hash={}, payload
45
+ raise 'Payload cannot be blank' if payload.nil? || payload.empty?
46
+
47
+ hash.symbolize_keys!
48
+ call(:post, hash[:endpoint], (hash[:args]||{}).merge({:method => :post}), payload)
49
+ end
50
+
51
+ # low level api for request (needed por PUT, PATCH & DELETE methods)
52
+ #
53
+ # ==== Attributes
54
+ # * +endpoint+ - Url endpoint ex. /merchant/get
55
+ # * +args+ - Request arguments, (add headers key for extra headers options) ex. { method: :get, headers: { 'content-type' => 'xml' } } (method key is needed, otherwise :get will be setted)
56
+ # * +params+ - Request parameters / payload data
57
+ def call method, endpoint, args={}, params
58
+ raise "Endpoint can't be blank" unless endpoint
59
+ raise "Method is missing" unless method
60
+
61
+ url = (method == :get || method == :delete) ? url(endpoint,params) : url(endpoint)
62
+
63
+ begin
64
+ RestClient::Request.execute(method: method,
65
+ url: url,
66
+ headers: header(args[:headers]),
67
+ payload: params || {}
68
+ ) do |response, request, result|
69
+ status = response.code == 200 ? :debug : :error
70
+ #print(status, request, response.body)
71
+ parse(response, endpoint)
72
+ end
73
+ rescue RestClient::RequestTimeout
74
+ { status: '400', message: "RestClient timeout" }
75
+ end
76
+ end
77
+
78
+ def parse(response, endpoint = nil)
79
+ @headers, @code, @cookies, @cookie_jar, @request, @body = response.headers, response.code, response.cookies, response.cookie_jar, response.request, response.body
80
+ begin
81
+ JSON.parse(response)
82
+ rescue JSON::ParserError
83
+ { status: '400', message: "RestClient failed to parse JSON: #{response}" }
84
+ end
85
+ end
86
+
87
+ protected
88
+
89
+ def opts_to_vars(opts)
90
+ instance_eval do
91
+ opts.each do |k, v|
92
+ instance_variable_set("@#{k}".to_sym, v)
93
+ end
94
+ end
95
+ end
96
+
97
+ def header headers={}
98
+ common_headers.merge({
99
+ 'X-Client-Id' => @x_person_id,
100
+ 'Content-Type' => @content_type || 'application/json',
101
+ 'Charset' => @charset || 'utf-8'
102
+ }).merge(headers || {})
103
+ end
104
+
105
+ def url endpoint, args={}
106
+ url_constructor endpoint, args
107
+ end
108
+
109
+
110
+ def print(status, request, response)
111
+ status = :debug
112
+ @logger.send(status,
113
+ "#{DateTime.now} "\
114
+ "- Request: #{request.inspect} "\
115
+ "- Response: #{response.force_encoding('utf-8')}"
116
+ )
117
+ end
118
+
119
+ def common_headers
120
+ { 'X-Client-Id' => api_client_id }.merge(
121
+ {"Authorization" => "Basic #{@api_headers_token}"}
122
+ ) if @api_headers_token
123
+ end
124
+
125
+ def url_constructor endpoint, hash
126
+ url = "#{@connection_protocol}://#{format(api_domain)}/#{format(@prefix)}" << (@version ? "/#{@version}" : "") << "/#{format(endpoint)}"
127
+ url << ("?#{parametrize(hash)}") unless hash.empty?
128
+ url
129
+ end
130
+
131
+ def parametrize hash
132
+ hash.map do |key,values|
133
+ [values].flatten.map do |value|
134
+ "#{key}=#{value}"
135
+ end
136
+ end.join('&')
137
+ end
138
+
139
+ def format string
140
+ match_data = string.match(/\w+.+\w+/)
141
+ match_data ? match_data[0] : ''
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ module ApiConnector
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Juan Manuel Rodulfo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-10 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.2.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 4.2.4
69
+ description: API Connector main class for wrapping RestClient functionality
70
+ email:
71
+ - jm.rodulfo.salcedo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - api_connector.gemspec
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/api_connector.rb
90
+ - lib/api_connector/api_connector.rb
91
+ - lib/api_connector/version.rb
92
+ homepage: https://github.com/Juanchote/api_connectors
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ allowed_push_host: http://mygemserver.com
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.0.14
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: API connectors
117
+ test_files: []
118
+ has_rdoc: