easy_meli 0.3.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
+ SHA256:
3
+ metadata.gz: d17badfe56ecab178ff6cb784e9d78bb8c0cd4ac9421e5ea356a52212622c76a
4
+ data.tar.gz: 4f40821e57261ade83265e971ea8a45a85db038fff68b26744daeaedf80c87fd
5
+ SHA512:
6
+ metadata.gz: 8b95637656926bd1b393ed569c136cb5f486b2c76ec46ef733eabb1746ef52a062d4f669c0b068ca93c2dd0dc52641483b9866304fa10a9e8c22847254971387
7
+ data.tar.gz: 5c2310fbd9282c91f5777596d05f3504585cd394f3f31a59a1a514a41953593cd5422c87db971f4b3a5ce77b6989e8251676d7bd6257135fff79d24915ca3e30
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ /vender
11
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in easy_meli.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Eric Northam
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,97 @@
1
+ # EasyMeli
2
+
3
+ A simple gem to access low level MercadoLibre api calls. This is the draft beta version.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'easy_meli'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install easy_meli
20
+
21
+ ## Usage
22
+
23
+ Configure the gem with your application ID and secret key.
24
+
25
+ ```ruby
26
+ EasyMeli.configure do |config|
27
+ config.application_id = 'your_app_id'
28
+ config.secret_key = 'your_secret_key'
29
+ end
30
+ ```
31
+
32
+ To get the authorization url that the end-user uses to give your app authorization to access MercadoLibre on your part call the `authorization_url` with the desired country and the url to redirect to in order to complete the authorization.
33
+
34
+ ```ruby
35
+ EasyMeli::AuthorizationClient.authorization_url('MX', 'your_redirect_url')
36
+ ```
37
+
38
+ Once MercadoLibre calls your redirect url you can get a refresh token by calling
39
+
40
+ ```ruby
41
+ response = EasyMeli::AuthorizationClient.new.create_token('the_code_in_the_redirect', 'the_same_redirect_url_as_above')
42
+ ```
43
+ This will return a response object with a json body that you can easily access via `response.to_h`.
44
+
45
+ If you want to refresh the token call
46
+
47
+ ```ruby
48
+ response = EasyMeli::AuthorizationClient.new.refresh_token('a_refresh_token')
49
+ ```
50
+
51
+ Once you can have an access token you can create a client and call the supported http verb methods.
52
+
53
+ ```ruby
54
+ client = EasyMeli::ApiClient.new(access_token)
55
+
56
+ client.get(path, query: { a: 1 })
57
+ client.post(path, query: { a: 1 }, body: { b: 1 })
58
+ client.put(path, query: { a: 1 }, body: { b: 1 })
59
+ client.delete(path, query: { a: 1 })
60
+ ```
61
+
62
+ You can also pass a logger when instantiating the `EasyMeli::ApiClient` or `EasyMeli::AuthorizationClient`. The logger class must implement a `log` method which will be called with the [HTTParty response](https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/Response) for every remote request sent.
63
+
64
+ ```ruby
65
+ EasyMeli::AuthorizationClient.new(logger: my_logger)
66
+ EasyMeli::ApiClient.new(access_token, logger: my_logger)
67
+ ```
68
+
69
+ ### Complete example showing how to retrieve a user profile
70
+ ```ruby
71
+ EasyMeli.configure do |config|
72
+ config.application_id = "your_app_id"
73
+ config.secret_key = "your_secret_key"
74
+ end
75
+
76
+ authorization_client = EasyMeli::AuthorizationClient.new
77
+ token = authorization_client.refresh_token(previously_stored_refresh_token).to_h['access_token']
78
+
79
+ api_client = EasyMeli::ApiClient.new(token)
80
+
81
+ response = api_client.get('/users/me')
82
+
83
+ ```
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/easybroker/easy_meli.
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy_meli"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/easy_meli.gemspec ADDED
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "easy_meli/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_meli"
8
+ spec.version = EasyMeli::VERSION
9
+ spec.authors = ["Eric Northam"]
10
+ spec.email = ["eric@northam.us"]
11
+
12
+ spec.summary = %q{A simple gem to work with MercadoLibre's API}
13
+ spec.homepage = "https://github.com/easybroker/easy_meli"
14
+ spec.license = "MIT"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against " \
20
+ "public gem pushes."
21
+ end
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "httparty", "~> 0.18"
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.17"
35
+ spec.add_development_dependency "rake", "~> 13.0"
36
+ spec.add_development_dependency "minitest", "~> 5.0"
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency "webmock"
39
+ spec.add_development_dependency "mocha"
40
+ end
data/lib/easy_meli.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'httparty'
2
+
3
+ require 'easy_meli/version'
4
+ require 'easy_meli/errors'
5
+ require 'easy_meli/configuration'
6
+ require 'easy_meli/authorization_client'
7
+ require 'easy_meli/api_client'
8
+
9
+ module EasyMeli
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.configure
15
+ yield(configuration)
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyMeli::ApiClient
4
+ include HTTParty
5
+
6
+ API_ROOT_URL = 'https://api.mercadolibre.com'
7
+
8
+ TOKEN_ERRORS = {
9
+ 'invalid_grant' => 'Invalid Grant',
10
+ 'forbidden' => 'Forbidden',
11
+ 'Malformed access_token' => 'Malformed access token'
12
+ }
13
+
14
+ base_uri API_ROOT_URL
15
+ format :json
16
+
17
+ attr_reader :logger, :access_token
18
+
19
+ def initialize(access_token, logger: nil)
20
+ @logger = logger
21
+ @access_token = access_token
22
+ end
23
+
24
+ def get(path, query: {})
25
+ send_request(:get, path, query: query)
26
+ end
27
+
28
+ def post(path, query: {}, body: {})
29
+ send_request(:post, path, query: query, body: body)
30
+ end
31
+
32
+ def put(path, query: {}, body: {})
33
+ send_request(:put, path, query: query, body: body)
34
+ end
35
+
36
+ def delete(path, query: {})
37
+ send_request(:delete, path, query: query)
38
+ end
39
+
40
+ private
41
+
42
+ def send_request(verb, path = '', params = {})
43
+ query = params[:query] || params['query'] || {}
44
+ query[:access_token] = access_token
45
+ self.class.send(verb, path, params.merge(query)).tap do |response|
46
+ logger&.log response
47
+ check_authentication(response)
48
+ end
49
+ end
50
+
51
+ def check_authentication(response)
52
+ response_message = error_message_from_body(response.to_h)
53
+ return if response_message.to_s.empty?
54
+
55
+ TOKEN_ERRORS.keys.each do |key|
56
+ if response_message.include?(key)
57
+ raise EasyMeli::AuthenticationError.new(TOKEN_ERRORS[key], response)
58
+ end
59
+ end
60
+ end
61
+
62
+ def error_message_from_body(body)
63
+ return if body.nil?
64
+ body['error'].to_s.empty? ? body['message'] : body['error']
65
+ end
66
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyMeli::AuthorizationClient
4
+ include HTTParty
5
+
6
+ AUTH_TOKEN_URL = 'https://api.mercadolibre.com/oauth/token'
7
+ AUTH_PATH = '/authorization'
8
+ BASE_AUTH_URLS = {
9
+ AR: 'https://auth.mercadolibre.com.ar',
10
+ BR: 'https://auth.mercadolivre.com.br',
11
+ CO: 'https://auth.mercadolibre.com.co',
12
+ CR: 'https://auth.mercadolibre.com.cr',
13
+ EC: 'https://auth.mercadolibre.com.ec',
14
+ CL: 'https://auth.mercadolibre.cl',
15
+ MX: 'https://auth.mercadolibre.com.mx',
16
+ UY: 'https://auth.mercadolibre.com.uy',
17
+ VE: 'https://auth.mercadolibre.com.ve',
18
+ PA: 'https://auth.mercadolibre.com.pa',
19
+ PE: 'https://auth.mercadolibre.com.pe',
20
+ PT: 'https://auth.mercadolibre.com.pt',
21
+ DO: 'https://auth.mercadolibre.com.do'
22
+ }
23
+
24
+ format :json
25
+
26
+ attr_reader :logger
27
+
28
+ def initialize(logger: nil)
29
+ @logger = logger
30
+ end
31
+
32
+ def self.authorization_url(country_code, redirect_uri)
33
+ params = {
34
+ client_id: EasyMeli.configuration.application_id,
35
+ response_type: 'code',
36
+ redirect_uri: redirect_uri
37
+ }
38
+ HTTParty::Request.new(:get, country_auth_url(country_code), query: params).uri.to_s
39
+ end
40
+
41
+ def create_token(code, redirect_uri)
42
+ query_params = merge_auth_params(
43
+ grant_type: 'authorization_code',
44
+ code: code,
45
+ redirect_uri: redirect_uri
46
+ )
47
+ post_auth(query_params)
48
+ end
49
+
50
+ def refresh_token(refresh_token)
51
+ query_params = merge_auth_params(
52
+ grant_type: 'refresh_token',
53
+ refresh_token: refresh_token
54
+ )
55
+ post_auth(query_params)
56
+ end
57
+
58
+ private
59
+
60
+ def post_auth(params)
61
+ self.class.post(AUTH_TOKEN_URL, query: params).tap do |response|
62
+ logger&.log response
63
+ end
64
+ end
65
+
66
+ def self.country_auth_url(country_code)
67
+ url = BASE_AUTH_URLS[country_code.to_s.upcase.to_sym] ||
68
+ (raise ArgumentError.new('%s is an invalid country code' % country_code))
69
+ [url, AUTH_PATH].join
70
+ end
71
+
72
+ def merge_auth_params(options = {})
73
+ options.merge(
74
+ client_id: EasyMeli.configuration.application_id,
75
+ client_secret: EasyMeli.configuration.secret_key
76
+ )
77
+ end
78
+ end
@@ -0,0 +1,8 @@
1
+ class EasyMeli::Configuration
2
+ attr_accessor :application_id, :secret_key
3
+
4
+ def initialize
5
+ @application_id = nil
6
+ @secret_key = nil
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module EasyMeli
2
+ class Error < StandardError
3
+ attr_reader :response
4
+
5
+ def initialize(message, response)
6
+ @response = response
7
+ super(message)
8
+ end
9
+ end
10
+
11
+ class AuthenticationError < Error; end
12
+ end
@@ -0,0 +1,3 @@
1
+ module EasyMeli
2
+ VERSION = "0.3.0"
3
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_meli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Northam
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - eric@northam.us
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - bin/console
124
+ - bin/setup
125
+ - easy_meli.gemspec
126
+ - lib/easy_meli.rb
127
+ - lib/easy_meli/api_client.rb
128
+ - lib/easy_meli/authorization_client.rb
129
+ - lib/easy_meli/configuration.rb
130
+ - lib/easy_meli/errors.rb
131
+ - lib/easy_meli/version.rb
132
+ homepage: https://github.com/easybroker/easy_meli
133
+ licenses:
134
+ - MIT
135
+ metadata:
136
+ homepage_uri: https://github.com/easybroker/easy_meli
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 3.0.3
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A simple gem to work with MercadoLibre's API
156
+ test_files: []