is24 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +56 -2
  2. data/lib/is24.rb +72 -12
  3. data/lib/is24.rb~ +72 -12
  4. data/lib/is24/version.rb +1 -1
  5. metadata +2 -2
data/README.md CHANGED
@@ -22,7 +22,7 @@ client = Is24::Api.new(
22
22
  )
23
23
  ```
24
24
 
25
- Get expose for object
25
+ Get exposé for object
26
26
 
27
27
  ```ruby
28
28
  expose = client.expose(ID)
@@ -34,7 +34,61 @@ puts expose
34
34
  puts expose.contactDetails.address
35
35
  ```
36
36
 
37
- ### Contributing to is24
37
+ ## Authentication
38
+
39
+ IS24 uses oauth v1.0.
40
+
41
+ ### Step 1: Get a request token
42
+
43
+ Set up a new client
44
+
45
+ ```ruby
46
+ client = Is24::Api.new(
47
+ consumer_key: YOUR_CONSUMER_KEY,
48
+ consumer_secret: YOUR_CONSUMER_SECRET
49
+ )
50
+ ```
51
+
52
+ and request a request_token providing a callback uri to your app
53
+
54
+ ```ruby
55
+ request_token = client.request_token(YOUR_CALLBACK_URI)
56
+
57
+ ## returns
58
+ # {
59
+ # :oauth_token => OAUTH_TOKEN,
60
+ # :oauth_token_secret => OAUTH_TOKEN_SECRET,
61
+ # :redirect_uri => "http://rest.immobilienscout24.de/restapi/security/oauth/confirm_access?oauth_token=OAUTH_TOKEN"
62
+ # }
63
+ ```
64
+
65
+ Redirect your user to the redirect_uri to authorize your request.
66
+
67
+ ### Step 2: Request access token
68
+
69
+ Once the user has authorized your request you'll receive a get request to your
70
+ callback. Parse the response params and grab your tokens:
71
+
72
+ ```ruby
73
+ tokens = client.request_access_token(
74
+ oauth_token: OAUTH_TOKEN,
75
+ oauth_token_secret: OAUTH_TOKEN_SECRET,
76
+ oauth_verifier: OAUTH_VERIFIER )
77
+
78
+ ## returns
79
+ # {
80
+ # :oauth_token=> OAUTH_TOKEN,
81
+ # :oauth_token_secret=> OAUTH_TOKEN_SECRET
82
+ # }
83
+ ```
84
+
85
+ Until the user revokes permission to your app, the access_token is valid for an unlimited time. Store the tokens and reuse them for subsequent calls to restricted resources of the REST API.
86
+
87
+ ## TODO
88
+
89
+ * Error handling ;)
90
+
91
+ ## Contributing to is24
38
92
 
39
93
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
40
94
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
@@ -2,22 +2,59 @@ require 'is24/logger'
2
2
  require 'faraday'
3
3
  require 'faraday_middleware'
4
4
  require 'faraday_middleware/response/mashify'
5
+ require 'cgi'
5
6
 
6
7
  module Is24
7
8
  class Api
8
9
  include Is24::Logger
9
10
 
10
11
  API_ENDPOINT = "http://rest.immobilienscout24.de/restapi/api/search/v1.0/"
12
+ API_AUTHORIZATION_ENDPOINT = "http://rest.immobilienscout24.de/restapi/security/"
11
13
 
12
14
  def initialize( options = {} )
13
- logger "Initialized Bnerd IS24 with options #{options}"
15
+ logger "Initialized b'nerd IS24 with options #{options}"
14
16
 
15
17
  @token = options[:token] || nil
16
18
  @secret = options[:secret] || nil
17
19
  @consumer_secret = options[:consumer_secret] || nil
18
20
  @consumer_key = options[:consumer_key] || nil
19
21
 
20
- raise "Invalid Credentials" if @secret.nil? || @token.nil?
22
+ raise "Missing Credentials!" if @consumer_secret.nil? || @consumer_key.nil?
23
+ end
24
+
25
+ def request_token( callback_uri )
26
+ # TODO error handling
27
+ response = connection(:authorization, callback_uri).get("oauth/request_token")
28
+
29
+ body = response.body.split('&')
30
+ response = {
31
+ :oauth_token => CGI::unescape(body[0].split("=")[1]),
32
+ :oauth_token_secret => CGI::unescape(body[1].split("=")[1]),
33
+ :redirect_uri => "http://rest.immobilienscout24.de/restapi/security/oauth/confirm_access?#{body[0]}"
34
+ }
35
+ end
36
+
37
+ def request_access_token( params = {} )
38
+ # TODO error handling
39
+ @oauth_verifier = params[:oauth_verifier]
40
+ @token = params[:oauth_token]
41
+ @secret = params[:oauth_token_secret]
42
+
43
+ response = connection(:authorization).get("oauth/access_token")
44
+ puts response.inspect
45
+ body = response.body.split('&')
46
+
47
+ response = {
48
+ :oauth_token => body[0].split('=')[1],
49
+ :oauth_token_secret => CGI::unescape(body[1].split('=')[1]),
50
+ }
51
+
52
+ # set credentials in client
53
+ @token = response[:oauth_token]
54
+ @token_secret = response[:oauth_token_secret]
55
+
56
+ # return access token and secret
57
+ response
21
58
  end
22
59
 
23
60
  def search(options)
@@ -40,23 +77,46 @@ module Is24
40
77
 
41
78
  protected
42
79
 
43
- def connection
44
- Faraday::Connection.new(
80
+ def connection(connection_type = :default, callback_uri = nil)
81
+
82
+ # set request defaults
83
+ defaults = {
45
84
  :url => API_ENDPOINT,
46
85
  :accept => 'application/json',
47
86
  :headers => {
48
87
  :accept => 'application/json',
49
- :user_agent => 'b\'nerd .media IS24 Ruby Client'} ) do |builder|
50
- builder.request :oauth, {
51
- :consumer_key => @consumer_key,
52
- :consumer_secret => @consumer_secret,
53
- :token => @token,
54
- :token_secret => @secret
55
- }
88
+ :user_agent => 'b\'nerd .media IS24 Ruby Client'}
89
+ }
90
+
91
+ defaults.merge!( {
92
+ :url => API_AUTHORIZATION_ENDPOINT
93
+ } ) if connection_type =~ /authorization/i
94
+
95
+ # define oauth credentials
96
+ oauth = {
97
+ :consumer_key => @consumer_key,
98
+ :consumer_secret => @consumer_secret,
99
+ :token => @token,
100
+ :token_secret => @secret
101
+ }
102
+
103
+ # merge callback_uri if present
104
+ oauth.merge!( {
105
+ :callback => callback_uri
106
+ } ) if connection_type =~ /authorization/i && callback_uri
107
+
108
+ # merge verifier if present
109
+ oauth.merge!( {
110
+ :verifier => @oauth_verifier
111
+ } ) if connection_type =~ /authorization/i && @oauth_verifier
112
+
113
+ Faraday::Connection.new( defaults ) do |builder|
114
+ builder.request :oauth, oauth
56
115
  builder.response :mashify
57
- builder.response :json
116
+ builder.response :json unless connection_type =~ /authorization/i
58
117
  builder.adapter Faraday.default_adapter
59
118
  end
60
119
  end
120
+
61
121
  end
62
122
  end
@@ -2,22 +2,59 @@ require 'is24/logger'
2
2
  require 'faraday'
3
3
  require 'faraday_middleware'
4
4
  require 'faraday_middleware/response/mashify'
5
+ require 'cgi'
5
6
 
6
7
  module Is24
7
8
  class Api
8
9
  include Is24::Logger
9
10
 
10
11
  API_ENDPOINT = "http://rest.immobilienscout24.de/restapi/api/search/v1.0/"
12
+ API_AUTHORIZATION_ENDPOINT = "http://rest.immobilienscout24.de/restapi/security/"
11
13
 
12
14
  def initialize( options = {} )
13
- logger "Initialized Bnerd IS24 with options #{options}"
15
+ logger "Initialized b'nerd IS24 with options #{options}"
14
16
 
15
17
  @token = options[:token] || nil
16
18
  @secret = options[:secret] || nil
17
19
  @consumer_secret = options[:consumer_secret] || nil
18
20
  @consumer_key = options[:consumer_key] || nil
19
21
 
20
- raise "Invalid Credentials" if @secret.nil? || @token.nil?
22
+ raise "Missing Credentials!" if @consumer_secret.nil? || @consumer_key.nil?
23
+ end
24
+
25
+ def request_token( callback_uri )
26
+ # TODO error handling
27
+ response = connection(:authorization, callback_uri).get("oauth/request_token")
28
+
29
+ body = response.body.split('&')
30
+ response = {
31
+ :oauth_token => CGI::unescape(body[0].split("=")[1]),
32
+ :oauth_token_secret => CGI::unescape(body[1].split("=")[1]),
33
+ :redirect_uri => "http://rest.immobilienscout24.de/restapi/security/oauth/confirm_access?#{body[0]}"
34
+ }
35
+ end
36
+
37
+ def request_access_token( params = {} )
38
+ # TODO error handling
39
+ @oauth_verifier = params[:oauth_verifier]
40
+ @token = params[:oauth_token]
41
+ @secret = params[:oauth_token_secret]
42
+
43
+ response = connection(:authorization).get("oauth/access_token")
44
+ puts response.inspect
45
+ body = response.body.split('&')
46
+
47
+ response = {
48
+ :oauth_token => body[0].split('=')[1],
49
+ :oauth_token_secret => CGI::unescape(body[1].split('=')[1]),
50
+ }
51
+
52
+ # set credentials in client
53
+ @token = response[:oauth_token]
54
+ @token_secret = response[:oauth_token_secret]
55
+
56
+ # return access token and secret
57
+ response
21
58
  end
22
59
 
23
60
  def search(options)
@@ -40,23 +77,46 @@ module Is24
40
77
 
41
78
  protected
42
79
 
43
- def connection
44
- Faraday::Connection.new(
80
+ def connection(connection_type = :default, callback_uri = nil)
81
+
82
+ # set request defaults
83
+ defaults = {
45
84
  :url => API_ENDPOINT,
46
85
  :accept => 'application/json',
47
86
  :headers => {
48
87
  :accept => 'application/json',
49
- :user_agent => 'b\'nerd .media IS24 Ruby Client'} ) do |builder|
50
- builder.request :oauth, {
51
- :consumer_key => @consumer_key,
52
- :consumer_secret => @consumer_secret,
53
- :token => @token,
54
- :token_secret => @secret
55
- }
88
+ :user_agent => 'b\'nerd .media IS24 Ruby Client'}
89
+ }
90
+
91
+ defaults.merge!( {
92
+ :url => API_AUTHORIZATION_ENDPOINT
93
+ } ) if connection_type =~ /authorization/i
94
+
95
+ # define oauth credentials
96
+ oauth = {
97
+ :consumer_key => @consumer_key,
98
+ :consumer_secret => @consumer_secret,
99
+ :token => @token,
100
+ :token_secret => @secret
101
+ }
102
+
103
+ # merge callback_uri if present
104
+ oauth.merge!( {
105
+ :callback => callback_uri
106
+ } ) if connection_type =~ /authorization/i && callback_uri
107
+
108
+ # merge verifier if present
109
+ oauth.merge!( {
110
+ :verifier => @oauth_verifier
111
+ } ) if connection_type =~ /authorization/i && @oauth_verifier
112
+
113
+ Faraday::Connection.new( defaults ) do |builder|
114
+ builder.request :oauth, oauth
56
115
  builder.response :mashify
57
- builder.response :json
116
+ builder.response :json unless connection_type =~ /authorization/i
58
117
  builder.adapter Faraday.default_adapter
59
118
  end
60
119
  end
120
+
61
121
  end
62
122
  end
@@ -1,3 +1,3 @@
1
1
  module Is24
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is24
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json