airbnb_api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0d99109e63b4a91df5ab728034eceb9267442709
4
- data.tar.gz: 67da1aed62d058c5b3cadeed7d7e0e9014f4eee3
2
+ SHA256:
3
+ metadata.gz: 4ec168f185260189dcf64ac1d372f4031462317d2426b32f2818640de90d22bc
4
+ data.tar.gz: 2cf16056ff63550ab17dfe57727af0cb414006507553153edbfd1d421be047af
5
5
  SHA512:
6
- metadata.gz: 46be1eda9036108d6db2af598eb9454fd60e141b60992c813d2949ec8c971422c46917a9bc4d3b301e245359f7db84914c04646f6d247911ed5fff67e023b5e7
7
- data.tar.gz: ab2a5dd8ab0fff2f628736385e3780fb429bd26a6114ad77a5a514e9828d575299ad8b1b195eacd1c5a9cc6effcba0f50c3381ec11980b462ad0729c022c21f3
6
+ metadata.gz: afcf74f219b732ee6e4dece2258a13982fbb74bb8f59ad7cb5e55ebe5d7c79a9cb5ca4fec9daa6dcb25b8fb70c75543de26e8ed498b2014efadf3cd864479276
7
+ data.tar.gz: d0e813ae95489dac63bae440d3ca895baef572eff97359546145cadab46256572d3814e6e743ae082f8b19e6b943944caec78e4934369b16be93c7b2cd804768
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /airbnb_api-*.gem
data/.travis.yml CHANGED
@@ -2,3 +2,14 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.2.4
5
+ env:
6
+ global:
7
+ - CC_TEST_REPORTER_ID=45175f9e10f6665368de72e4049a8c258ac65dacd4704f8cf1e71a5a87835498
8
+
9
+ before_script:
10
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
11
+ - chmod +x ./cc-test-reporter
12
+ - ./cc-test-reporter before-build
13
+
14
+ after_script:
15
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # AirbnbApi
2
2
 
