Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from swalberg/nation_builder_api.rb
Created June 22, 2014 21:40
Show Gist options
  • Save kivanio/31369985eb3c982e02ad to your computer and use it in GitHub Desktop.
Save kivanio/31369985eb3c982e02ad to your computer and use it in GitHub Desktop.
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