Class: NationSync::API

Inherits:
Object
  • Object
show all
Defined in:
lib/nationsync/api.rb

Constant Summary collapse

@@endpoint =

include HTTParty

"https://.nationbuilder.com"

Instance Method Summary collapse

Constructor Details

#initialize(domain, access_token, session_cookie) ⇒ API

Returns a new instance of API.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nationsync/api.rb', line 11

def initialize(domain, access_token, session_cookie)
  @access_token   = access_token
  @session_cookie = session_cookie

  @conn = Faraday.new(:url => "https://#{domain}.nationbuilder.com") do |faraday|
    faraday.adapter(Faraday.default_adapter)
    faraday.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
  end
  @conn.headers['Cookie'] = "_nbuild_session=#{@session_cookie}"
  @conn.params["access_token"] = @access_token
end

Instance Method Details

#pagesObject



34
35
36
# File 'lib/nationsync/api.rb', line 34

def pages
  @conn.get("/admin/theme_tool_api/pages.json").body["pages"]
end

#sitesObject



23
24
25
# File 'lib/nationsync/api.rb', line 23

def sites
  @conn.get("/admin/theme_tool_api/sites").body["sites"]
end

#theme_asset_put(theme_id, fn, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nationsync/api.rb', line 38

def theme_asset_put(theme_id, fn, value)
  @conn.put do |req|
    req.url "/admin/theme_tool_api/themes/#{theme_id}/assets.json"
    req.params = {}
    # req.body = JSON.dump({
    req.body = Rack::Utils.build_nested_query({
      "access_token" => @access_token,
      "asset" => {
        "key" => fn,
        "value" => value
      }
    })
  end
  # data: {
  #   access_token: nation.get("accessToken"),
  #   asset: {
  #     key: file.filename,
  #     value: value,
  #     attachment: attachment
  #   }
  # },
end

#theme_asset_put_attachment(theme_id, fn, value) ⇒ Object

data: {

access_token: nation.get("accessToken"),
asset: {
  key: file.filename,
  value: value,
  attachment: attachment
}

},



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nationsync/api.rb', line 60

def theme_asset_put_attachment(theme_id, fn, value)
  @conn.put do |req|
    req.url "/admin/theme_tool_api/themes/#{theme_id}/assets.json"
    req.params = {}
    req.body = Rack::Utils.build_nested_query({
      "access_token" => @access_token,
      "asset" => {
        "key" => fn,
        "attachment" => Base64.encode64(value)
      }
    })
  end
end

#theme_assets(theme_id) ⇒ Object



30
31
32
# File 'lib/nationsync/api.rb', line 30

def theme_assets(theme_id)
  @conn.get("/admin/theme_tool_api/themes/#{theme_id}/assets.json").body["assets"]
end

#themesObject



27
28
29
# File 'lib/nationsync/api.rb', line 27

def themes
  @conn.get("/admin/theme_tool_api/themes").body["themes"]
end