3
- Simple gem to use the Airbnb API. Still work in progress since the Airbnb API is still work in progress.
3
+ [![Build Status](https://travis-ci.org/dennisvdvliet/airbnb_api.svg?branch=master)](https://travis-ci.org/dennisvdvliet/airbnb_api) [![Maintainability](https://api.codeclimate.com/v1/badges/616582587775bcabe3cd/maintainability)](https://codeclimate.com/github/dennisvdvliet/airbnb_api/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/616582587775bcabe3cd/test_coverage)](https://codeclimate.com/github/dennisvdvliet/airbnb_api/test_coverage) [![Gem Version](https://badge.fury.io/rb/airbnb_api.svg)](https://badge.fury.io/rb/airbnb_api)
4
+
5
+ The AirbnbApi ruby library provides an easy way to interact with the Airbnb API. The Airbnb API is being actively developed so please expect this gem to change regularly. The documentation for the Airbnb API can be found [here](https://www.airbnb.com/partner/api-docs/).
4
6
 
5
7
  ## Installation
6
8
 
@@ -20,7 +22,37 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- TODO: Write usage instructions here
25
+ ### Authorization
26
+
27
+ Some endpoints on the Airbnb API can be accessed using the Client ID and Client Secret you obtain by registering your application with Airbnb. However the majority of endpoints require and oAuth token you get from the end user.
28
+
29
+ To obtain an oAuth token redirect a user to the following url:
30
+ ```
31
+ https://www.airbnb.com/oauth2/auth?client_id=:your_client_id&redirect_uri=:your_redirect_url&scope=:your_scope&state=:whatever_you_want
32
+ ```
33
+
34
+ One the user logged in and approved your application they will be redirected to `:your_redirect_url`. This redirect request will include two additional GET parameters: `code` and `scope`
35
+
36
+ In case the user does not approve your application the same URL will be used but the followin GET parameters will be included: `error` and `error_description`
37
+
38
+ The `code` can be used to obtain an `access_token` and a `refresh_token` you can use to perform API requests. You can use the following code to get them.
39
+
40
+ ```ruby
41
+ client = AirbnbApi::Client.new(id: '1234', secret: 'topsecret')
42
+ tokens = client.tokens.create_from_code(code)
43
+ ```
44
+
45
+ You save both the `access_token` and `refresh_token` since you will need the `refresh_token` to obtain a new `access_token` once it expires. Currently the `access_token` expires after 24 hours.
46
+
47
+ Refreshing a token is done as follows:
48
+
49
+ ```ruby
50
+ tokens = client.tokens.refresh_token(refresh_token)
51
+ ```
52
+
53
+
54
+
55
+ ## Supported endpoints
24
56
 
25
57
  ## Development
26
58
 
@@ -30,7 +62,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
30
62
 
31
63
  ## Contributing
32
64
 
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/airbnb_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dennisvdvliet/airbnb_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
66
 
35
67
 
36
68
  ## License
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/airbnb_api.gemspec CHANGED
@@ -1,30 +1,30 @@
1
- # coding: utf-8
1
+
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'airbnb_api/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "airbnb_api"
7
+ spec.name = 'airbnb_api'
8
8
  spec.version = AirbnbApi::VERSION
9
- spec.authors = ["Dennis van der Vliet"]
10
- spec.email = ["dennis.vandervliet@gmail.com"]
9
+ spec.authors = ['Dennis van der Vliet']
10
+ spec.email = ['dennis.vandervliet@gmail.com']
11
11
 
12
- spec.summary = "API Client for Airbnb API"
13
- spec.description = "API Client for Airbnb API"
14
- spec.homepage = "https://github.com/dennisvdvliet/airbnb_api"
15
- spec.license = "MIT"
12
+ spec.summary = 'API Client for Airbnb API'
13
+ spec.description = 'API Client for Airbnb API'
14
+ spec.homepage = 'https://github.com/dennisvdvliet/airbnb_api'
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_dependency "faraday"
23
- spec.add_dependency "faraday_middleware"
20
+ spec.require_paths = ['lib']
24
21
 
25
- spec.add_development_dependency "bundler", "~> 1.12"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec", "~> 3.0"
28
- spec.add_development_dependency "webmock", "~> 3.1"
22
+ spec.add_dependency 'faraday'
23
+ spec.add_dependency 'faraday_middleware'
29
24
 
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'simplecov'
29
+ spec.add_development_dependency 'webmock', '~> 3.1'
30
30
  end
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "airbnb_api"
3
+ require 'bundler/setup'
4
+ require 'airbnb_api'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "airbnb_api"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/lib/airbnb_api.rb CHANGED
@@ -1,9 +1,22 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
+ require 'json'
3
4
 
5
+ # api operations
6
+ require 'airbnb_api/api_operations/find'
7
+ require 'airbnb_api/api_operations/create'
4
8
 
5
- require "airbnb_api/version"
6
- require "airbnb_api/client"
7
- module AirbnbApi
8
- # Your code goes here...
9
- end
9
+ require 'airbnb_api/version'
10
+
11
+ require 'airbnb_api/client'
12
+
13
+ require 'airbnb_api/service'
14
+ require 'airbnb_api/service/listing'
15
+ require 'airbnb_api/service/token'
16
+
17
+ require 'airbnb_api/resource'
18
+ require 'airbnb_api/resource/listing'
19
+ require 'airbnb_api/resource/token'
20
+
21
+ require 'airbnb_api/errors/errors'
22
+ require 'airbnb_api/util/error_handling'
@@ -0,0 +1,9 @@
1
+ module AirbnbApi
2
+ module APIOperations
3
+ module Create
4
+ def create(attributes)
5
+ build @client.post(self.class.path.to_s, attributes.to_json)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AirbnbApi
2
+ module APIOperations
3
+ module Find
4
+ def find(id)
5
+ @client.get("#{self.class.path}/#{id}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -2,9 +2,11 @@ require 'base64'
2
2
 
3
3
  module AirbnbApi
4
4
  class Client
5
- attr_reader :id, :secret
6
- def initialize(id: ,secret:)
7
- @id, @secret = id, secret
5
+ attr_reader :id, :secret, :oauth_token
6
+ def initialize(id:, secret:, oauth_token: nil)
7
+ @id = id
8
+ @secret = secret
9
+ @oauth_token = oauth_token
8
10
  end
9
11
 
10
12
  def version
@@ -12,38 +14,59 @@ module AirbnbApi
12
14
  end
13
15
 
14
16
  def base_url
15
- "https://api.airbnb.com"
17
+ 'https://api.airbnb.com'
16
18
  end
17
19
 
18
20
  def adapter
19
21
  @adapter ||= Faraday.default_adapter
20
22
  end
21
23
 
22
-
23
24
  def http
24
25
  @http ||= Faraday.new(url: base_url) do |faraday|
25
26
  faraday.headers = headers
26
27
  faraday.request :url_encoded
27
28
  faraday.response :json
29
+
30
+ faraday.use AirbnbApi::Util::ErrorHandling
31
+
28
32
  faraday.adapter adapter
29
33
  end
30
34
  end
31
35
 
32
36
  %i[get patch post delete].each do |call|
33
37
  define_method call do |path, options = {}|
34
- http.public_send(call, "/#{version}/#{path}", options).body
38
+ http.public_send(call, "#{version}#{path}", options).body
35
39
  end
36
40
  end
37
41
 
42
+ def oauth_token?
43
+ @oauth_token != nil
44
+ end
45
+
46
+ def listings
47
+ AirbnbApi::Service::Listing.new(self)
48
+ end
49
+
50
+ def tokens
51
+ return AirbnbApi::Service::Token.new(self) if AirbnbApi::Service::Token.can_use_client?(self)
52
+ raise AirbnbApi::Errors::InvalidClient
53
+ end
38
54
 
39
55
  private
40
56
 
41
57
  def headers
42
- {
58
+ default = {
43
59
  'Accept' => 'application/json',
44
- 'Content-Type' => 'application/json',
45
- 'Authorization' => "Basic #{basic_auth_header}"
60
+ 'Content-Type' => 'application/json'
46
61
  }
62
+
63
+ if oauth_token
64
+ default['X-Airbnb-Oauth-Token'] = oauth_token
65
+ default['X-Airbnb-API-Key'] = id
66
+ else
67
+ default['Authorization'] = "Basic #{basic_auth_header}"
68
+ end
69
+ default
47
70
  end
48
71
 
49
72
  def basic_auth_header
@@ -0,0 +1,8 @@
1
+ module AirbnbApi
2
+ module Errors
3
+ class BadRequest < StandardError
4
+ end
5
+ class InvalidClient < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ module AirbnbApi
2
+ module Resource
3
+ def initialize(attributes)
4
+ self.attributes = attributes
5
+ end
6
+
7
+ def attributes=(attributes)
8
+ @attributes = attributes
9
+ attributes.each do |attribute, value|
10
+ if respond_to?(writer = attribute.to_s + '=')
11
+ send(writer, value)
12
+ else
13
+ # self.class.logger.warn "`#{attribute}' is not a listed attribute for #{self.class}"
14
+ end
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ attr_reader :attributes
20
+
21
+ def attributes(attributes)
22
+ attr_accessor(*@attributes = attributes)
23
+ end
24
+
25
+ def build(attributes)
26
+ new(attributes)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ module AirbnbApi
2
+ module Resource
3
+ class Listing
4
+ include AirbnbApi::Resource
5
+ extend AirbnbApi::Resource::ClassMethods
6
+
7
+ attributes %i[
8
+ id
9
+ name
10
+ property_type_category
11
+ room_type_category
12
+ bedrooms
13
+ bathrooms
14
+ beds
15
+ bed_type_category
16
+ amenity_categories
17
+ requested_approval_status_category
18
+ has_availability
19
+ permit_or_tax_id
20
+ apt
21
+ street
22
+ city
23
+ state
24
+ zipcode
25
+ country_code
26
+ lat
27
+ lng
28
+ user_defined_location
29
+ directions
30
+ check_in_time
31
+ check_in_time_ends_at
32
+ check_out_time
33
+ min_nights_input_value
34
+ max_nights_input_value
35
+ person_capacity
36
+ cancellation_policy_category
37
+ listing_currency
38
+ listing_price
39
+ listing_weekend_price
40
+ listing_weekly_price
41
+ listing_monthly_price
42
+ listing_security_deposit
43
+ listing_cleaning_fee
44
+ guests_included
45
+ listing_extra_person_price
46
+ instant_book_welcome_message
47
+ synchronization_category
48
+ ]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ module AirbnbApi
2
+ module Resource
3
+ class Token
4
+ include AirbnbApi::Resource
5
+ extend AirbnbApi::Resource::ClassMethods
6
+
7
+ attributes %i[
8
+ access_token
9
+ expires_at
10
+ refresh_token
11
+ user_id
12
+ ]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module AirbnbApi
2
+ module Service
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def build(attributes)
8
+ resource_class.new attributes
9
+ end
10
+
11
+ module ClassMethods
12
+ attr_accessor :path
13
+ attr_reader :requires_oauth_token
14
+ def set_path(path)
15
+ @path = path
16
+ end
17
+
18
+ def require_oauth_token
19
+ @requires_oauth_token = true
20
+ end
21
+
22
+ def can_use_client?(client)
23
+ requires_oauth_token? == client.oauth_token?
24
+ end
25
+
26
+ def requires_oauth_token?
27
+ @requires_oauth_token == true
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module AirbnbApi
2
+ module Service
3
+ class Listing
4
+ include AirbnbApi::Service
5
+ extend AirbnbApi::Service::ClassMethods
6
+ include AirbnbApi::APIOperations::Find
7
+ include AirbnbApi::APIOperations::Create
8
+
9
+ set_path '/listings'
10
+ require_oauth_token
11
+
12
+ private
13
+
14
+ def resource_class
15
+ AirbnbApi::Resource::Listing
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module AirbnbApi
2
+ module Service
3
+ class Token
4
+ include AirbnbApi::Service
5
+ include AirbnbApi::APIOperations::Create
6
+ include AirbnbApi::APIOperations::Find
7
+ extend AirbnbApi::Service::ClassMethods
8
+
9
+ set_path '/oauth2/authorizations'
10
+
11
+ def create_from_code(code)
12
+ build @client.post(self.class.path.to_s, { code: code }.to_json)
13
+ end
14
+
15
+ def refresh_token(refresh_token)
16
+ build @client.post(self.class.path.to_s, { refresh_token: refresh_token }.to_json)
17
+ end
18
+
19
+ private
20
+
21
+ def resource_class
22
+ AirbnbApi::Resource::Token
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module AirbnbApi
2
+ module Util
3
+ class ErrorHandling < Faraday::Response::Middleware
4
+ ERROR_MAP = {
5
+ 400 => AirbnbApi::Errors::BadRequest,
6
+ 401 => true,
7
+ 402 => true,
8
+ 403 => true,
9
+ 404 => true,
10
+ 405 => true,
11
+ 406 => true,
12
+ 422 => true,
13
+ 429 => true,
14
+ 500 => true
15
+ }.freeze
16
+
17
+ def on_complete(response)
18
+ key = response[:status].to_i
19
+ raise ERROR_MAP[key], response if ERROR_MAP.key? key
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module AirbnbApi
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: airbnb_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis van der Vliet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-26 00:00:00.000000000 Z
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
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'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: webmock
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -113,7 +127,17 @@ files:
113
127
  - bin/console
114
128
  - bin/setup
115
129
  - lib/airbnb_api.rb
130
+ - lib/airbnb_api/api_operations/create.rb
131
+ - lib/airbnb_api/api_operations/find.rb
116
132
  - lib/airbnb_api/client.rb
133
+ - lib/airbnb_api/errors/errors.rb
134
+ - lib/airbnb_api/resource.rb
135
+ - lib/airbnb_api/resource/listing.rb
136
+ - lib/airbnb_api/resource/token.rb
137
+ - lib/airbnb_api/service.rb
138
+ - lib/airbnb_api/service/listing.rb
139
+ - lib/airbnb_api/service/token.rb
140
+ - lib/airbnb_api/util/error_handling.rb
117
141
  - lib/airbnb_api/version.rb
118
142
  homepage: https://github.com/dennisvdvliet/airbnb_api
119
143
  licenses:
@@ -135,9 +159,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
159
  version: '0'
136
160
  requirements: []
137
161
  rubyforge_project:
138
- rubygems_version: 2.4.8
162
+ rubygems_version: 2.7.3
139
163
  signing_key:
140
164
  specification_version: 4
141
165
  summary: API Client for Airbnb API
142
166
  test_files: []
143
- has_rdoc: