gecko-ruby 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +9 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGELOG.md +41 -0
  6. data/CONTRIBUTING.md +0 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +144 -0
  10. data/Rakefile +19 -0
  11. data/gecko-ruby.gemspec +34 -0
  12. data/generate.thor +81 -0
  13. data/lib/gecko-ruby.rb +1 -0
  14. data/lib/gecko.rb +31 -0
  15. data/lib/gecko/client.rb +132 -0
  16. data/lib/gecko/helpers/association_helper.rb +69 -0
  17. data/lib/gecko/helpers/inspection_helper.rb +51 -0
  18. data/lib/gecko/helpers/record_helper.rb +29 -0
  19. data/lib/gecko/helpers/serialization_helper.rb +61 -0
  20. data/lib/gecko/helpers/validation_helper.rb +91 -0
  21. data/lib/gecko/record/account.rb +66 -0
  22. data/lib/gecko/record/address.rb +33 -0
  23. data/lib/gecko/record/base.rb +66 -0
  24. data/lib/gecko/record/base_adapter.rb +365 -0
  25. data/lib/gecko/record/company.rb +41 -0
  26. data/lib/gecko/record/contact.rb +25 -0
  27. data/lib/gecko/record/currency.rb +23 -0
  28. data/lib/gecko/record/exceptions.rb +15 -0
  29. data/lib/gecko/record/fulfillment.rb +42 -0
  30. data/lib/gecko/record/fulfillment_line_item.rb +26 -0
  31. data/lib/gecko/record/image.rb +38 -0
  32. data/lib/gecko/record/invoice.rb +29 -0
  33. data/lib/gecko/record/invoice_line_item.rb +18 -0
  34. data/lib/gecko/record/location.rb +23 -0
  35. data/lib/gecko/record/order.rb +50 -0
  36. data/lib/gecko/record/order_line_item.rb +35 -0
  37. data/lib/gecko/record/product.rb +27 -0
  38. data/lib/gecko/record/purchase_order.rb +43 -0
  39. data/lib/gecko/record/purchase_order_line_item.rb +29 -0
  40. data/lib/gecko/record/tax_type.rb +21 -0
  41. data/lib/gecko/record/user.rb +44 -0
  42. data/lib/gecko/record/variant.rb +96 -0
  43. data/lib/gecko/version.rb +3 -0
  44. data/test/client_test.rb +29 -0
  45. data/test/fixtures/vcr_cassettes/accounts.yml +57 -0
  46. data/test/fixtures/vcr_cassettes/accounts_current.yml +57 -0
  47. data/test/fixtures/vcr_cassettes/addresses.yml +68 -0
  48. data/test/fixtures/vcr_cassettes/addresses_count.yml +58 -0
  49. data/test/fixtures/vcr_cassettes/companies.yml +62 -0
  50. data/test/fixtures/vcr_cassettes/companies_count.yml +58 -0
  51. data/test/fixtures/vcr_cassettes/contacts.yml +60 -0
  52. data/test/fixtures/vcr_cassettes/contacts_count.yml +58 -0
  53. data/test/fixtures/vcr_cassettes/currencies.yml +62 -0
  54. data/test/fixtures/vcr_cassettes/currencies_count.yml +58 -0
  55. data/test/fixtures/vcr_cassettes/fulfillments.yml +59 -0
  56. data/test/fixtures/vcr_cassettes/fulfillments_count.yml +58 -0
  57. data/test/fixtures/vcr_cassettes/images.yml +59 -0
  58. data/test/fixtures/vcr_cassettes/images_count.yml +58 -0
  59. data/test/fixtures/vcr_cassettes/invoice_line_items.yml +63 -0
  60. data/test/fixtures/vcr_cassettes/invoice_line_items_count.yml +62 -0
  61. data/test/fixtures/vcr_cassettes/invoices.yml +63 -0
  62. data/test/fixtures/vcr_cassettes/invoices_count.yml +62 -0
  63. data/test/fixtures/vcr_cassettes/locations.yml +65 -0
  64. data/test/fixtures/vcr_cassettes/locations_count.yml +58 -0
  65. data/test/fixtures/vcr_cassettes/order_line_items.yml +63 -0
  66. data/test/fixtures/vcr_cassettes/order_line_items_count.yml +62 -0
  67. data/test/fixtures/vcr_cassettes/orders.yml +62 -0
  68. data/test/fixtures/vcr_cassettes/orders_count.yml +58 -0
  69. data/test/fixtures/vcr_cassettes/products.yml +79 -0
  70. data/test/fixtures/vcr_cassettes/products_count.yml +58 -0
  71. data/test/fixtures/vcr_cassettes/products_new_invalid.yml +54 -0
  72. data/test/fixtures/vcr_cassettes/products_new_valid.yml +58 -0
  73. data/test/fixtures/vcr_cassettes/purchase_order_line_items.yml +64 -0
  74. data/test/fixtures/vcr_cassettes/purchase_order_line_items_count.yml +62 -0
  75. data/test/fixtures/vcr_cassettes/purchase_orders.yml +63 -0
  76. data/test/fixtures/vcr_cassettes/purchase_orders_count.yml +62 -0
  77. data/test/fixtures/vcr_cassettes/tax_types.yml +74 -0
  78. data/test/fixtures/vcr_cassettes/tax_types_count.yml +62 -0
  79. data/test/fixtures/vcr_cassettes/users.yml +60 -0
  80. data/test/fixtures/vcr_cassettes/users_count.yml +58 -0
  81. data/test/fixtures/vcr_cassettes/users_current.yml +54 -0
  82. data/test/fixtures/vcr_cassettes/variants.yml +60 -0
  83. data/test/fixtures/vcr_cassettes/variants_count.yml +58 -0
  84. data/test/gecko_test.rb +7 -0
  85. data/test/helpers/association_helper_test.rb +56 -0
  86. data/test/helpers/inspection_helper_test.rb +27 -0
  87. data/test/helpers/serialization_helper_test.rb +30 -0
  88. data/test/helpers/validation_helper_test.rb +24 -0
  89. data/test/record/account_adapter_test.rb +43 -0
  90. data/test/record/address_adapter_test.rb +14 -0
  91. data/test/record/address_test.rb +18 -0
  92. data/test/record/company_adapter_test.rb +14 -0
  93. data/test/record/company_test.rb +18 -0
  94. data/test/record/contact_adapter_test.rb +14 -0
  95. data/test/record/contact_test.rb +18 -0
  96. data/test/record/currency_adapter_test.rb +14 -0
  97. data/test/record/currency_test.rb +18 -0
  98. data/test/record/fulfillment_adapter_test.rb +24 -0
  99. data/test/record/fulfillment_line_item_adapter_test.rb +21 -0
  100. data/test/record/fulfillment_line_item_test.rb +18 -0
  101. data/test/record/fulfillment_test.rb +27 -0
  102. data/test/record/image_adapter_test.rb +14 -0
  103. data/test/record/image_test.rb +25 -0
  104. data/test/record/invoice_adapter_test.rb +14 -0
  105. data/test/record/invoice_line_item_adapter_test.rb +20 -0
  106. data/test/record/invoice_line_item_test.rb +18 -0
  107. data/test/record/invoice_test.rb +18 -0
  108. data/test/record/location_adapter_test.rb +14 -0
  109. data/test/record/location_test.rb +18 -0
  110. data/test/record/order_adapter_test.rb +14 -0
  111. data/test/record/order_line_item_adapter_test.rb +14 -0
  112. data/test/record/order_line_item_test.rb +18 -0
  113. data/test/record/order_test.rb +18 -0
  114. data/test/record/product_adapter_test.rb +32 -0
  115. data/test/record/product_test.rb +18 -0
  116. data/test/record/purchase_order_adapter_test.rb +14 -0
  117. data/test/record/purchase_order_line_item_adapter_test.rb +14 -0
  118. data/test/record/purchase_order_line_item_test.rb +18 -0
  119. data/test/record/purchase_order_test.rb +18 -0
  120. data/test/record/tax_type_adapter_test.rb +14 -0
  121. data/test/record/tax_type_test.rb +18 -0
  122. data/test/record/user_adapter_test.rb +27 -0
  123. data/test/record/user_test.rb +18 -0
  124. data/test/record/variant_adapter_test.rb +14 -0
  125. data/test/record/variant_test.rb +44 -0
  126. data/test/support/let.rb +10 -0
  127. data/test/support/shared_adapter_examples.rb +159 -0
  128. data/test/support/shared_record_examples.rb +21 -0
  129. data/test/support/testing_adapter.rb +11 -0
  130. data/test/support/vcr_support.rb +7 -0
  131. data/test/test_helper.rb +21 -0
  132. metadata +430 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3e9fd6fed6c04a1519102f2148126d4afce428a
