easy_broker 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e776eb6519e1c3bfcb77a951c47b78d89d425665c9400149ed75b792e6fa7cef
4
+ data.tar.gz: 7297f553a3f882198d86cc1857750795fefb76a2afdddb3a8fefd15f2a704d74
5
+ SHA512:
6
+ metadata.gz: 19205db9c8dde2f1e2024ddc2c1be72e2bdee469dd6a5ff7403fd7f65ac21d3714e4599fb40ac2c5e4f52e4394d768f285ead5bf1260bd8f6f22a190515d90e9
7
+ data.tar.gz: 3264d842b5195beca8060faab72b2bdb53991680a0230877ab9ce01eaf333c229590a5e62c359f9019e7b259b4ff8accdfce6d21c5cfe2e533a0344ddd80411d
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ /vendor
11
+ .DS_Store
12
+ *.gem
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 0.1.1
4
+ * Initial release
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_broker.gemspec
6
+ gemspec
@@ -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.
@@ -0,0 +1,75 @@
1
+ # EasyBroker
2
+
3
+ A gem that makes it easy to work with the [EasyBroker API](https://api.easybroker.com/api/playground).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'easy_broker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install easy_broker
20
+
21
+ ## Usage
22
+
23
+ Configure the gem with your application ID and secret key. This is the first draft which
24
+ just offers basic support for finding and searching properties.
25
+
26
+ ```ruby
27
+ EasyBroker.configure do |config|
28
+ config.api_key = 'your_app_key'
29
+
30
+ # Optionally change the root API URL
31
+ # config.api_root_url = 'https://www.myebproxy.com/api/v1/'
32
+ end
33
+ ```
34
+
35
+ Then in your app use the _client_ to access the API.
36
+
37
+ ```ruby
38
+ client = EasyBroker.client
39
+
40
+ client.properties.find('EB-123')
41
+ results = client.properties.search # results is an enumerator
42
+
43
+ # Print the first page of up to 20 results
44
+ results.each do |property|
45
+ puts property.public_id
46
+ end
47
+
48
+ # Or use find_each to automatically iterate over all the results on each page
49
+ results.find_each do |property|
50
+ puts property.public_id
51
+ end
52
+
53
+ # Search for only published properties
54
+ client.properties.search(search: { statuses: [:published] } )
55
+ ```
56
+
57
+ You can also pass a logger to log any methods that make remote calls. 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.
58
+
59
+ ```ruby
60
+ EasyBroker.api_client(logger: my_logger)
61
+ ```
62
+
63
+ ## Development
64
+
65
+ 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.
66
+
67
+ 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).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/easybroker/easy_broker_api.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy_broker"
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__)
@@ -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
@@ -0,0 +1,39 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "easy_broker/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "easy_broker"
7
+ spec.version = EasyBroker::VERSION
8
+ spec.authors = ["Eric Northam"]
9
+ spec.email = ["eric@northam.us"]
10
+
11
+ spec.summary = %q{A gem to work with EasyBroker's API}
12
+ spec.homepage = "https://github.com/easybroker/easybroker_gem/"
13
+ spec.license = "MIT"
14
+
15
+ if spec.respond_to?(:metadata)
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ else
18
+ raise "RubyGems 2.0 or newer is required to protect against " \
19
+ "public gem pushes."
20
+ end
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "httparty", "~> 0.18"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.17"
34
+ spec.add_development_dependency "rake", "~> 13.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "webmock"
38
+ spec.add_development_dependency "mocha"
39
+ end
@@ -0,0 +1,29 @@
1
+ require 'httparty'
2
+
3
+ require 'easy_broker/version'
4
+ require 'easy_broker/constants'
5
+ require 'easy_broker/errors'
6
+ require 'easy_broker/configuration'
7
+ require 'easy_broker/api_client'
8
+ require 'easy_broker/public_client'
9
+ require 'easy_broker/paginated_response'
10
+ require 'easy_broker/query'
11
+ require 'easy_broker/properties'
12
+
13
+ module EasyBroker
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration)
20
+ end
21
+
22
+ def self.api_client(logger: nil)
23
+ EasyBroker::ApiClient.new(logger: logger)
24
+ end
25
+
26
+ def self.client(logger: nil)
27
+ EasyBroker::PublicClient.new(logger: logger)
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyBroker::ApiClient
4
+ include HTTParty
5
+
6
+ format :json
7
+
8
+ attr_reader :logger
9
+
10
+ def initialize(logger: nil)
11
+ self.class.base_uri EasyBroker.configuration.api_root_url
12
+ self.class.headers EasyBroker::DEFAULT_HEADERS.merge(
13
+ EasyBroker::AUTHORIZATION_HEADER => EasyBroker.configuration.api_key
14
+ )
15
+ @logger = logger
16
+ end
17
+
18
+ def get(path, query: {})
19
+ send_request(:get, path, query: query)
20
+ end
21
+
22
+ def post(path, query: {}, body: {})
23
+ send_request(:post, path, query: query, body: body)
24
+ end
25
+
26
+ def put(path, query: {}, body: {})
27
+ send_request(:put, path, query: query, body: body)
28
+ end
29
+
30
+ def delete(path, query: {})
31
+ send_request(:delete, path, query: query)
32
+ end
33
+
34
+ private
35
+
36
+ def send_request(verb, path = '', params = {})
37
+ query = params[:query] || params['query'] || {}
38
+
39
+ self.class.send(verb, path, params.merge(query)).tap do |response|
40
+ check_errors(response)
41
+ logger&.log response
42
+ end
43
+ end
44
+
45
+ def check_errors(response)
46
+ raise EasyBroker::AuthenticationError.new(response) if response.code == 401
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ class EasyBroker::Configuration
2
+ attr_accessor :api_key, :api_root_url
3
+
4
+ def initialize
5
+ @api_key = nil
6
+ @api_root_url = EasyBroker::DEFAULT_API_ROOT_URL
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module EasyBroker
2
+ USER_AGENT = "EasyBroker-%s" % VERSION
3
+ DEFAULT_HEADERS = {
4
+ 'Content-Type' => 'application/json',
5
+ 'Accept' => 'application/json',
6
+ 'User-Agent' => USER_AGENT
7
+ }
8
+ DEFAULT_API_ROOT_URL = 'https://www.easybroker.com/api/v1'
9
+ AUTHORIZATION_HEADER = 'X-Authorization'
10
+ end
@@ -0,0 +1,16 @@
1
+ module EasyBroker
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
12
+ def initialize(response)
13
+ super('Invalid API Key or missing permissions', response)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ class EasyBroker::PaginatedResponse
2
+ include Enumerable
3
+
4
+ attr_reader :query, :response
5
+
6
+ def initialize(query)
7
+ @query = query
8
+ @response = query.get
9
+ end
10
+
11
+ def total
12
+ pagination&.total
13
+ end
14
+
15
+ def limit
16
+ pagination&.limit
17
+ end
18
+
19
+ def page
20
+ pagination&.page
21
+ end
22
+
23
+ def next_page
24
+ next_page_number = page.nil? ? 1 : page + 1
25
+
26
+ if next_page_number * limit <= total
27
+ @response = query.get(next_page_number)
28
+ end
29
+ end
30
+
31
+ def find_each
32
+ return self.to_enum(:find_each) unless block_given?
33
+ loop do
34
+ each do |object|
35
+ yield object
36
+ end
37
+ break unless next_page
38
+ end
39
+ end
40
+
41
+ def each
42
+ return response.content.to_enum(:each) unless block_given?
43
+
44
+ response.content.each do |object|
45
+ yield object
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def pagination
52
+ response&.pagination
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyBroker::Properties
4
+ ENDPOINT = '/properties'
5
+
6
+ attr_reader :api_client
7
+
8
+ def initialize(api_client)
9
+ @api_client = api_client
10
+ end
11
+
12
+ def find(public_id)
13
+ response = api_client.get("#{ENDPOINT}/#{public_id}")
14
+ JSON.parse(response.body, object_class: OpenStruct)
15
+ end
16
+
17
+ def search(query = {})
18
+ stored_query = EasyBroker::Query.new(api_client, ENDPOINT, query)
19
+ EasyBroker::PaginatedResponse.new(stored_query)
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyBroker::PublicClient
4
+ attr_reader :api_client
5
+
6
+ def initialize(logger: nil)
7
+ @api_client = EasyBroker::ApiClient.new(logger: logger)
8
+ end
9
+
10
+ def properties
11
+ EasyBroker::Properties.new(api_client)
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyBroker::Query
4
+ attr_reader :api_client, :endpoint, :query_params
5
+
6
+ def initialize(api_client, endpoint, query_params)
7
+ @api_client = api_client
8
+ @endpoint = endpoint
9
+ @query_params = query_params
10
+ end
11
+
12
+ def get(page = 1)
13
+ query_params[:page] = page
14
+ response = api_client.get(endpoint, query: query_params)
15
+ JSON.parse(response.body, object_class: OpenStruct)
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module EasyBroker
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_broker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Northam
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-18 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
+ - CHANGELOG.md
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - easy_broker.gemspec
127
+ - lib/easy_broker.rb
128
+ - lib/easy_broker/api_client.rb
129
+ - lib/easy_broker/configuration.rb
130
+ - lib/easy_broker/constants.rb
131
+ - lib/easy_broker/errors.rb
132
+ - lib/easy_broker/paginated_response.rb
133
+ - lib/easy_broker/properties.rb
134
+ - lib/easy_broker/public_client.rb
135
+ - lib/easy_broker/query.rb
136
+ - lib/easy_broker/version.rb
137
+ homepage: https://github.com/easybroker/easybroker_gem/
138
+ licenses:
139
+ - MIT
140
+ metadata:
141
+ homepage_uri: https://github.com/easybroker/easybroker_gem/
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.0.3
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: A gem to work with EasyBroker's API
161
+ test_files: []