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 CHANGED
@@ -1,17 +1,17 @@
1
1
  ---
2
2
  !binary "U0hBMjU2":
3
3
  metadata.gz: !binary |-
4
- YzdjMTRkOGJmN2I4ZmFiYjk0MjdhZmZiNzBmMDc3OGJjZjRiOWYwNzgyY2Vi
5
- MWI1ZGViOTUzYzhkMjZmMDNmMg==
4
+ YmU0OGQ4NmZjMGNkYTJjMTc1NTBmN2IzODkyMTZjMGNlZjA2YWMwYzRhOTY5
5
+ MWYyNGMzMWJhMDQwY2IyOWQ1Mw==
6
6
  data.tar.gz: !binary |-
7
- ZTA5MDNlMmY5MzljMmJhMzhjOTQ2NGE2NTNiMjk1Mjk1ZTZiNmRkNmNkZDgz
8
- NGMyMGJjNzU4MjM0Y2RiODQxMg==
7
+ N2ZiOGRjM2I2N2Y3ZGVlNDFjYjQ0YjY1MzMzOTQwYzViZDM2NDBlOTYzZDI0
8
+ OGNlYzRjYTE3M2U1NzVlNjUxOA==
9
9
  SHA512:
10
10
  metadata.gz: !binary |-
11
- NjVlODUxNzE5ODgwZjQ2NmFkMTMyNGJkMjMxZGUxNmM3M2Y0YWU1NjRjZTdm
12
- OWE0NmRjNzkxYWJjNWZmMDQ5YzQxNTQzNDExNzQ1NWIzYmYzMzJkM2ZmNzE5
13
- ZmI5MDVmY2NiYzZhYzgyZTEwOTJkMjhhZDE0NmZmY2IyOTFjOTI=
11
+ ZTdjN2MyNzY0ZTU1M2ZhYmRlM2Y4MzlmYzc2MmEyN2EzYTcwYTcwMjkxNjVm
12
+ ZDVhMTQ4OTk3MjkxMGRhZGFhYzg0ZTM4NzM0ZjU3MmIyNTUxMTgzMjliOTRm
13
+ OTBiOWNhNjVhMzcwMjg5YzliMWViMGE0NDcyMmNmMzdiZjA5ODk=
14
14
  data.tar.gz: !binary |-
15
- MjFhNjU1YTUyMGY3OTNjNmE0OGU0ODgxNzE3MTU1ZjZjNGQ4NjBkOTZmNDY1
16
- NWZiNTlmZDI3OGMzNGUzYzVmNTQwYjc3ZWFhMDE3ZjNkM2ZlMTkwZjM1Y2Ji
17
- Y2MxYWMxOGU4ZWJkMDRlYmZiNjI2Mjk0NmRkMmY1Zjg2M2NmZTM=
15
+ ODkxNDgxNzE2MjQ3NWYxOGU0YzQ0ZTMyNjA1NWJlNzhjMzk1Y2FkZjlhY2U4
16
+ YTgxYmFjNzcyMTZkZWJmNDI4OTMzYzI5NGYzMDgxYjBmY2Y3MmRhOGYwZjQ4
17
+ NTEwMmVkNmNkNmRmZDg1OTY1Njc0MjAwMWUxMzljMDBkMWUxMjQ=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- allegro-api (0.0.1pre)
4
+ allegro-api (0.0.2pre)
5
5
  json (~> 1.8.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
+ [![Gem Version](https://badge.fury.io/rb/allegro-api.svg)](https://badge.fury.io/rb/allegro-api) [![Build Status](https://travis-ci.org/kmi3c/allegro-api.svg?branch=master)](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 "bundler/setup"
4
- require "allegro"
3
+ require 'bundler/setup'
4
+ require 'pry'
5
+ require 'pry-stack_explorer'
6
+ require 'allegro-api'
5
7
 
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"
8
+ # TODO add ENV support and auto init of a client?
9
+ require 'irb'
14
10
  IRB.start(__FILE__)
@@ -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({client_id: client_id, secret: secret})
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 authorize
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
- { grant_type: 'client_credentials', method: :post }
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 authorized?
26
- @authorized
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(params = {})
8
+ def initialize(_options = {})
9
9
  @logger = Logger.new(STDOUT)
10
- @options = params
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
- raise e
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
- case content_type
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
- def default_params
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
@@ -1,3 +1,3 @@
1
1
  module Allegro
2
- VERSION = "0.0.1pre"
2
+ VERSION = "0.0.2pre"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
+ require 'base64'
3
4
  require 'allegro/version'
4
5
  require 'allegro/client'
5
6
  require 'allegro/http'
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.1pre
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