-
-
Save kivanio/31369985eb3c982e02ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/concern' | |
module NationBuilderApi | |
extend ActiveSupport::Concern | |
def token | |
self.class.token | |
end | |
def nationbuilder(to, data) | |
self.class.nationbuilder to, data | |
end | |
private | |
def client | |
self.class.client | |
end | |
def redirect_uri | |
Rails.application.secrets.nb_callback | |
end | |
module ClassMethods | |
def token | |
the_token = Rails.cache.fetch "token" | |
OAuth2::AccessToken.from_hash(client, access_token: the_token) | |
end | |
def nationbuilder(to, data) | |
(method, endpoint) = to.split /:/ | |
response = token.send(method, "/api/v1/#{endpoint}", { | |
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }, | |
body: data.to_json | |
}) | |
JSON.parse(response.body) | |
end | |
def client | |
site_path = Rails.application.secrets.nb_site | |
OAuth2::Client.new(Rails.application.secrets.nb_client_id, Rails.application.secrets.nb_secret, site: site_path) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment