allegro-api 0.0.1pre → 0.0.2pre
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.
- checksums.yaml +10 -10
- data/Gemfile.lock +1 -1
- data/README.md +3 -1
- data/bin/console +6 -10
- data/lib/allegro/client.rb +40 -7
- data/lib/allegro/http.rb +8 -15
- data/lib/allegro/version.rb +1 -1
- data/lib/{allegro.rb → allegro-api.rb} +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMjU2":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
5
|
-
|
4
|
+
YmU0OGQ4NmZjMGNkYTJjMTc1NTBmN2IzODkyMTZjMGNlZjA2YWMwYzRhOTY5
|
5
|
+
MWYyNGMzMWJhMDQwY2IyOWQ1Mw==
|
6
6
|
data.tar.gz: !binary |-
|
7
|
-
|
8
|
-
|
7
|
+
N2ZiOGRjM2I2N2Y3ZGVlNDFjYjQ0YjY1MzMzOTQwYzViZDM2NDBlOTYzZDI0
|
8
|
+
OGNlYzRjYTE3M2U1NzVlNjUxOA==
|
9
9
|
SHA512:
|
10
10
|
metadata.gz: !binary |-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
ZTdjN2MyNzY0ZTU1M2ZhYmRlM2Y4MzlmYzc2MmEyN2EzYTcwYTcwMjkxNjVm
|
12
|
+
ZDVhMTQ4OTk3MjkxMGRhZGFhYzg0ZTM4NzM0ZjU3MmIyNTUxMTgzMjliOTRm
|
13
|
+
OTBiOWNhNjVhMzcwMjg5YzliMWViMGE0NDcyMmNmMzdiZjA5ODk=
|
14
14
|
data.tar.gz: !binary |-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
ODkxNDgxNzE2MjQ3NWYxOGU0YzQ0ZTMyNjA1NWJlNzhjMzk1Y2FkZjlhY2U4
|
16
|
+
YTgxYmFjNzcyMTZkZWJmNDI4OTMzYzI5NGYzMDgxYjBmY2Y3MmRhOGYwZjQ4
|
17
|
+
NTEwMmVkNmNkNmRmZDg1OTY1Njc0MjAwMWUxMzljMDBkMWUxMjQ=
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
[](https://badge.fury.io/rb/allegro-api) [](https://travis-ci.org/kmi3c/allegro-api)
|
1
2
|
# Allegro API
|
2
3
|
|
3
|
-
Simple Ruby REST API client for Allegro API.
|
4
|
+
Simple Ruby REST API client for Allegro API for device flow.
|
4
5
|
|
5
6
|
https://developer.allegro.pl/about/#rest-api
|
7
|
+
https://developer.allegro.pl/swagger.yaml
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
data/bin/console
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pry'
|
5
|
+
require 'pry-stack_explorer'
|
6
|
+
require 'allegro-api'
|
5
7
|
|
6
|
-
#
|
7
|
-
|
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"
|
8
|
+
# TODO add ENV support and auto init of a client?
|
9
|
+
require 'irb'
|
14
10
|
IRB.start(__FILE__)
|
data/lib/allegro/client.rb
CHANGED
@@ -1,29 +1,49 @@
|
|
1
1
|
module Allegro
|
2
2
|
|
3
3
|
AUTH_URI = 'https://allegro.pl/auth/oauth'
|
4
|
-
API_URI = 'https://api.allegro.pl
|
4
|
+
API_URI = 'https://api.allegro.pl'
|
5
5
|
|
6
6
|
class Client
|
7
7
|
|
8
8
|
attr_reader :http_agent
|
9
9
|
|
10
10
|
def initialize(client_id, secret, _options = {})
|
11
|
-
@http_agent = Http::Agent.new(
|
11
|
+
@http_agent = Http::Agent.new(_options)
|
12
12
|
@authorized = false
|
13
|
-
authorize
|
13
|
+
authorize(client_id, secret)
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def authorized?
|
17
|
+
@authorized
|
18
|
+
end
|
19
|
+
|
20
|
+
def authorize(client_id, secret)
|
21
|
+
auth_token = Base64.strict_encode64("#{client_id}:#{secret}")
|
17
22
|
response = @http_agent.fetch(
|
18
23
|
auth_url('token'),
|
19
|
-
{
|
24
|
+
{
|
25
|
+
method: :post,
|
26
|
+
grant_type: 'client_credentials',
|
27
|
+
headers: default_headers.merge({
|
28
|
+
authorization: "Basic #{auth_token}"
|
29
|
+
})
|
30
|
+
}
|
20
31
|
)
|
21
32
|
@access_token = response['access_token']
|
33
|
+
@token_type = response['token_type']
|
22
34
|
@authorized = true if response && @access_token
|
23
35
|
end
|
24
36
|
|
25
|
-
def
|
26
|
-
@
|
37
|
+
def get(resource, params)
|
38
|
+
@http_agent.fetch(
|
39
|
+
api_url(resource), default_params.merge(params)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def search(params)
|
44
|
+
@http_agent.fetch(
|
45
|
+
api_url('offers/listing'), default_params.merge(params)
|
46
|
+
)
|
27
47
|
end
|
28
48
|
|
29
49
|
def api_url(url)
|
@@ -34,5 +54,18 @@ module Allegro
|
|
34
54
|
[AUTH_URI, url.to_s].join('/')
|
35
55
|
end
|
36
56
|
|
57
|
+
def default_params
|
58
|
+
{
|
59
|
+
method: :get, headers: default_headers
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def default_headers
|
64
|
+
{
|
65
|
+
accept: 'application/vnd.allegro.public.v1+json',
|
66
|
+
authorization: "#{@token_type} #{@access_token}"
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
37
70
|
end
|
38
71
|
end
|
data/lib/allegro/http.rb
CHANGED
@@ -5,16 +5,14 @@ module Http
|
|
5
5
|
attr_accessor :options, :logger
|
6
6
|
attr_reader :authorized
|
7
7
|
|
8
|
-
def initialize(
|
8
|
+
def initialize(_options = {})
|
9
9
|
@logger = Logger.new(STDOUT)
|
10
|
-
@options =
|
11
|
-
@authorized = false
|
10
|
+
@options = _options
|
12
11
|
end
|
13
12
|
|
14
13
|
# TODO: split to more atomic methods fe: handle_error, prepare request, etc.
|
15
14
|
def fetch(uri, params = {})
|
16
15
|
uri = URI(uri)
|
17
|
-
params.merge!(default_params)
|
18
16
|
body = []
|
19
17
|
begin
|
20
18
|
session = Net::HTTP.new(uri.host, uri.port)
|
@@ -39,12 +37,14 @@ module Http
|
|
39
37
|
when Net::HTTPRedirection
|
40
38
|
raise StandardError, response.message
|
41
39
|
else
|
40
|
+
body = parse_body(body, response.content_type)
|
41
|
+
return body if body.is_a?(Hash)
|
42
42
|
raise StandardError, response.message
|
43
43
|
end
|
44
44
|
rescue StandardError => e
|
45
45
|
logger.error e.message
|
46
46
|
body = []
|
47
|
-
|
47
|
+
raise e
|
48
48
|
end
|
49
49
|
body
|
50
50
|
end
|
@@ -64,22 +64,15 @@ module Http
|
|
64
64
|
request
|
65
65
|
end
|
66
66
|
headers.each{|k,v| request[k.to_s] = v }
|
67
|
-
request.basic_auth(options[:client_id], options[:secret])
|
68
67
|
request
|
69
68
|
end
|
70
69
|
|
71
70
|
def parse_body(body, content_type)
|
72
|
-
|
73
|
-
when /application\/(vnd\.allegro\.public\.v\d+\+json|json)/
|
74
|
-
JSON.parse(body)
|
75
|
-
else
|
76
|
-
body
|
77
|
-
end
|
71
|
+
json?(content_type) ? JSON.parse(body) : body
|
78
72
|
end
|
79
73
|
|
80
|
-
|
81
|
-
|
82
|
-
{ method: :get, headers: { accept: 'application/vnd.allegro.public.v1+json'}}
|
74
|
+
def json?(content_type)
|
75
|
+
!!content_type.match(/application\/(vnd\.allegro\.public\.v\d+\+json|json)/)
|
83
76
|
end
|
84
77
|
end
|
85
78
|
end
|
data/lib/allegro/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: allegro-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paweł Adamski
|
@@ -112,7 +112,7 @@ files:
|
|
112
112
|
- allegro-api.gemspec
|
113
113
|
- bin/console
|
114
114
|
- bin/setup
|
115
|
-
- lib/allegro.rb
|
115
|
+
- lib/allegro-api.rb
|
116
116
|
- lib/allegro/client.rb
|
117
117
|
- lib/allegro/http.rb
|
118
118
|
- lib/allegro/version.rb
|