4
+ data.tar.gz: 6f9df328f522933a00cc2153b1bdc0ddf3b21478
5
+ SHA512:
6
+ metadata.gz: f2fd067297069ac22a4f11fe40690317c217b121c2eed07d286c386702c98105ba1546a30cc0a5ab9eb060742b159eea01bdfba6d314775f59968bd079d37039
7
+ data.tar.gz: 11fe376aeafdbbce26989aad6f7ff9d604bdf89b0e295907f0baf07e6baaf07a07d54e29f1f99d1db24982447afdcc210e5d04893a8d05d12ede80a42200a0df
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .env
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ bin
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ # I personally disagree with this default and much rather outdented keywords
2
+ AccessModifierIndentation:
3
+ EnforcedStyle: outdent
4
+
5
+ SpaceAroundEqualsInParameterDefault:
6
+ EnforcedStyle: no_space
7
+
8
+ IndentHash:
9
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ ## Unreleased
2
+
3
+ - Renamed gem so we can publish it on RubyGems.org
4
+ - Add `Gecko::Record::TaxType`
5
+ - Add `Gecko::Record::PurchaseOrder` and `Gecko::Record::PurchaseOrderLineItem`
6
+ - Add `Gecko::Record::Invoice` & `Gecko::Record::InvoiceLineItem`
7
+ - Change pagination numbers to fetch from the headers instead of the metadata object
8
+ - Add `Gecko::Record::Contact`
9
+ - Add `Gecko::Record::User`
10
+ - Add `Gecko::Record::Location`
11
+ - Remove `@client.Account.build`
12
+ - TradeGecko API updates - Variant prices + locations now returned as arrays
13
+
14
+ ## 0.0.3 (2014-03-27)
15
+
16
+ - Add `record.save`
17
+ - Parse server-side validation errors
18
+ - Add support for readonly attributes in serialization
19
+ - Basic serialization support via `as_json` and `serializable_hash` helpers
20
+
21
+ ## 0.0.2 (2014-03-08)
22
+
23
+ - AS::Notifications for API calls
24
+ - Add `@client.Record.build(attributes)`
25
+ - Add `Record#persisted?`
26
+ - Implement a custom User-Agent
27
+
28
+ ## 0.0.1 (2014-03-07)
29
+
30
+ - Add `rake console`
31
+ - Verify an ID is passed to the find methods
32
+ - Custom `RecordNotFound` errors
33
+ - Add current account query method `@client.Account.current`
34
+ - Add `Gecko::Record::Account`
35
+ - Custom `record#inspect`
36
+ - Adds a basic IdentityMap and related query methods
37
+ - Basic query methods for Records `client.Record.find(1)`, `client.Record.where()`
38
+ - Adds `@client.Record` record adapter accessors
39
+ - Add `belongs_to`/`has_many` support to `Gecko::Record` types
40
+ - Defines the framework for Gecko::Record::XXXX record classes
41
+ - Gecko::Client wraps around TradeGecko Authentication
data/CONTRIBUTING.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gecko.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TradeGecko Pte Ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # TradeGecko RubyGem [![Build Status](https://travis-ci.org/tradegecko/gecko.png)](https://travis-ci.org/tradegecko/gecko) [![Code Climate](https://codeclimate.com/github/tradegecko/gecko.png)](https://codeclimate.com/github/tradegecko/gecko)
2
+ The official TradeGecko API RubyGem
3
+
4
+ ## Introduction
5
+
6
+ This library provides a Ruby interface to publicly available (beta) API for TradeGecko.
7
+
8
+ If you are unfamiliar with the TradeGecko API, you can read the documentation located at [http://developer.tradegecko.com](http://developer.tradegecko.com)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'gecko-ruby', '~> 0.0.4'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install gecko-ruby
23
+
24
+ ## Basic Usage
25
+
26
+ ```ruby
27
+ client = Gecko::Client.new(CLIENT_ID, CLIENT_SECRET)
28
+ client.access_token = existing_access_token
29
+
30
+ products = client.Product.where(q: "Gecko")
31
+ #=> [<Gecko::Record::Product id=1 name="Geckotron">, <Gecko::Record::Product id=3 name="Green Gecko">]
32
+ ```
33
+
34
+ ## Finding Records
35
+
36
+ #### Basic finders
37
+ ```ruby
38
+ client.Product.find(123)
39
+ #=> <Gecko::Record::Product id=123 name="Geckotron">
40
+ client.Product.find_many(123, 124)
41
+ #=> [<Gecko::Record::Product id=123 name="Geckotron">, <Gecko::Record::Product id=124 name="Salamander">
42
+ client.Product.where(ids: [123, 124])
43
+ #=> [<Gecko::Record::Product id=123 name="Geckotron">, <Gecko::Record::Product id=124 name="Salamander">
44
+ ```
45
+
46
+ #### Identity Map
47
+
48
+ The Gecko gem ships with a basic identity map
49
+
50
+ ```ruby
51
+ client.Product.find(123)
52
+ # Makes an API request
53
+
54
+ client.Product.find(123)
55
+ # Returned from identity map on second request, no API request
56
+
57
+ client.Product.fetch(123)
58
+ # Does not use identity map and makes request regardless
59
+
60
+ client.Product.find_many([123, 124])
61
+ # Will return 123 from memory and make request for 124
62
+
63
+ client.Product.where(ids: [123, 124])
64
+ # Does not use identity map and makes request regardless
65
+ ```
66
+
67
+ ## Building Records
68
+
69
+ ```ruby
70
+ geckobot = client.Product.build(name: "Geckobot", product_type: "Robot")
71
+ #=> <Gecko::Record::Product id=nil name="Geckobot" product_type: "Robot">
72
+ geckobot.persisted?
73
+ #=> false
74
+ ```
75
+
76
+ ## Persisting Records
77
+
78
+ #### Create Record
79
+
80
+ ```ruby
81
+ geckobot = client.Product.build(name: "Geckobot", product_type: "Robot")
82
+ #=> <Gecko::Record::Product id=nil name="Geckobot" product_type: "Robot">
83
+ geckobot.persisted?
84
+ #=> false
85
+ geckobot.save # Persists to API
86
+ #=> true
87
+ geckobot
88
+ #=> <Gecko::Record::Product id=124 name="Geckobot" product_type: "Robot">
89
+ ```
90
+
91
+ #### Update Record
92
+
93
+ ```ruby
94
+ geckobot = client.Product.find(124)
95
+ #=> <Gecko::Record::Product id=124 name="Geckobot" product_type: "Robot">
96
+ geckobot.persisted?
97
+ #=> true
98
+ geckobot.product_type = "Robo-boogie"
99
+ geckobot.save # Persists to API
100
+ #=> true
101
+ geckobot
102
+ #=> <Gecko::Record::Product id=124 name="Geckobot" product_type: "Robo-boogie">
103
+ ```
104
+
105
+ #### Validations
106
+
107
+ ```ruby
108
+ geckobot = client.Product.find(124)
109
+ #=> <Gecko::Record::Product id=124 name="Geckobot" product_type: "Robot">
110
+ geckobot.persisted?
111
+ #=> true
112
+ geckobot.name = nil
113
+ geckobot.save # Attempts to persist to API
114
+ #=> false
115
+ geckobot.valid?
116
+ #=> false
117
+ geckobot.errors
118
+ #=> #<Gecko::Errors:0x007ff46d961810 @base=#<Gecko::Record::Base:0x007ff46d96aaa0 id: 124, name: nil>, @messages={:name=>["can't be blank"]}>
119
+ ```
120
+
121
+ ## Instrumentation
122
+
123
+ The Gecko gem supports instrumentation via AS::Notifications.
124
+ You can subscribe to API calls by subscribing to `'request.gecko'` notifications
125
+
126
+ ```ruby
127
+ ActiveSupport::Notifications.subscribe('request.gecko') do |name, start, finish, id, payload|
128
+ # Do Something
129
+ end
130
+ ```
131
+
132
+ ## TODO
133
+ - Deleting records
134
+ - Complete record collection
135
+ - Handle more API Errors
136
+ - Clean up Access Token management
137
+
138
+ ## Contributing
139
+
140
+ 1. Fork it ( http://github.com/[my-github-username]/gecko/fork )
141
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
142
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
143
+ 4. Push to the branch (`git push origin my-new-feature`)
144
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.pattern = "test/**/*_test.rb"
10
+ end
11
+
12
+ desc "Open an irb session with Gecko"
13
+ task :console do
14
+ require 'irb'
15
+ require 'irb/completion'
16
+ require 'gecko-ruby'
17
+ ARGV.clear
18
+ IRB.start
19
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gecko/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gecko-ruby'
8
+ spec.version = Gecko::VERSION
9
+ spec.authors = ["Bradley Priest"]
10
+ spec.email = ['bradley@tradegecko.com']
11
+ spec.description = %q{A Ruby interface to the TradeGecko API.}
12
+ spec.summary = spec.description
13
+ spec.homepage = 'https://github.com/tradegecko/gecko/'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "vcr"
25
+ spec.add_development_dependency "webmock"
26
+ spec.add_development_dependency "mocha"
27
+ spec.add_development_dependency "dotenv"
28
+ spec.add_development_dependency "thor"
29
+ spec.add_development_dependency "pry"
30
+
31
+ spec.add_dependency "oauth2"
32
+ spec.add_dependency "virtus"
33
+ spec.add_dependency "activesupport"
34
+ end
data/generate.thor ADDED
@@ -0,0 +1,81 @@
1
+ require 'active_support/inflector'
2
+ class Generate < Thor
3
+ include Thor::Actions
4
+
5
+ desc "model MODEL_NAME", "generates a new model"
6
+ def model(model_name)
7
+ @model_name = model_name
8
+ @file_name = model_name.underscore
9
+ @plural = ask "What is the underscore plural of #{@model_name}? (#{@file_name}s):"
10
+ @plural = @plural.to_s.length > 0 ? @plural : @file_name + "s"
11
+ create_file "lib/gecko/record/#{@file_name}.rb", model_template
12
+ create_file "test/record/#{@file_name}_adapter_test.rb", test_adapter_template
13
+ create_file "test/record/#{@file_name}_test.rb", test_template
14
+ insert_into_file("lib/gecko/client.rb", " record :#{@model_name}\n", before: "\n # Return OAuth client")
15
+ append_to_file("lib/gecko.rb", "require 'gecko/record/#{@file_name}'")
16
+ end
17
+
18
+ private
19
+ def default_plural?
20
+ @plural == @file_name + "s"
21
+ end
22
+
23
+ def model_template
24
+ %{require 'gecko/record/base'
25
+
26
+ module Gecko
27
+ module Record
28
+ class #{@model_name} < Base
29
+ # belongs_to :widget
30
+ # has_many :wodgets
31
+ # attribute :name, String
32
+ end
33
+
34
+ class #{@model_name}Adapter < BaseAdapter
35
+ #{"def plural_name
36
+ '#{@plural}'
37
+ end" if !default_plural?}
38
+ end
39
+ end
40
+ end
41
+ }
42
+ end
43
+
44
+ def test_template
45
+ %{require 'test_helper'
46
+
47
+ class Gecko::#{@model_name}Test < Minitest::Test
48
+ include VCRHelper
49
+ include SharedRecordExamples
50
+
51
+ let(:plural_name) { "#{@plural}" }
52
+ let(:record_class) { Gecko::Record::#{@model_name} }
53
+
54
+ def setup
55
+ @json = load_vcr_hash("#{@plural}", "#{@plural}").first
56
+ @record = record_class.new(client, @json)
57
+ end
58
+
59
+ def test_initializes_record
60
+ assert_instance_of(Gecko::Record::#{@model_name}, @record)
61
+ end
62
+ end}
63
+ end
64
+
65
+ def test_adapter_template
66
+ %{require 'test_helper'
67
+
68
+ class Gecko::Record::#{@model_name}AdapterTest < Minitest::Test
69
+ include TestingAdapter
70
+ include SharedAdapterExamples
71
+
72
+ let(:adapter) { @client.#{@model_name} }
73
+ let(:plural_name) { "#{@plural}" }
74
+ let(:record_class) { Gecko::Record::#{@model_name} }
75
+
76
+ def test_initializes_adapter
77
+ assert_instance_of(Gecko::Record::#{@model_name}Adapter, @client.#{@model_name})
78
+ end
79
+ end}
80
+ end
81
+ end
data/lib/gecko-ruby.rb ADDED
@@ -0,0 +1 @@
1
+ require 'gecko'
data/lib/gecko.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'virtus'
2
+ require 'active_support/inflector'
3
+ require 'active_support/notifications'
4
+ require 'oauth2'
5
+
6
+ require 'gecko/client'
7
+
8
+ # Include models
9
+ require 'gecko/record/base'
10
+ require 'gecko/record/base_adapter'
11
+ require 'gecko/record/exceptions'
12
+
13
+ require 'gecko/record/account'
14
+ require 'gecko/record/address'
15
+ require 'gecko/record/company'
16
+ require 'gecko/record/contact'
17
+ require 'gecko/record/currency'
18
+ require 'gecko/record/order'
19
+ require 'gecko/record/order_line_item'
20
+ require 'gecko/record/fulfillment'
21
+ require 'gecko/record/fulfillment_line_item'
22
+ require 'gecko/record/invoice'
23
+ require 'gecko/record/invoice_line_item'
24
+ require 'gecko/record/product'
25
+ require 'gecko/record/variant'
26
+ require 'gecko/record/image'
27
+ require 'gecko/record/location'
28
+ require 'gecko/record/user'
29
+ require 'gecko/record/purchase_order'
30
+ require 'gecko/record/purchase_order_line_item'
31
+ require 'gecko/record/tax_type'
@@ -0,0 +1,132 @@
1
+ require 'faraday'
2
+ require 'oauth2/version'
3
+ require 'gecko/version'
4
+ require 'gecko/helpers/record_helper'
5
+
6
+ module Gecko
7
+ # The Gecko::Client class
8
+ class Client
9
+ extend Helpers::RecordHelper
10
+
11
+ record :Account
12
+ record :Currency
13
+ record :Location
14
+ record :User
15
+
16
+ record :Address
17
+ record :Company
18
+ record :Contact
19
+
20
+ record :Order
21
+ record :OrderLineItem
22
+ record :Fulfillment
23
+ record :FulfillmentLineItem
24
+ record :Invoice
25
+ record :InvoiceLineItem
26
+
27
+ record :Product
28
+ record :Variant
29
+ record :Image
30
+ record :PurchaseOrder
31
+ record :PurchaseOrderLineItem
32
+ record :TaxType
33
+
34
+ # Return OAuth client
35
+ #
36
+ # @return [OAuth2::Client]
37
+ #
38
+ # @api private
39
+ attr_reader :oauth_client
40
+
41
+ # Access the OAuth AccessToken for this client
42
+ #
43
+ # @return [OAuth2::AccessToken]
44
+ #
45
+ # @api public
46
+ attr_accessor :access_token
47
+
48
+ # Initialize a new Gecko client object with an instantiated OAuth2::Client
49
+ #
50
+ # @param [String] client_id
51
+ # @param [String] client_secret
52
+ # @param [#to_hash] extra options hash to pass to the OAuth2::Client
53
+ #
54
+ # @return [undefined]
55
+ #
56
+ # @api private
57
+ def initialize(client_id, client_secret, options={})
58
+ setup_oauth_client(client_id, client_secret, options)
59
+ end
60
+
61
+ # Authorize client from existing access and refresh token
62
+ #
63
+ # @example
64
+ # client.authorize_from_existing("ABC", "DEF", "1393982563")
65
+ #
66
+ # @param [String] access_token
67
+ # @param [String] refresh_token
68
+ # @param [String] expires_at
69
+ #
70
+ # @return [OAuth2::AccessToken]
71
+ #
72
+ # @api public
73
+ def authorize_from_existing(access_token, refresh_token, expires_at)
74
+ @access_token = OAuth2::AccessToken.new(oauth_client, access_token, {
75
+ refresh_token: refresh_token,
76
+ expires_at: expires_at
77
+ })
78
+ end
79
+
80
+ # Authorize client by fetching a new access_token via refresh token
81
+ #
82
+ # @example
83
+ # client.authorize_from_refresh_token("DEF")
84
+ #
85
+ # @param [String] refresh_token
86
+ #
87
+ # @return [OAuth2::AccessToken]
88
+ #
89
+ # @api public
90
+ def authorize_from_refresh_token(refresh_token)
91
+ @access_token = oauth_client.get_token({
92
+ client_id: oauth_client.id,
93
+ client_secret: oauth_client.secret,
94
+ refresh_token: refresh_token,
95
+ grant_type: 'refresh_token'
96
+ })
97
+ end
98
+
99
+ # Fetch the adapter for a specified Gecko::Record class
100
+ #
101
+ # @param [String] klass_name Gecko::Record subclass name
102
+ #
103
+ # @return [Gecko::Record::BaseAdapter]
104
+ #
105
+ # @api private
106
+ def adapter_for(klass_name)
107
+ self.public_send(klass_name.to_sym)
108
+ end
109
+
110
+ private
111
+
112
+ def setup_oauth_client(client_id, client_secret, options)
113
+ defaults = {
114
+ site: 'https://api.tradegecko.com',
115
+ authorize_url: 'https://go.tradegecko.com/oauth/authorize',
116
+ connection_opts: {
117
+ headers: default_headers
118
+ }
119
+ }
120
+ @oauth_client = OAuth2::Client.new(client_id, client_secret, defaults.merge(options))
121
+ end
122
+
123
+ def default_headers
124
+ {
125
+ 'User-Agent' => ["Gecko/#{Gecko::VERSION}",
126
+ "OAuth2/#{OAuth2::Version.to_s}",
127
+ "Faraday/#{Faraday::VERSION}",
128
+ "Ruby/#{RUBY_VERSION}"].join(' ')
129
+ }
130
+ end
131
+ end
132
+ end